Description John 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. Dayton has 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? Input line 1th enters C and H, and then line H enters a VI. The maximum volume of available hay for the Output. Sample Input7 3//Total volume of 7, with 3 items to backpack
2
6
5The wagon holds 7 volumetric units; Three bales is offered for sale withvolumes 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
Bare 0/1 backpack, do not worry about timeouts, the data is very weak. You need to use a scrolling array to reduce the complexity of space.
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)