Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
There are several brands of sneakers that Iserlohn wants to collect, such as Air Jordan and Nike Pro. And each brand has released various products. For the reason that Iserlohn is definitely a sneaker-mania, he desires to buy at least one product for each brand.
Although the fixed price of each product has been labeled, Iserlohn sets values for each of them based on his own tendency. With handsome but limited money, he wants to maximize the total value of the shoes he is going to buy. Obviously, as a collector, he won’t buy the same product twice.
Now, Iserlohn needs you to help him find the best solution of his problem, which means to maximize the total value of the products he can buy.
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #define eps 1e-14 const int N=1e4+100,M=4e6+10,inf=1e9+10,mod=1e9+7; const ll INF=1e18+10; int dp[11][N]; int v[11][N],w[11][N]; int si[11]; int main() { int n,T,k; while(~scanf("%d%d%d",&n,&T,&k)) { memset(si,0,sizeof(si)); memset(dp[0],0,sizeof(dp[0])); for(int i=1;i<=n;i++) { int flag; scanf("%d",&flag); scanf("%d%d",&v[flag][si[flag]],&w[flag][si[flag]]); si[flag]++; } for(int i=1;i<=k;i++) { for(int t=0;t<=T;t++) dp[i][t]=-inf; for(int t=0;t<si[i];t++) { for(int j=T;j>=v[i][t];j--) { dp[i][j]=max(dp[i][j],dp[i][j-v[i][t]]+w[i][t]); dp[i][j]=max(dp[i][j],dp[i-1][j-v[i][t]]+w[i][t]); } } } if(dp[k][T]>=0) printf("%d\n",dp[k][T]); else printf("Impossible\n"); } return 0; }