Blueberries
DP
#include <iostream>
#include<cstring>
using namespace std;
const int MAX = 1005;
int main()
{
int t, n, k;
int dp[MAX][MAX];
int raspBerries[MAX];
cin >> t;
int s = 1;
while(s <= t)
{
cin >> n >> k;
for(int i = 0; i < n; i++)
{
cin >> raspBerries[i];
}
for(int i = 0; i < MAX; i++)
{
for(int j = 0; j < MAX; j++)
{
dp[i][j] = 0;
}
}
for(int i = 1; i <= n; i++)
{
for(int w = 1; w <= k; w++)
{
if(i == 1)
{
if(raspBerries[i - 1] <= w)
{
dp[i][w] = raspBerries[i - 1];
}
}
else
{
if(raspBerries[i - 1] <= w)
{
dp[i][w] = max(raspBerries[i - 1] + dp[i - 2][w - raspBerries[i - 1]], dp[i - 1][w]);
}
else
{
dp[i][w] = dp[i - 1][w];
}
}
}
}
cout << "Scenario #" << s << ": " << dp[n][k] << endl;
s++;
}
return 0;
}
Comments
Post a Comment