-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11450.cpp
46 lines (43 loc) · 944 Bytes
/
11450.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h>
using namespace std;
int M,C;
int gar[30][30];
int memo[220][30];
int rec(int n,int m) {
if(m<0) {
return -1000000;
}
if(n==C+1) return M-m;
int mk=-10000;
if(memo[m][n]==-1) {
for(int i=1; i<=gar[n][0]; i++) {
mk=max(rec(n+1,m-gar[n][i]),mk);
}
memo[m][n]=mk;
}
memo[m][n]=mk;
memo[m][n]=max(memo[m][n],mk);
return memo[m][n];
}
int main() {
//freopen("output.txt","w",stdout);
int N;
cin >> N;
while(N--) {
cin >> M >> C;
memset(gar,-1,sizeof gar);
memset(memo,-1,sizeof memo);
for(int i=1; i<=C; i++) {
cin >> gar[i][0];
for(int j=1; j<=gar[i][0]; j++) {
cin >> gar[i][j];
}
}
int v=rec(1,M);
if(v>=0) {
cout << v << endl;
} else {
cout << "no solution" << endl;
}
}
}