hdu 2546 01背包
把最贵的先留着最后再买
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h>
#include<string.h>
int dp[1010];
int p[1010];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int n,m,i,j,pos;
while(scanf("%d",&n),n)
{
int Max=0;
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
if(p[i]>Max)
{
pos=i;
Max=p[i];
}
}
scanf("%d",&m);
if(m<5)
printf("%d\n",m);
else
{
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
for(j=m;j>=p[i];j--)
if(i!=pos&&dp[j]<dp[j-p[i]]+p[i])
{
dp[j]=dp[j-p[i]]+p[i];
}
}
printf("%d\n",m-dp[m-5]-Max);
}
}
return 0;
}