Title Description
Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his hay inventory, leaving him With nothing to feed the cows. He hitched up his wagon with capacity C (1 <= C <= 50,000) cubic units and sauntered over to Farmer Don's to get som E Hay before the cows miss a meal.
Farmer Don had a wide variety of H (1 <= H <= 5,000) hay bales for sale, each with its own volume (1 <= v_i <= C). Bales of hay, you know, is somewhat flexible and can be jammed to the oddest of spaces in a wagon.
FJ carefully evaluates the volumes so, he can figure out the largest amount of the hay he can purchase for his cows.
Given the volume constraint and a list of bales to buy, what is the greatest volume of hay FJ can purchase? He can ' t purchase partial bales, of course. Each of the input line (after the first) lists a single Bale FJ can buy.
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. There are 1≤h≤5000 bales of hay, and each pack has its volume Vi (l≤vi≤c). John can only buy the whole package,
How many volumes of hay can he transport back?
Input/output format
Input format:
Output format:
- Line 1: A single integer which are the greatest volume of hay FJ can purchase given the list of bales for sale and Constrai Nts.
Input and Output Sample input example # #:
Sample # # of output:
Description
The wagon holds 7 volumetric units; Three bales is offered for sale with volumes of 2, 6, and 5 units, respectively.
Buying The smaller bales fills the wagon.
Knapsack problem But the last one's a little bit confusing.
#include <iostream>#include<cstring>#include<cstdio>using namespacestd;inti,j,h,c,v[5001],f[50001];intMain () {Ios::sync_with_stdio (false); CIN>>c>>h; for(i=0; iV[i]; for(i=0; ii) { for(j=c;j>=v[i];--j)if(F[j-v[i]]+v[i]>f[j]) f[j]=f[j-v[i]]+v[i];//'s not going to write like this. tle} cout<<f[c];}
Rokua P2925 [Usaco08dec] hay for sale hay for Sale