codevs 裝箱問題 1014

題目描述 Description
        有一個箱子容量爲V(正整數,0<=V<=20000),同時有n個物品(0<n<=30),每個物品有一個體積(正整數)。要求n個物品中,任取若干個裝入箱內,使箱子的剩餘空間爲最小。

輸入描述 Input Description

一個整數v,表示箱子容量一個整數n,表示有n個物品接下來n個整數,分別表示這n 個物品的各自體積


輸出描述 Output Description
一個整數,表示箱子剩餘空間。


樣例輸入 Sample Input

24

6
8
3
12
7
9
7


樣例輸出 Sample Output

0


解題思路:

        超水的(不知爲什麼會出現在天梯黃金級),暴力,dp均可

代碼:

     

var
  n,x,a,mx:longint;
  f:array[1..20000] of longint;

procedure try(s,ans:longint);
begin
  if (s<mx) and (s>=0) then mx:=s;
    if ans>x then exit;
      if s<=0 then exit;
        try(s-f[ans],ans+1);
          try(s,ans+1);
end;

begin
  readln(n);
  readln(x);
  for a:=1 to x do
    readln(f[a]);
  mx:=n;
  try(n,1);
  write(mx);
end.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章