Rice card
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 22962 Accepted Submission (s): 8047
Problem description UESTC part of the canteen meal card has a very strange design, that is, before purchasing 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, outputs a line that contains an integer that represents the smallest possible balance on the card.
Sample Input1505101 2 3 2 1 1 2 3 2 1500
Sample Output-4532
SOURCEUESTC 6th Programming Contest Online
1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 intMain ()6 {7 intn,m;8 intprice[1050];9 intd[1050];Ten while(cin>>n,n) One { Amemset (D,0,sizeof(d)); - - for(intI=1; i<=n;i++) theCin>>Price[i]; -Cin>>m; -Sort (price+1, price+n+1); - if(m<5) + { -cout<<m<<Endl; + } A Else at { -m-=5; - for(intI=1; i<n;i++) - for(intj=m;j>=price[i];j--) - { - inD[j]=max (d[j],d[j-price[i]]+price[i]); - } to +cout<<m+5-d[m]-price[n]<<Endl; - } the } * return 0; $}
Hangzhou Electric oj2546~0-1 knapsack problem