-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11450.cpp
More file actions
63 lines (58 loc) · 963 Bytes
/
11450.cpp
File metadata and controls
63 lines (58 loc) · 963 Bytes
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
#include<bits/stdc++.h>
#define inf -21483620
using namespace std;
int t,m,c,k,p,max1=0;
vector<int> price[20];
int memo[202][22];
int shop(int money,int g)
{
if(money<0)
return inf;
else if(g==c){
return m-money;
}
else{
int i,res=0;
if(memo[money][g]==-1){
for(i=1;i<=price[g][0];i++){
if(price[g][i]<=money)
memo[money-price[g][i]][g+1]=res=shop(money-price[g][i],g+1);
max1=res>max1?res:max1;
}
memo[money][g]=max1;
}
else{
res=memo[money][g];
max1=res>max1?res:max1;
}
return max1;
}
}
int main()
{
int i,j,res=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&c);
max1=0;
memset(memo,-1,sizeof(memo));
for(i=0;i<c;i++)
{
scanf("%d",&k);
price[i].clear();
price[i].push_back(k);
for(j=0;j<k;j++)
{
scanf("%d",&p);
price[i].push_back(p);
}
}
res=shop(m,0);
if(res!=0)
printf("%d\n",res);
else
printf("no solution\n");
}
return 0;
}