[BZOJ1606] [Usaco2008 Dec]Hay For Sale 购买干草

[Usaco2008 Dec]Hay For Sale 购买干草

Time Limit: 5 Sec Memory Limit: 64 MB

Description

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

他最多可以运回多少体积的干草呢?

Input

第1行输入C和H,之后H行一行输入一个Vi.

Output

最多的可买干草体积.

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.

Sample Output

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

HINT

Buying the two smaller bales fills the wagon.

Source

Silver

3392ms

var
 dp:array[0..   50000]of longint;
 x:array[0..5000]of longint;
 i,j,m,n:longint;
function max(a,b:longint):longint;
begin
 if a>b
 then exit(a)
 else exit(b);
end;

begin
 readln(m,n);
 for i:=1 to n do
  readln(x[i]);
 for i:=1 to n do
  for j:=m downto x[i] do
   dp[j]:=max(dp[j],dp[j-x[i]]+x[i]);
 writeln(dp[m]);
end.

968ms(记录状态达到,不用判断大小真心好快啊)

var
 dp:array[0..   50000]of longint;
 x:array[0..5000]of longint;
 i,j,m,n:longint;
begin
 readln(m,n);
 for i:=1 to n do
  readln(x[i]);
 dp[0]:=1;
 for i:=1 to n do
  for j:=m downto x[i] do
   if dp[j-x[i]]=1
   then dp[j]:=1;
 for i:=m downto 1 do
  if dp[i]=1
  then begin writeln(i); break; end;
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值