DescriptionJohn suffered a major loss: the cockroach ate all his hay and left a herd of hungry cows. He rode a carriage in C (1≤c≤50000), and went to the house to buy some hay. Because of the H (1≤h≤5000) bales of hay, each pack has its volume Vi (l≤vi≤c). John can only buy the whole package, how many volumes of hay will he be able to ship back?InputEnter C and h in line 1th, then enter a VI on line H.OutputThe largest volume of available hay.Sample Input7 3//Total volume of 7, with 3 items to backpack
2
6
5
The wagon holds 7 volumetric units; Three bales is offered for sale with
Volumes of 2, 6, and 5 units, respectively.
Sample Output7//maximum volume that can be backed up
HINT
Buying The smaller bales fills the wagon.
Source
Silver
Solution
Violence DP can be water I'll talk nonsense?
1#include <bits/stdc++.h>2 using namespacestd;3 inta[5005];4 BOOLf[50005];5 intMain ()6 {7 intans, C, H;8scanf"%d%d", &c, &h);9 for(inti =1; I <= H; i++)Tenscanf"%d", A +i); Onef[0] =true; A for(inti =1; I <= H; i++) - for(intj = C-a[i]; ~j; j--) - if(F[j]) F[j + a[i]] =true; the for(inti =1; I <= C; i++) - if(F[i]) ans =i; -printf"%d\n", ans); - return 0; +}
View Code
[BZOJ1606] [Usaco2008 Dec] Hay for Sale Purchase (DP)