Codeforce 588A - Duff and Meat (贪心)

Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.

There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1,?...,?an and p1,?...,?pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.

Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.

Input

The first line of input contains integer n (1?≤?n?≤?105), the number of days.

In the next n lines, i-th line contains two integers ai and pi (1?≤?ai,?pi?≤?100), the amount of meat Duff needs and the cost of meat in that day.

Output

Print the minimum money needed to keep Duff happy for n days, in one line.

Examples

input

31 32 23 1

output

10

input

31 32 13 2

output

8

Note

In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.

In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.

题解:不断更新肉价的最小值

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <cstdlib>
 7 #include <iomanip>
 8 #include <cmath>
 9 #include <ctime>
10 #include <map>
11 #include <set>
12 using namespace std;
13 #define lowbit(x) (x&(-x))
14 #define max(x,y) (x>y?x:y)
15 #define min(x,y) (x<y?x:y)
16 #define MAX 100000000000000000
17 #define MOD 1000000007
18 #define pi acos(-1.0)
19 #define ei exp(1)
20 #define PI 3.141592653589793238462
21 #define INF 0x3f3f3f3f3f
22 #define mem(a) (memset(a,0,sizeof(a)))
23 typedef long long ll;
24 const int N=100005;
25 const int mod=1e9+7;
26 int a[N],b[N];
27 int main()
28 {
29     int n;
30     scanf("%d",&n);
31     for(int i=0;i<n;i++){
32         scanf("%d %d",&a[i],&b[i]);
33     }
34     ll s=0;
35     s+=a[0]*b[0];
36     int t=b[0];
37     for(int i=1;i<n;i++){
38         if(b[i]<t) t=b[i];
39         s+=a[i]*t;
40     }
41     printf("%lld\n",s);
42     return 0;
43 }
时间: 2024-10-29 19:07:29

Codeforce 588A - Duff and Meat (贪心)的相关文章

Duff and Meat_贪心

Duff and Meat TimeLimit:1000MS  MemoryLimit:256MB 64-bit integer IO format:%I64d Problem Description Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.

Duff and Meat - CF 588A

题目大意:有一个人他有每天需要吃ai千克肉,并且当天肉的价格是pi,问N天后他至少需要花费多少钱买肉. 分析:注意一下,他是可以提前买好肉放着的. 代码如下: #include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<math.h> using namespace std; const int MAXN = 17; const int

Codeforces Round #326 (Div. 2)-Duff and Meat

题意: Duff每天要吃ai千克肉,这天肉的价格为pi(这天可以买好多好多肉),现在给你一个数值n为Duff吃肉的天数,求出用最少的钱满足Duff的条件. 思路: 只要判断相邻两天中,今天的总花费 = ai*pi 与昨天的总花费(还有加上今天要吃的肉的重量)= (ai-1 + ai)*pi-1 . 代码如下: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <fst

codeforce 448C Painting Fence (贪心)

原题地址:http://codeforces.com/problemset/problem/448/C 题意: 略 题解 略 #include<bits/stdc++.h> #define clr(x,y) memset((x),(y),sizeof(x)) using namespace std; typedef long long LL; const int maxn=5000; int A[maxn+5]; int d[maxn+5][20]; void rmq_init(int A[]

三道类似的简单贪心

这几天遇到三道相似的贪心问题. 1. 汽车加油问题(算法设计与分析基础 习题4-9) 初始时油量为n.从起点到终点之间有k个加油站,汽车油箱容量上限为n,每个加油站可无限量供应汽油. 给出n,k和相对位置(在一条直线上),求最少加油次数及对应加油记录. 贪心策略:一直走,当到不了站点 i 时,在i-1加油至容量上限n(距离超过n时无解). 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 const

Duff and Weight Lifting_贪心

Duff and Weight Lifting TimeLimit:1000MS  MemoryLimit:256MB 64-bit integer IO format:%I64d Problem Description Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of 

codeforce 985C Liebig&#39;s Barrels(贪心+思维)

Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You have m?=?n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, y

codeforce Gym 100685E Epic Fail of a Genie(贪心)

题意:给出一堆元素,求一个子集,使子集的乘积最大,如有多个,应该使子集元素个数尽量小. 题解:贪心,如果有乘积大于1的正数,那么是一定要选的,注意负数也可能凑出大于1的正数,那么将绝对值大于1的负数两两配对,如果还剩下一个,那么在判断一下,那个负数和比它小的最小负数的乘积是否大于1,如果是那么就选这两个.把所有可能凑成大于1的数选完以后,剩下的数一定比1小,那么就不选. 如果无法凑出大于1的数,那么再分类讨论一下. 挺容易写错... #include<bits/stdc++.h> using

CodeForce 508C Anya and Ghosts (贪心+模拟)

题目大意:有m个时刻,在第i时刻即wi秒的时候需要保持有r根蜡烛亮着,每根蜡烛维持的时间为t秒,点一根蜡烛需要1秒. 注意:一根蜡烛亮的时间为下一秒开始.并且一开始是可以事先准备蜡烛的. 想法:利用了优先队列,维护r根蜡烛,每次wi秒,它需要开始点蜡烛的最晚时间为wi-t,如果不够这个时间,那么在最晚结束点蜡烛的时间wi-1开始补上. 感谢阿扎夫人提供的思维题. AC代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include&