PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000089. Tem thư và kích thước phong bì (Stamps and Envelope Size)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 SS stamps, and any denomination may be used repeatedly. A postage amount xx is reachable when a combination of between one and SS stamps has total value exactly xx.

The maximum gap-free coverage CC of a set is the largest integer such that every postage amount 1,2,…,C1,2,\ldots,C is reachable. Coverage ends at the first unreachable amount, even if some larger amounts are reachable. For example, with denominations 11 and 33 and at most five stamps, amounts 11 through 1313 are reachable, but 1414 is not, giving coverage 1313.

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 SS, the maximum number of stamps per envelope. The second line contains NN, the number of candidate sets. Each of the next NN lines begins with the denomination count did_i, followed by did_i strictly increasing positive denominations.

The original UVa input consists of multiple datasets terminated by S=0S=0. This version contains one dataset and therefore no sentinel.

Output

Print one line in the form max coverage = C : a1 a2 ... ad, where CC is the maximum gap-free coverage and a1<a2<⋯<ada_1<a_2<\cdots<a_d are the denominations of the selected set. Separate the numbers with spaces.

Subtasks

  • Subtask 1 (20 points): 1≤S≤41\le S\le4, 1≤N≤41\le N\le4, 1≤di≤S1\le d_i\le S, strictly increasing denominations in [1,20][1,20].

  • Subtask 2 (30 points): 1≤S≤71\le S\le7, 1≤N≤71\le N\le7, 1≤di≤S1\le d_i\le S, denominations in [1,60][1,60].

  • Subtask 3 (50 points): 1≤S≤101\le S\le10, 1≤N≤101\le N\le10, 1≤di≤S1\le d_i\le S, denominations in [1,100][1,100].

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 7171 with at most five stamps. Each has four denominations, so the set with smaller maximum denomination 2121 wins over 2828.

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.