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.

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

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.