a - the small Peng Yu's sweeping canteen plan Time Limit: 20000/10000ms (java/others) Memory Limit: 128000/64000kb (java/others) SubmitStatusProblem Description
The canteen in Hua-Hua village is very strange, that is, if the amount of this meal card is less than 5 yuan, the rice card can not be brushed.
That is to say, as long as this meal card amount is equal to 5 yuan, can be casually brush ~
One day, little Peng Yu looked at the meal of the dining hall, "Wow, eat well!" I want to buy all of them! ”
But little Peng Yu didn't have that much money, so he was ready to make the most of his money to buy the food!
Could you tell me the minimum amount of rice card balance in the last small Peng Yu?
Input
Multiple sets of test data (up to 100 groups)
The first row n, which indicates that there are n dishes
The second line next n numbers, A[i] means how much is the I course?
The third line, a number m, represents the rice card of the small Peng Yu, which initially has M-yuan
1<=n<=1000,1<=a[i]<=10000,1<=m<=10000
Output outputs an integer that indicates the amount of the last meal card displayed in sample Input
1100006101 2 3 2 1 1 2 3 2 150
Sample Output
-999432
Solution: 01 The use of backpacks, because 5 dollars can buy anything, so, we put the most expensive dishes alone, we only need to use (N-1) to find the price capacity for (M-5), can buy the maximum value. Finally, the price of the most expensive dish is subtracted.
1#include <stdio.h>2#include <string.h>3#include <iostream>4#include <algorithm>5 using namespacestd;6 #defineMAX 101007 intDp[max];8 intVal[max];9 intMain ()Ten { One intN,m,i,j,max; A while(SCANF ("%d", &n)! =EOF) - { - for(i=0, max=0; i<n;i++) the { -scanf"%d",&val[i]); - if(Val[i]>max) Max=val[i];/*Take maximum value*/ - } +scanf"%d",&M); - for(i=0; i<=m;i++) dp[i]=0; + if(m<5|| n==0) {printf ("%d\n", M);Continue;} A Else at { - intsign=1; - for(i=0; i<n;i++) - { - if(Sign&&val[i]==max)/*Remove the maximum value once*/ -{sign=0;Continue;} in for(j=m-5; j>=val[i];j--) - { to if(dp[j]<dp[j-val[i]]+Val[i]) + { -dp[j]=dp[j-val[i]]+Val[i]; the } * } $ }Panax Notoginsengprintf"%d\n", m-dp[m-5]-Max); - } the } + return 0; A}
View Code
A-the small Peng Yu's sweeping canteen plan