-
Notifications
You must be signed in to change notification settings - Fork 0
/
11450.cpp
71 lines (66 loc) · 1.02 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
11450 - Wedding shopping
*/
#include<bits/stdc++.h>
using namespace std;
int N,M,C,k;
int check[210][25];
int cost[25][25];
int test(int money, int g)
{
if(money<0){
return -1000000000;}
if(g==C)
return M-money;
if(check[money][g]!=-1)
return check[money][g];
int ans=-1;
for(int i=1; i<=cost[g][0]; i++){
ans = max(ans, test(money-cost[g][i],g+1));
}
return check[money][g]=ans;
}
int main()
{
cin>>N;
while(N--)
{
cin>>M>>C;
for(int i=0; i<C; i++)
{
cin>>cost[i][0];
for(int j=1; j<=cost[i][0]; j++)
{
cin>>cost[i][j];
}
}
memset(check,-1,sizeof(check));
int ans = test(M,0);
if(ans<0)
cout<<"no solution\n";
else
cout<<ans<<endl;
}
return 0;
}
/*
Sample Input
3
100 4
3 8 6 4
2 5 10
4 1 3 3 7
4 50 14 23 8
20 3
3 4 6 8
2 5 10
4 1 3 5 5
5 3
3 6 4 8
2 10 6
4 7 3 1 7
Sample Output
75
19
no solution
*/