Problem Description
After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store.
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.
![](https://i-blog.csdnimg.cn/blog_migrate/ef35afe76885c46ac5bbfbb33553acea.jpeg)
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.
Input
Input contains multiple test cases. Each test case begins with three integers 1<=N<=100 representing the total number of products, 1 <= M<= 10000 the money Iserlohn gets, and 1<=K<=10 representing the sneaker brands. The following N lines each represents a product with three positive integers 1<=a<=k, b and c, 0<=b,c<100000, meaning the brand’s number it belongs, the labeled price, and the value of this product. Process to End Of File.
Output
For each test case, print an integer which is the maximum total value of the sneakers that Iserlohn purchases. Print "Impossible" if Iserlohn's demands can’t be satisfied.
Sample Input
5 10000 3 1 4 6 2 5 7 3 4 99 1 55 77 2 44 66
Sample Output
255
#include <iostream>
using namespace std;
#include<algorithm>
#include<string.h>
long long dp[20][20000];
struct hyf
{
int a,ww,vv;
}b[105];
struct node
{
int w[105],v[105];
}p[105];
int cmp(struct hyf c,struct hyf d)
{
return c.a<d.a;
}
int main()
{
int n,m,h,i,j,k,vis[105],j1,i1,num[105];
while(cin>>n>>m>>h)
{
memset(num,0,sizeof(num));
for(i=0;i<n;i++)
cin>>b[i].a>>b[i].ww>>b[i].vv;
for(i=0;i<n;i++)
{
p[b[i].a].v[num[b[i].a]]=b[i].vv;
p[b[i].a].w[num[b[i].a]]=b[i].ww;
num[b[i].a]++;
}
memset(dp,-1,sizeof(dp));
for(j=0;j<=m;j++)
dp[0][j]=0;
for(i=1;i<=h;i++)
for(k=0;k<num[i];k++)
for(j=m;j>=p[i].w[k];j--)//注意for循环的顺序,只有这样才能保证每组至少取一个。
{ if(dp[i][j-p[i].w[k]]!=-1)
dp[i][j]=max(dp[i][j],dp[i][j-p[i].w[k]]+p[i].v[k]);
if(dp[i-1][j-p[i].w[k]]!=-1)
dp[i][j]=max(dp[i][j],dp[i-1][j-p[i].w[k]]+p[i].v[k]);
//这两处位置不能颠倒,因为当代价为0时,可能会出现dp[i][k]=dp[i-1][j-0]+v,dp[i][j]=dp[i][j-0]+v=dp[i-1][j-0]+v+v;重复计算了一次。
}
if(dp[h][m]<0)
cout<<"Impossible\n";
else
cout<<dp[h][m]<<endl;
}
return 0;
}