-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBlueberries.cpp
41 lines (33 loc) · 939 Bytes
/
Blueberries.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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int dp[1001][1001];
int getMaxBerries(vector<int>& bushes, int i, int nBerries, int N, int K) {
if(i > N)
return nBerries;
if(dp[i][nBerries] == -1) {
if(bushes[i] + nBerries <= K)
dp[i][nBerries] = std::max(getMaxBerries(bushes, i+2, nBerries + bushes[i], N, K),
getMaxBerries(bushes, i+1, nBerries, N, K));
else
dp[i][nBerries] = getMaxBerries(bushes, i+1, nBerries, N, K);
}
return dp[i][nBerries];
}
int main() {
int T;
scanf("%d", &T);
for(int t = 1; t <= T; t++) {
int N, K;
scanf("%d%d", &N, &K);
vector<int> bushes(N);
for(int i = 0; i < N; i++)
scanf("%d", &bushes[i]);
for(int i = 0; i <= N; i++)
for(int j = 0; j <= K; j++)
dp[i][j] = -1;
printf("Scenario #%d: %d\n", t, getMaxBerries(bushes, 0, 0, N, K));
}
return 0;
}