#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N=105;
const int M=10005;
const int inf=-0x7f7f7f7f;
struct node{
int val;
int cost;
};
vector<node>g[N];
int dp[N][M];
int main(){
int n,m,k;
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
for(int i=1;i<=k;i++){
fill(dp[i],dp[i]+m+1,inf);
g[i].clear();
}
int r,c,id;
for(int i=0;i<n;i++){
scanf("%d%d%d",&id,&r,&c);
node temp;
temp.cost=r;
temp.val=c;
g[id].push_back(temp);
}
dp[0][0]=0;
fill(dp[0],dp[0]+m+1,0);
for(int i=1;i<=k;i++){
for(int h=0;h<g[i].size();h++){
node temp=g[i][h];
for(int j=m;j>=temp.cost;j--){
if(dp[i][j-temp.cost]!=inf){
dp[i][j]=max(dp[i][j],dp[i][j-temp.cost]+temp.val);//11
}
if(dp[i-1][j-temp.cost]!=inf){
dp[i][j]=max(dp[i][j],dp[i-1][j-temp.cost]+temp.val);//22
}
}
}
}
if(dp[k][m]==inf) printf("Impossible\n");
else printf("%d\n",dp[k][m]);
}
return 0;
}
这里用dp[i][j]表示符合条件(每组至少一种)的前i组,j费用,可或得的最大价值。
也就是说现在dp[i][j]都是有符合条件的状态得来,保证了结果的正确性,比较坑的是11,22的顺序。。。。这样是为了保证不多加,因为如果temp.cost=0,则就会多加。。