Proud Merchants
题目连接:HDU - 3466
题意:n个物品, 一开始有m单位钞票,每个物品有三个属性:p, q, v;
p是物品价格, q是买下该物品手中至少要有的钞票,v是每个物品的价值;求能买到的物品最大价值和是多少;
若现有A,B两个物品,属性分别为p1, q1, v1; p2, q2, v2; 先买A物品时手中钞票至少要有p1+q2,先买B物品手中至少要有p2+q1单位的钞票;那么先买A的条件为p1+q2>p2+q1,化简为q1-p1<q2-p2;
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m;
struct node{
int p, q, v;
}t[550];
bool cmp(node a, node b){
return a.q-a.p<b.q-b.p;
}
int dp[5500];
int main(){
while(~scanf("%d%d", &n, &m)){
for(int i=0; i<n; i++){
scanf("%d%d%d", &t[i].p, &t[i].q, &t[i].v);
}
memset(dp, 0, sizeof(dp));
sort(t, t+n, cmp);
//for(int i=0; i<n; i++){
// printf("p:%d q:%d v:%d\n", t[i].p, t[i].q, t[i].v);
//}
int ans=0;
for(int i=0; i<n; i++){
for(int j=m; j>=t[i].q; j--){
dp[j]=max(dp[j], dp[j-t[i].p]+t[i].v);
ans=max(dp[j], ans);
}
}
printf("%d\n", dp[m]);
}
return 0;
}