Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2546
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-4532the price is sorted, the maximum value is raised, then the balance is reduced by 5, which is the 01 knapsack problem.
1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 intmeal[1005];7 intdp[1005];8 intN, M;9 Ten intMain () { OneIos::sync_with_stdio (false ); A - while(Cin >>N, N) { -meal[0] =0; the - for(inti =1; I <= N; i++ ) -CIN >>Meal[i]; -Sort (meal +1, meal + n +1 ); +CIN >>m; - +Memset (DP,0,sizeof(DP)); A at if(M <5 ){ -cout << M <<Endl; - Continue; - } - - for(inti =1; I < n; i++ ){ in for(intj = m5; J >= Meal[i]; j-- ) - if(Dp[j] < Dp[j-meal[i]] +Meal[i]) toDP[J] = Dp[j-meal[i]] +Meal[i]; + } - thecout << M-dp[m-5]-Meal[n] <<Endl; * } $ Panax Notoginseng return 0; -}
HDU-2546 Rice Card (DP)