#QHD0000089. Tem thư và kích thước phong bì (Stamps and Envelope Size)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tem thư và kích thước phong bì (Stamps and Envelope Size)
Stamps and Envelope Size
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
A postal service must choose one of several candidate stamp denomination sets. An envelope holds at most stamps, and any denomination may be used repeatedly. A postage amount is reachable when a combination of between one and stamps has total value exactly .
The maximum gap-free coverage of a set is the largest integer such that every postage amount is reachable. Coverage ends at the first unreachable amount, even if some larger amounts are reachable. For example, with denominations and and at most five stamps, amounts through are reachable, but is not, giving coverage .
Choose the candidate set with the largest coverage. Break ties by choosing the set with fewer denominations. If still tied, prefer the set with the smaller largest denomination, then compare the second-largest, third-largest, and so forth. The denominations of each candidate are distinct and listed in increasing order.
You must compare the given candidates, not invent a new denomination set.
Input
The first line contains , the maximum number of stamps per envelope. The second line contains , the number of candidate sets. Each of the next lines begins with the denomination count , followed by strictly increasing positive denominations.
The original UVa input consists of multiple datasets terminated by . This version contains one dataset and therefore no sentinel.
Output
Print one line in the form max coverage = C : a1 a2 ... ad, where is the maximum gap-free coverage and are the denominations of the selected set. Separate the numbers with spaces.
Subtasks
-
Subtask 1 (20 points): , , , strictly increasing denominations in .
-
Subtask 2 (30 points): , , , denominations in .
-
Subtask 3 (50 points): , , , denominations in .
Examples
Example 1
Input
5
2
4 1 4 12 21
4 1 5 12 28
Output
max coverage = 71 : 1 4 12 21
Explanation: Both sets reach coverage with at most five stamps. Each has four denominations, so the set with smaller maximum denomination wins over .
Example 2
Input
6
2
3 1 5 8
4 1 5 7 8
Output
max coverage = 48 : 1 5 7 8
Explanation: The second set has a larger gap-free coverage and therefore wins despite using an extra denomination.