poj3628 Bookshelf 2 (0/backpack) __integer

Source: Internet
Author: User

To give N and b, and then give n number, with this n number of some, to find a and, this and is the minimum value of >=b, the output of the minimum and B difference.

Analysis: This problem is very simple, is very obvious 01 knapsack problem, here n items, the weight of each item is C[i], value is w[i] and c[i]==w[i],, capacity for all C[i] and sum. Just start from scratch in f[] and find a minimal >=b is the solution to the problem

Description

Farmer John recently bought another bookshelf for the Cow library, but the shelf was getting filled up quite quickly, and N Ow the "only available" is in the top.

FJ has N cows (1≤n≤20) each with some height of Hi (1≤hi≤1,000,000-these are, very tall). The bookshelf has a height of B (1≤b≤s, where S is the sum of the heights of all cows).

To reach is the top of the bookshelf, one or more of the cows can stand on top of each of the "a stack, so" their total The is the sum of each of the their individual heights. This total height must is no less than the height of the bookshelf in order for the "cows to reach".

Since a taller stack of cows than necessary can be dangerous, your job are to find this set of cows that produces a stack of The smallest height possible such the stack can reach the bookshelf. Your program should print the minimal ' excess ' height between the optimal stack of cows and the bookshelf.

Input

* Line 1:two space-separated integers:n and B
* Lines 2..n+1:line i+1 contains a single Integer:hi

Output

* Line 1: A single integer representing the (non-negative) difference between the total height of the optimal set of cows And the height of the shelf.

Sample Input

5
3
1
3
5
6

Sample Output

1
Code:
#include <iostream>
using namespace std;
int f[1000050],c[10000];
int main ()
{
    int i,j,n,v,sum;
    while (scanf ("%d%d", &n,&v)!=eof)
    {
       memset (f,0,sizeof (f));
       Memset (C,0,sizeof (c));
       sum=0;
       for (i=1;i<=n;i++)
       {
         scanf ("%d", &c[i]);
         Sum+=c[i];
       }
       for (i=1;i<=n;i++)
       {for
         (j=sum;j>=c[i];j--)
           F[j]=max (F[j],f[j-c[i]]+c[i]);
       for (i=1;i<=sum;i++)
       {
         if (f[i]>=v)
         {
           printf ("%d\n", f[i]-v);
           break;
         }
    }} return 0;
}
       

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.