Skip to content

Instantly share code, notes, and snippets.

@KT-Yeh
Created April 30, 2014 02:29
Show Gist options
  • Save KT-Yeh/ae01485bbcef35239970 to your computer and use it in GitHub Desktop.
Save KT-Yeh/ae01485bbcef35239970 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <algorithm>
using namespace std;
int dp[40005] = {0};
struct Block{
int h;
int a;
int c;
}b[401];
bool cmp(const Block &a, const Block &b)
{
return a.a < b.a;
}
int main()
{
int K;
while (scanf("%d", &K) != EOF) {
// Input
for (int k = 0; k < K; ++k)
scanf("%d %d %d", &b[k].h, &b[k].a, &b[k].c);
sort(b, b+K, cmp);
// Knapsack
fill(dp, dp+40005, 0);
for (int k = 0; k < K; ++k)
for (int i = 0; i < b[k].c; ++i)
for (int j = b[k].a; j-b[k].h >= 0; --j)
dp[j] = max(dp[j], dp[j-b[k].h] + b[k].h);
// Ouput
int ans = 0;
for (int i = 0; i <= b[K-1].a; ++i)
ans = max(ans, dp[i]);
printf("%d\n", ans);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment