http://acm.hdu.edu.cn/showproblem.php?pid=3033
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int brand[124],price[124],value[124],dp[15][100024]; int main(void) { int N,K,M,i,j,k; while(scanf("%d%d%d",&N,&M,&K)!=EOF) { for(i=0;i<N;i++) scanf("%d%d%d",&brand[i],&price[i],&value[i]); memset(dp,0,sizeof(dp)); for(i=1;i<=K;i++) for(j=0;j<N;j++) for(k=M;k>=price[j];k--) if(brand[j]==i) { dp[i][k]=max(dp[i][k],dp[i][k-price[j]]+value[j]); dp[i][k]=max(dp[i][k],dp[i-1][k-price[j]]+value[j]); } if(dp[K][M]==0) printf("Impossible\n"); else printf("%d\n",dp[K][M]); } return 0; }