首页 > 代码库 > poj Yogurt factory (贪心水题)

poj Yogurt factory (贪心水题)

# include <stdio.h>
# include <string.h>
# include <algorithm>
using namespace std;
int main()
{
	int i,n,s;
	__int64 sum ;
	int cost[10010],num[10010];
	while(~scanf("%d%d",&n,&s))
	{
		for(i=0;i<n;i++)
			scanf("%d%d",&cost[i],&num[i]);
	     for(i=0;i<n;i++)
		 {
			 if(i==0)
				 sum=cost[i]*num[i];
			 else
			 {
				 if(cost[i]>cost[i-1]+s)
				 {
					 sum+=(cost[i-1]+s)*num[i];
					 cost[i]=cost[i-1]+s;//更新最省价格
				 }
				 else
				 {
					 sum+=cost[i]*num[i];
				 }
			 }
		 }
		 printf("%I64d\n",sum);
	}
	return 0;
}

poj Yogurt factory (贪心水题)