码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2546 饭卡

时间:2015-06-25 21:16:25      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

单独拿5块出来买最贵的东西,然后对接下来的m-5块钱和n-1道菜做01背包处理

#include<iostream>
#include<algorithm>
#include<cstring> 
#define maxn 1000+5
using namespace std;
int n,m;
int p[maxn];
int dp[maxn];
int main()
{
	while(cin>>n&&n)
	{
		for(int i=0;i<n;i++) cin>>p[i];
		sort(p,p+n);
		cin>>m;
		if(m<5)
		{
			cout<<m<<endl;continue;
		}
		int maxx=p[n-1];
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n-1;i++)
		{
			for(int j=m-5;j>=p[i];j--)
			{
				dp[j]=max(dp[j],dp[j-p[i]]+p[i]);
			} 
		}
		cout<<(m-5-dp[m-5])+5-maxx<<endl;
	}
	return 0;
} 


hdu 2546 饭卡

标签:

原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/46640673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!