-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDP.cpp
More file actions
46 lines (46 loc) · 976 Bytes
/
Copy pathDP.cpp
File metadata and controls
46 lines (46 loc) · 976 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
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<stack>
#include<vector>
#include<fstream>
using namespace std;
typedef vector<int> vi;
int arr[205][20];
int model[205][25];
int M, C,i, j;
int calc(int money, int c)
{
if(money<0)return -10000;
if(c==C)return M-money;
if(arr[money][c]!=-1)return arr[money][c];
int maxx=-10000;
for(int ii=1;ii<=model[c][0];ii++)
{
maxx=max(maxx, calc(money-model[c][ii], c+1));
}
return arr[money][c]=maxx;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &M, &C);
for(i=0;i<C;i++)
{
scanf("%d", &model[i][0]);
for(j=1;j<=model[i][0];j++)
scanf("%d",&model[i][j]);
}
memset(arr, -1, sizeof arr);
int ans=calc(M, 0);
if(ans<0)printf("no solution\n");
else printf("%d\n", ans);
}
return 0;
}