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公斤的限制.
``
/* ***********************************************
Author :111qqz
Created Time :2016年04月14日 星期四 19时33分09秒
File Name :code/bzoj/3407.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=5E4+7;
7int n,V;
8int a[505];
9int dp[N];
1void solve ( int cost,int val)
2{
3 for ( int i = V ; i >= cost ; i--)
4 dp[i] = max(dp[i],dp[i-cost]+val);
5}
6int main()
7{
8 #ifndef ONLINE_JUDGE
9 freopen("code/in.txt","r",stdin);
10 #endif
scanf("%d %d",&V,&n);
for ( int i = 1 ; i <= n ; i++) scanf("%d",&a[i]);
1 ms(dp,0);
2 for ( int i = 1 ; i <= n ; i++)
3 {
4 solve(a[i],a[i]);
5 }
1 int ans = 0 ;
2 for ( int i = V ; i >= 0 ; i--) ans = max(ans,dp[i]);
3 printf("%d\n",ans);
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}