Rokua P2925 [Usaco08dec] hay for sale hay for Sale

Source: Internet
Author: User

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:

    • Line 1:two space-separated Integers:c and H

    • Lines 2..h+1:each Line describes the volume of a single bale:v_i

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.