Knapsack problem--packing problem (Valley 1049) with true template

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.