#P1208. [USACO1.3] Mixing Milk

[USACO1.3] Mixing Milk

题目描述

The Merry Milk Makers company buys milk from farmers, packages it into attractive 1- and 2-Unit bottles, and then sells that milk to grocery stores so we can each start our day with delicious cereal and milk.

Since milk packaging is such a difficult business in which to make money, it is important to keep the costs as low as possible. Help Merry Milk Makers purchase the farmers' milk in the cheapest possible manner. The MMM company has an extraordinarily talented marketing department and knows precisely how much milk they need each day to package for their customers.

The company has contracts with several farmers from whom they may purchase milk, and each farmer has a different price at which they sell milk to the packing plant. A herd of cows can only produce so much milk each day, so the farmers already know how much milk they will have available.

Each day, Merry Milk Makers can purchase an integer number of units of milk from each farmer, a number that is always less than or equal to the farmer's limit. This might be the entire production from that farmer, none of the production, or any integer in between.

Given:

  • the Merry Milk Makers' daily requirement of milk,
  • the cost per unit for milk from each farmer,
  • the amount of milk available from each farmer,

Calculate the minimum amount of money that Merry Milk Makers must spend to meet their daily need for milk.

Note: The total milk produced per day by the farmers will always be sufficient to meet the demands of the Merry Milk Makers even if the prices are high.

输入格式

  • Line 1: Two integers, NN and MM.

The first value, NN (0N20000000 \le N \le 2000000) is the amount of milk that Merry Milk Makers wants per day.

The second, MM (0M50000 \le M \le 5000) is the number of farmers that they may buy from.

  • Lines 2 through M+1M+1: The next MM lines each contain two integers: PiP_i and AiA_i.

PiP_i (0Pi10000 \le P_i \le 1000) is price in cents that farmer ii charges.

AiA_i (0Ai20000000 \le A_i \le 2000000) is the amount of milk that farmer ii can sell to Merry Milk Makers per day.

输出格式

A single line with a single integer that is the minimum cost that Merry Milk Makers must pay for one day's milk.

100 5
5 20
9 40
3 10
8 80
6 30

630

提示

Input Explanation

100 5 means MMM wants 100100 units of milk from 55 farmers.

5 20 means farmer 1 says, "I can sell you 2020 units at 55 cents per unit".

9 40 and 3 10 are similar.

8 80 means farmer 4 says, "I can sell you 8080 units at 88 cents per unit".

6 30 means farmer 5 says, "I can sell you 3030 units at 66 cents per unit".

Output Explanation

Here is how the MMM company spent only 630630 cents to purchase 100100 units of milk:

Price per unit Units available Units bought Price ×\times units Total cost Notes
55 2020 5205*20 100100
99 4040 00 Bought no milk from farmer 22
33 1010 3103*10 3030
88 8080 4040 8408*40 320320 Did not buy all 8080 units
66 3030 6306*30 180180
Total 180180 100100 630630 Cheapest total cost

USACO Training Section 1.3.