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

#QHD0000007. Mua sắm cho đám cưới (Wedding Shopping)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Mua sắm cho đám cưới (Wedding Shopping)

Wedding Shopping

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

You have a budget of MM monetary units to buy clothes from CC different categories. Category ii contains KiK_i models; model jj has price pi,jp_{i,j}.

You must purchase exactly one model from each category, spending at most MM. Among all feasible selections, find the maximum total amount spent.

If the budget cannot cover one model from every category, print no solution.

Input

The first line contains MM and CC, the budget and the number of clothing categories.

Each of the next CC lines starts with KiK_i, followed by KiK_i integers pi,1,pi,2,…,pi,Kip_{i,1},p_{i,2},\ldots,p_{i,K_i}, the prices in category ii.

Output

Print the largest affordable total for exactly one model from each category, or the exact phrase no solution if no valid selection exists.

Subtasks

  • Subtask 1 (20 points): 1≤M≤351\le M\le 35; 1≤C≤41\le C\le 4; 1≤Ki≤31\le K_i\le 3 (1≤i≤C1\le i\le C); pi,j∈Z>0p_{i,j}\in\mathbb{Z}_{>0} (1≤i≤C1\le i\le C, 1≤j≤Ki1\le j\le K_i).
  • Subtask 2 (30 points): 1≤M≤1001\le M\le 100; 1≤C≤121\le C\le 12; 1≤Ki≤51\le K_i\le 5 (1≤i≤C1\le i\le C); pi,j∈Z>0p_{i,j}\in\mathbb{Z}_{>0} (1≤i≤C1\le i\le C, 1≤j≤Ki1\le j\le K_i).
  • Subtask 3 (50 points): 1≤M≤2001\le M\le 200; 1≤C≤201\le C\le 20; 1≤Ki≤201\le K_i\le 20 (1≤i≤C1\le i\le C); pi,j∈Z>0p_{i,j}\in\mathbb{Z}_{>0} (1≤i≤C1\le i\le C, 1≤j≤Ki1\le j\le K_i).

Examples

Input

100 4
3 8 6 4
2 5 10
4 1 3 3 7
4 50 14 23 8

Output

75

Explanation

Choose items priced 8,10,7,508,10,7,50 from the four groups. Their total is 8+10+7+50=758+10+7+50=75, within the 100100 budget. No feasible selection of one item per group has a higher total.