BZOJ 3407: [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题(01背包)

3407: [Usaco2009 Oct]Bessie’s Weight Problem 贝茜的体重问题

Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 88  Solved: 79 [Submit][Status][Discuss]

Description

贝茜像她的诸多姊妹一样,因为从约翰的草地吃了太多美味的草而长出了太多的赘肉.所以约翰将她置于一个及其严格的节食计划之中.她每天不能吃多过H(5≤日≤45000)公斤的干草.贝茜只能吃一整捆干草;当她开始吃一捆干草的之后就再也停不下来了.她有一个完整

的N(1≤N≤500)捆可以给她当作晚餐的干草的清单.她自然想要尽量吃到更多的干草.很自然地,每捆干草只能被吃一次(即使在列表中相同的重量可能出现2次,但是这表示的是两捆干草,其中每捆干草最多只能被吃掉一次).

给定一个列表表示每捆干草的重量Si(1≤Si≤H),求贝茜不超过节食的限制的前提下可以吃掉多少干草(注意一旦她开始吃一捆干草就会把那一捆干草全部吃完).

Input

第1行:两个由空格隔开的整数日和N.

第2到第N+1行:第i+l行是一个单独的整数,表示第i捆干草的重量Si.

Output

一个单独的整数表示贝茜在限制范围内最多可以吃多少公斤的干草.

Sample Input

56 4 15 19 20 21

Sample Output

56

HINT

有四捆草,重量分别是15,19,20和21.贝茜在56公斤的限制范围内想要吃多少就可以吃多少.

贝茜可以吃3捆干草(重量分别为15,20,21).恰好达到她的56公斤的限制.

``

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月14日 星期四 19时33分09秒
 4File Name :code/bzoj/3407.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=5E4+7;
34int n,V;
35int a[505];
36int dp[N];
37
38void solve ( int cost,int val)
39{
40    for ( int i = V ; i >= cost ; i--)
41	dp[i] = max(dp[i],dp[i-cost]+val);
42}
43int main()
44{
45	#ifndef  ONLINE_JUDGE 
46	freopen("code/in.txt","r",stdin);
47  #endif
48
49	scanf("%d %d",&V,&n);
50	for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
51
52	ms(dp,0);
53	for ( int i = 1 ; i <= n ; i++)
54	{
55	    solve(a[i],a[i]);
56	}
57
58	int ans = 0 ;
59	for ( int i = V ; i >= 0 ; i--) ans = max(ans,dp[i]);
60	printf("%d\n",ans);
61
62  #ifndef ONLINE_JUDGE  
63  fclose(stdin);
64  #endif
65    return 0;
66}