【题意】
n种物品,每种价格为cost[],个数为count[],求所能凑成最接近且小于cash的价值
【输入】
多组数据,每组数据一行,首先为两个数cash、n
接下来2n个数两个一组,分别为count、cost
【输出】
对于每组数据,输出一个值表示答案
多重背包问题,对于每种物品个数分解成2^0、2^1……等等的单件物品做01背包即可
错误原因已经用打字标出来了!
据说必须要读完一组就readln
program poj1276;
var
cash,n,i,j,k,p,q:longint;
count,cost:array [0..11] of longint;
f:array [0..100001] of boolean;
begin
fillchar(f,sizeof(f),false);
f[0]:=true;
while not seekeof do
begin
while i>0 do
begin
f[i]:=false;
dec(i);
end;
read(cash,n);
for i:=1 to n do
read(count[i],cost[i]);
readln;
for i:=1 to n do
begin
j:=count[i];
while j<>0 do
begin
k:=0;
while j>=(1 shl (k+1)) - 1 do
begin
p:=1 shl k;
for q:=cash downto p*cost[i] do
f[q]:=f[q] or f[q-p*cost[i]];
inc(k);
end;
j:=j-(1 shl k)+1;
end;
end;
for i:=cash downto 0 do
if f[i] then break;
writeln(i);
end;
end.