-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfoodie.cpp
49 lines (46 loc) · 1.06 KB
/
foodie.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
#include<cstdio>
#include<cstring>
int dp[101][1001];
int computeMax(int* items, int index, int k) {
if(index < 0) {
return 0;
}
if(k<=0) {
return 0;
}
if(dp[index][k] != -1) {
return dp[index][k];
}
int val1 = 0;
if(k - items[index] >= 0) {
val1 = computeMax(items, index - 1, k - items[index]) + items[index];
}
int val2 = computeMax(items, index - 1, k);
int maxVal = val1 > val2?val1:val2;
dp[index][k] = maxVal;
return maxVal;
}
int main() {
int t;
scanf("%d", &t);
while(t--) {
memset(dp, -1, sizeof(dp));
int n, k;
scanf("%d %d", &n, &k);
int* items = new int[n];
for(int i=0;i<n;++i) {
int cnt;
scanf("%d", &cnt);
int sum = 0;
while(cnt--) {
int tmp;
scanf("%d", &tmp);
sum += tmp;
}
items[i] = sum;
}
int val = computeMax(items, n-1, k);
printf("%d\n", val);
delete [] items;
}
}