# POJ 2392 dp

##### 题意

$dp[i+1][j]:=用前i种数加和得到j时第i种数最多能剩余多少个（不能加和得到j的情况下为-1）$

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define min(a,b)    (((a) < (b)) ? (a) : (b))
#define max(a,b)    (((a) > (b)) ? (a) : (b))
#define abs(x)    ((x) < 0 ? -(x) : (x))
#define INF 0x3f3f3f3f
#define delta 0.85
#define eps 1e-3
#define PI 3.14159265358979323846
#define MAX_K 400
#define MAX_A 40000
using namespace std;
struct block{
int h, a, c;
}blk[MAX_K];
int K;
int dp[MAX_A];

bool cmp(const block& a, const block& b){
return a.a < b.a;
}

int main(){
while(~scanf("%d", &K)){
memset(dp, -1, sizeof(dp));
for(int i = 0; i < K; i++) scanf("%d%d%d", &blk[i].h, &blk[i].a, &blk[i].c);
sort(blk, blk + K, cmp);
dp[0] = 0;
for(int i = 0; i < K; i++){
block &b = blk[i];
for(int j = 0; j <= blk[i].a; j++){
if(dp[j] >= 0) dp[j] = b.c;
else if(j < b.h || dp[j - b.h] <= 0) dp[j] = -1;
else dp[j] = dp[j - b.h] - 1;
}
}
int res;
for(int i = blk[K - 1].a; i >= 0; i--){
if(dp[i] >= 0){
res = i;
break;
}
}
printf("%d\n", res);
}
return 0;
}


©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试