# [BZOJ] 1606: [Usaco2008 Dec]Hay For Sale 购买干草

## 1606: [Usaco2008 Dec]Hay For Sale 购买干草

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1335  Solved: 989
[Submit][Status][Discuss]

## Description

约翰遭受了重大的损失：蟑螂吃掉了他所有的干草，留下一群饥饿的牛．他乘着容量为C(1≤C≤50000)个单位的马车，去顿因家买一些干草．  顿因有H(1≤H≤5000)包干草，每一包都有它的体积Vi(l≤Vi≤C).约翰只能整包购买，

## Input

第1行输入C和H，之后H行一行输入一个Vi．

最多的可买干草体积．

## Sample Input

7 3 //总体积为7,用3个物品来背包
2
6
5

The wagon holds 7 volumetric units; three bales are offered for sale with
volumes of 2, 6, and 5 units, respectively.

7 //最大可以背出来的体积

## HINT

Buying the two smaller bales fills the wagon.

Silver

## Code

 1 #include<cstdio>
2 #include<algorithm>
3 #include<iostream>
4 #define maxn 100000
5 using namespace std;
6
7 int arr[maxn],n,T,ans;
8
9 bool cmp(const int a,const int b){
10     return a>b;
11 }
12
13 int main(){
14     scanf("%d%d",&T,&n);
15
16     for(int i = 1;i <= n;i++){
17         scanf("%d",&arr[i]);
18     }
19
20     sort(arr+1,arr+1+n,cmp);
21
22     for(int i = 1;i <= n;i++){
24         for(int j = i;j <= n;j++){
27             }
28         }
30     }
31
32     printf("%d",ans);
33
34     return 0;
35 }

 1 #include<cstdio>
2 #include<iostream>
3 #define maxn 1000000
4 using namespace std;
5
6 int DP[maxn],n,m,cnt;
7
8 int main(){
9
10     scanf("%d%d",&n,&m);
11
12     for(int i = 1;i <= m;i++){
13         scanf("%d",&cnt);
14         DP[cnt] = 1;
15         for(int j = n;j >= cnt;j--){
16             DP[j] = DP[j]|DP[j-cnt];
17         }
18     }
19
20     int ans = n;
21     while(!DP[ans]) ans--;
22
23     printf("%d",ans);
24
25     return 0;
26 }

posted @ 2017-08-31 21:08  Leviaton  阅读(90)  评论(0编辑  收藏  举报