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.
01 backpack. Constant looks big, but it's not a T.
1 /*by Silvern*/2#include <algorithm>3#include <iostream>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <vector>8 using namespacestd;9 intRead () {Ten intx=0, f=1;CharCh=GetChar (); One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } the intf[50010]; - intn,c,v[5010]; - intMain () { -C=read (); n=read (); + inti,j; - for(i=1; i<=n;i++) v[i]=read (); +f[0]=1; A for(i=1; i<=n;i++){ at for(j=c;j>=v[i];j--){ -f[j]|=f[j-V[i]]; - } - } - for(j=c;j>=0; j--){ - if(F[j]) { inprintf"%d\n", j); - Break; to } + } - return 0; the}
Rokua P2925 [Usaco08dec] hay for sale hay for Sale