USACO Training – Mixing Milk

/*
ID: fourck1
LANG: C
TASK: milk
*/

#define __DEBUG__
#define MAXPRICE 1000

#include <stdio.h>

int main()
{
	int amount, line, i, money, sellamount;
	int price[MAXPRICE+1];
	int final=0;
#ifndef __DEBUG__
	FILE *fin;
	FILE *fout;

	fin = fopen("milk.in","r");
	fout = fopen("milk.out","w");
	fscanf(fin,"%d %d",&amount,&line);
#else
	printf("Input amount & line : ");
	scanf("%d %d",&amount,&line);
#endif

	for(i=0;i<=MAXPRICE;i++)
		price[i]=0;

	for(i=0;i

	{
#ifndef __DEBUG__
		fscanf(fin,"%d %d",&money,&sellamount);
#else
		printf("Input money & amount : ");
		scanf("%d %d",&money,&sellamount);
#endif
		price[money] += sellamount;
	}

	for(i=0;i<=MAXPRICE;i++)
	{
		if(amount

		{
			final += i*amount;
			amount = 0;
		}
		else
		{
			final += i*price[i];
			amount -= price[i];
		}

		if(amount == 0)
			break;
	}
#ifndef __DEBUG__
	fprintf(fout,"%d\n",final);
	fclose(fin);
	fclose(fout);
#else
	printf("%d\n",final);
#endif
	return 0;
}

구입할 우유의 양과 구입할 수 있는 목장의 수를 입력 받은 뒤에, 각 목장에서 판매할 수 있는 우유의 양과 가격을 입력받는다.
단위당 우유의 가격이 0~1000으로 한정되어 있는 것에 착안하여 1001짜리 배열을 생성한 뒤에 0으로 초기화 하고, 각 가격별로 구입할 수 있는 양을 더해서 0부터 시작해서 1000까지 구입할 양만큼만 값을 계산하도록 했다.

Leave a Reply