#include <iostream>
#include <algorithm>
using namespace std;
struct Node
{
int h, c, a;
Node(int h = 0, int c = 0, int a = 0) : h(h), c(c), a(a) {}
};
Node nodes[417];
int dp[2][40017], K;
bool compare(const Node &a, const Node &b)
{
return a.a < b.a;
}
void input()
{
scanf("%d", &K);
for (int i = 1; i <= K; i++)
{
scanf("%d%d%d", &nodes[i].h, &nodes[i].a, &nodes[i].c);
}
sort(nodes + 1, nodes + K + 1, compare);
}
void preHandle()
{
for (int i = 0; i <= 40007; i++)
{
dp[0][i] = -1;
dp[1][i] = -1;
}
}
void solve(int i, int j)
{
if (dp[(i - 1) % 2][j] >= 0)
{
dp[i % 2][j] = nodes[i].c;
}
else
{
if (j >= nodes[i].h && dp[i % 2][j - nodes[i].h] > 0)
{
dp[i % 2][j] = dp[i % 2][j - nodes[i].h] - 1;
}
else
{
dp[i % 2][j] = -1;
}
}
}
void doDp()
{
for (int i = 1; i <= K; i++)
{
dp[i % 2][0] = nodes[i].c;
for (int j = 1; j <= 40007; j++)
{
if (j > nodes[i].a)
{
break;
}
else
{
solve(i, j);
}
}
}
}
int findAns()
{
int ans = -1;
for (int i = 0; i <= 40007; i++)
{
if (dp[K % 2][i] >= 0)
{
if (ans == -1)
{
ans = i;
}
else if (ans < i)
{
ans = i;
}
}
}
return ans;
}
int main()
{
input();
preHandle();
doDp();
printf("%d\n", findAns());
return 0;
}
POJ 2392 Space Elevator 动态规划
最新推荐文章于 2024-08-22 10:56:53 发布