Mixing Milk
The Merry Milk Makers company has several farmers from which they may buy milk, and each one has a (potentially) different price at which they sell to the milk packing plant. Moreover, as a cow can only produce so much milk a day, the farmers only have so much milk to sell per day. Each day, Merry Milk Makers can purchase an integral amount of milk from each farmer, less than or equal to the farmer's limit.
Given the Merry Milk Makers' daily requirement of milk, along with the cost per gallon and amount of available milk for each farmer, calculate the minimum amount of money that it takes to fulfill the Merry Milk Makers' requirements.
Note: The total milk produced per day by the farmers will be sufficient to meet the demands of the Merry Milk Makers.
PROGRAM NAME: milk
INPUT FORMAT
Line 1: |
Two integers, N and M. |
Lines 2 through M+1: |
The next M lines each contain two integers, Pi and
Ai. |
SAMPLE INPUT (file milk.in)
5 20
9 40
OUTPUT FORMAT
A single line with a single integer that is the minimum price that Merry Milk Makers can get their milk at for one day.
SAMPLE OUTPUT (file milk.out)
630
/*
ID: sHinYU
PROB: milk
LANG: C
*/
#include <stdio.h>
#include <string.h>
int N, M, price[1002];
int main()
{
memset(price, 0, sizeof(price));
int i, p, a, count, ans;
freopen("milk.in", "r", stdin);
scanf("%d %d", &N, &M);
for(i = 0; i < M; i++)
{
scanf("%d %d", &p, &a);
price[p] += a;
}
fclose(stdin);
freopen("milk.out", "w", stdout);
for(i = 0, ans = 0; i < 1001 && N > 0; i++)
{
if(N - price[i] < 0)
ans += N * i;
else
ans += i * price[i];
N -= price[i];
}
printf("%d\n", ans);
fclose(stdout);
return 0;
}