Title Description DescriptionThere is a box with a capacity of V (positive integer, 0<=v<=20000), and at the same time there are n items (0<n<=30, each item has a volume (positive integer).
Requires n items, any number of boxes in the box, so that the remaining space of the box is minimal.
input/output format input/output
Input Format:
An integer representing the box capacity
An integer that indicates that there are n items
The next n lines, respectively, indicate the respective volume of the N items.
output Format:
An integer that represents the remaining space of the box.
input and Output sample sample Input/output
sample Test point # #
Input Sample:
24
6
8
3
12
7
9
7
Sample output:
0
var v,n,i,j,max:longint;
A:ARRAY[1..30] of Longint;
F:ARRAY[1..20000] of Boolean; Indicates whether it can be reached
Begin
Readln (v);
READLN (n);
For I:=1 to N do readln (A[i]);
F[0]:=true;
For I:=1 to N do
For J:=v-a[i] Downto 0 do//01 backpack upside down
If F[J] then f[j+a[i]]:=true; If F[J] can arrive, then F[j+a[i]] can also go to
For I:=v Downto 1 does//upside down to find the largest reachable package
If F[i] Then
Begin
Max:=i;
Break
End
Writeln (V-max);
End.
Knapsack problem--packing problem (Valley 1049) with true template