-
Notifications
You must be signed in to change notification settings - Fork 1
/
PELUS.cpp
42 lines (40 loc) · 891 Bytes
/
PELUS.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
#include <iostream>
using namespace std;
int M,C;
int P[22][22];
int DP[202][22];
int solve(int ML, int G) {
if(ML<0)
return -11238712;
if(G == C)
return M-ML;
if(DP[ML][G] != -1) return DP[ML][G];
int res = -1;
for(int i=1; i<=P[G][0]; i++) {
res = max(res, solve(ML - P[G][i], G+1));
}
return DP[ML][G] = res;
}
int main() {
int N;
cin >> N;
for(int n=0; n<N; n++) {
cin >> M >> C;
for(int c=0; c<C; c++) {
cin >> P[c][0];
for(int k=1; k<=P[c][0]; k++) {
cin >> P[c][k];
}
}
for(int i=0; i<=M; i++) {
for(int j=0; j<=C; j++)
DP[i][j] = -1;
}
int res = solve(M,0);
if(res < 0)
cout << "no solution" << endl;
else
cout << res << endl;
}
return 0;
}