首页 > 代码库 > 杭电2546

杭电2546

#include<iostream>#include<algorithm>#include<string.h>using namespace std;int max(int a,int b){	if(a>b)		return a;	else		return b;}int main(){	int n,price[1200],i,f[1200],Max,m,j;	while(cin>>n&&n)	{		for(i=1;i<=n;i++)		cin>>price[i];		cin>>m;		sort(price+1,price+n+1);		Max=price[n];		memset(f,0,sizeof(f)); 		if(m<5)			cout<<m<<endl;		else		{		for(i=1;i<=n-1;i++)		{			for(j=m-5;j>=price[i];j--)			{				f[j]=max(f[j],f[j-price[i]]+price[i]);				}				}		cout<<m-f[m-5]-Max<<endl;		}					}	return 0;}

  

杭电2546