POJ 3628 Bookshelf 2 (01 backpack)

Source: Internet
Author: User

POJ 3628 Bookshelf 2 (01 backpack)
Bookshelf 2

Time Limit:1000 MS Memory Limit:65536 K
Total Submissions:7496 Accepted:3451

Description

Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

FJ hasNCows (1 ≤N≤ 20) each with some heightHi(1 ≤Hi≤ 1,000,000-these are very tall cows). The bookshelf has a heightB(1 ≤BS, WhereSIs the sum of the heights of all cows ).

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

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

Input

* Line 1: Two space-separated integers:NAndB
* Lines 2 ..N+ 1: LineI+ 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 1631356

Sample Output

1

Source

USACO 2007 December Bronze

Meaning: give n AND B, then give n number, and use some of the n numbers to find a sum. The sum is> = the minimum value of B, and the difference between the output minimum value and B.

Analysis: This question is very simple. It is a very obvious 01 backpack problem. Here n items, each of which weighs c [I], the value is w [I] and c [I] = w [I]. The capacity is the sum of all c [I. You only need to find the minimum value> = B from the beginning in f [].

At first, I read the wrong question and thought it was the closest value to B. WA has been around for countless times.
Code: 46 MS
# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define M 1000005 # define N 10005int map [N], dp [M]; int main () {int I, j, n, v, sum; while (cin> n> v) {memset (dp, 0, sizeof (dp); memset (map, 0, sizeof (map); sum = 0; for (I = 1; I <= n; I ++) {cin> map [I]; sum + = map [I] ;}for (I = 1; I <= n; I ++) for (j = sum; j> = map [I]; j --) dp [j] = max (dp [j], dp [j-map [I] + map [I]); for (I = 1; I <= sum; I ++) {if (dp [I]> = v) // obviously, the first one must meet the conditions when it is greater than v. {Cout <
    
     

Related Article

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.