TOJ 2838.The Eating Puzzle

题目链接:http://acm.tju.edu.cn/toj/showp2838.html


2838.    The Eating Puzzle
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 1489    Accepted Runs: 776     Multiple test files



Bessie is on a diet where she can eat no more than  C (10 ≤  C ≤ 35,000) calories per day. Farmer John is teasing her by putting out  B (1 ≤  B ≤ 21) buckets of feed, each with some (potentially non-unique) number of calories (range: 1..35,000). Bessie has no self-control: once she starts on a feed bucket, she consumes all of it.

Bessie is not so good at combinatorics. Determine the optimal combination of feed buckets that gives Bessie as many calories without exceeding the limit C.

As an example, consider a limit of 40 calories and 6 buckets with 7, 13, 17, 19, 29, and 31 calories. Bessie could eat 7 + 31 = 38 calories but could eat even more by consuming three buckets: 7 + 13 + 19 = 39 calories. She can find no better combination.

Input

* Line 1: Two space-separated integers:  C and  B

* Line 2: B space-separated integers that respectively name the number of calories in bucket 1, 2, etc.

Output

* Line 1: A single line with a single integer that is largest number of calories Bessie can consume and still stay on her diet.

Sample Input

40 6
7 13 17 19 29 31

Sample Output

39



Source: USACO 2006 December Competition
Submit   List    Runs   Forum   Statistics

01背包,过之:

#include <stdio.h>
#include <string.h>
#include <algorithm>
int dp[35001];
int a[22];
int main(){
	int num,n;
	while(~scanf("%d%d",&num,&n)){
		for(int i=0;i<n;i++)
			scanf("%d",&a[i]);
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n;i++)
			for(int j=num;j>=a[i];j--)
				dp[j]=std::max(dp[j],dp[j-a[i]]+a[i]);
		printf("%d\n",dp[num]);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值