HDU 2546 Rice Card (01 backpack)

Source: Internet
Author: User

Rice card

Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)

Total submission (s): 18610 Accepted Submission (s): 6498

Problem Description

UESTC Department Canteen's rice card has a very strange design, that is, before the purchase to determine the balance. If the remaining amount on the card is greater than or equal to 5 yuan before the purchase of an item, the purchase must be successful (even if the balance is negative on the card after purchase), it cannot be purchased (even if the amount is sufficient). So we all want to try to keep the balance on the card to the minimum.

One day, the canteen has n vegetables for sale, each vegetable can be purchased once. If you know the price of each vegetable and the balance on the card, ask at least how much of the balance on the card.

Input

Multiple sets of data. For each group of data:

The first action is an integer n, which indicates the number of dishes. n<=1000.

The second line consists of n positive integers representing the price per vegetable. The price does not exceed 50.

The third line includes a positive integer m, which represents the balance on the card. m<=1000.

N=0 indicates the end of the data.

Output

For each set of inputs, the output line contains an integer that represents the smallest possible balance on the card.

Sample Input

1

50

5

10

1 2 3 2 1 1 2 3 2 1

50

0

Sample Output

-45

32

Source

UESTC 6th Programming Contest Online

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 intv[1010];7 intdp[1010];8 intMain ()9 {Ten     intn,m; One      while(cin>>n&&N) { Amemset (V,0,sizeof(v)); -Memset (DP,0,sizeof(DP)); -          for(intI=1; i<=n;i++) cin>>V[i]; theCin>>m; -Sort (v+1, v+1+n); -         intmaxn=V[n]; -         //Backpack Capacity m-5, number of items n-1 +         //Set aside 5 bucks to buy the most expensive -         if(m<5) {cout<<m<<endl;Continue;} +          for(intI=1; i<n;i++){ A              for(inti=n-5; j>=v[i];j--){ atDp[j]=max (dp[j],dp[j-v[i]]+v[i]); -             } -         } -cout<<m-dp[m-5]-maxn<<Endl; -     } -     return 0; in}

HDU 2546 Rice Card (01 backpack)

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.