1.3.1 Mixing Milk

Source: Internet
Author: User

The Merry Milk makers company buys Milk from farmers, packages it to attractive 1-and 2-unit bottles, and then sells th At milk to grocery stores so we can each start we have with delicious cereal and milk.

Since milk packaging is such a difficult business in which to make money, it's important to keep the costs as low as poss Ible. Help Merry Milk makers purchase the Farmers ' Milk in the cheapest possible manner. The MMM company has a extraordinarily talented marketing department and knows precisely how much milk they need to their customers.

The company have contracts with several farmers from whom they could purchase milk, and each farmer have a (potentially) Diffe Rent price @ which they sell milk to the packing plant. Of course, a herd of cows can only produce so much milk each day, so the farmers already know how much milk they would have Available.

Each day, the Merry Milk makers can purchase an integer number of units of Milk from each farmer, a number that's always less than or equal to the farmer ' s limit (and might is the entire production from this farmer, none of the production, or any Integer in between).

Given:

    • The Merry Milk makers ' daily requirement of Milk
    • The cost per unit of milk from each farmer
    • The amount of milk available from each farmer

Calculate the minimum amount of money, Merry Milk makers must spend to meet their daily need for Milk.

Note:the total milk produced per day by the farmers would always being sufficient to meet the demands of the Merry milk Maker s even if the prices is high.

Program Name:milk

INPUT FORMAT

line 1:

two integers, N and m. 

the First value, N, (0 <= N <= 2,000,000) is the amount of milk that Merry milk makers wants per day. 

the second, M, (0 <= M <= 5,000) is the number of farmers that they may buy from. 

Lines 2 through M+1:

The next M lines each contain the integers:pI and Ai.

PI (0 <= Pi <=) is the price of cents that farmer I charges.

AI (0 <= Ai <= 2,000,000) is the amount of milk that farmer I can sell to Merry milk makers per day.

SAMPLE INPUT (file milk.in)

100 5

5 20

9 40

3 10

8 80

6 30

INPUT explanation

5 --MMM wants units of milk from 5 farmers

5 --Farmer 1 says, "I can sell units at 5 cents per unit"

9 etc.

3 --Farmer 3 says, "I can sell units at 3 cents per unit"

8 etc.

6 --Farmer 5 says, "I can sell units at 6 cents per unit"

OUTPUT FORMAT

A single, with a single integer, that's the minimum cost, Merry Milk makers Msut pay for one day ' s Milk.

SAMPLE OUTPUT (file milk.out)

630

OUTPUT explanation

Here's how the MMM company spent only 630 cents to purchase units of milk:

price

per unit

Units

< Span class= "S1" > available

Units

bought

price *

# units

total cost

Notes

5

20

20

5*20

100

9

40

0

Bought no milk from farmer 2

3

10

10

3*10

30

8

80

40

8*40

320

did not buy all units!

6

30

6*30

180

Total

180

100

630

Cheapest Total Cost

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 structQ7 {8     Long LongP,a;9}v[5005];Ten BOOLCMP (q x,q y) One { A     returnx.p<Y.P; - } - intMain () the { -Freopen ("milk.in","R", stdin); -Freopen ("Milk.out","W", stdout); -     Long Longn,m; +Cin>>n>>m; -      for(intI=0; i<m;i++){ +Cin>>v[i].p>>v[i].a; A     } atSort (v,v+m,cmp); -     Long Longsum=0; -     Long LongPr=0; -      for(Long LongI=0; i<m&&sum<n;i++){ -         if(sum+v[i].a<N) { -sum+=v[i].a; inpr+=v[i].p*v[i].a; -         } to         Else { +pr+=v[i].p* (nsum); -sum=N; the         }  *     } $cout<<pr<<Endl;Panax Notoginseng     return 0; -}

1.3.1 Mixing Milk

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.