您的位置:首页 > 其它

codeforces/problem/175/C 贪心

2014-02-06 15:17 204 查看
C. Geometry Horse

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Vasya plays the Geometry Horse.

The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.

There are n types of geometric figures. The number of figures of type ki and
figure cost ci is
known for each figure type. A player gets ci·fpoints
for destroying one figure of type i, where f is
the current factor. The factor value can be an integer number from 1 to t + 1,
inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to i + 1 after destruction of pi (1 ≤ i ≤ t) figures,
so the (pi + 1)-th
figure to be destroyed is considered with factor equal to i + 1.

Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him.

Input

The first line contains the only integer number n (1 ≤ n ≤ 100) —
the number of figure types.

Each of the following n lines contains two integer numbers ki and ci (1 ≤ ki ≤ 109, 0 ≤ ci ≤ 1000),
separated with space — the number of figures of the i-th type and the cost of one i-type
figure, correspondingly.

The next line contains the only integer number t (1 ≤ t ≤ 100) —
the number that describe the factor's changes.

The next line contains t integer numbers pi (1 ≤ p1 < p2 < ... < pt ≤ 1012),
separated with spaces.

Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams
or the%I64d specificator.

Output

Print the only number — the maximum number of points Vasya can get.

Sample test(s)

input
1
5 10
2
3 6


output
70


input
2
3 8
5 10
1
20


output
74


Note

In the first example Vasya destroys three figures first and gets 3·1·10 = 30 points. Then the factor will become equal to 2 and
after destroying the last two figures Vasya will get 2·2·10 = 40 points. As a result Vasya will get 70 points.

In the second example all 8 figures will be destroyed with factor 1,
so Vasya will get (3·8 + 5·10)·1 = 74 points.

题意;给了n中物品,每种物品数量是ki,价值ci, 摧毁一件东西的收获值是 Ki* Ci * F;

有T个分段点。0-p1 中的东西f=1,摧毁够 pi 件后,f=f+1;

问最大的 收获值;

贪心的想法,ci值越大,留到越后处理,收获值就会越大。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int c,d;
void init()
{
scanf("%d%d",&c,&d);
}
}a[110];
long long p[110];
bool cmp(node a,node b)
{
return a.d<b.d;
}
int main()
{
//  freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
a[i].init();
sort(a,a+n,cmp);
int t;
scanf("%d",&t);

for(int i=1;i<=t;i++) cin>>p[i];
for(int i=t;i>1;i--) p[i]-=p[i-1];
long long ans=0;
int st=0;
for(int i=1;i<=t;i++)
{
long long cur=0;
while(p[i]>0&&st<n)
{
if(p[i]>a[st].c){
cur+=1ll*a[st].c*a[st].d;
p[i]-=a[st].c;
st++;
}
else{
cur+=p[i]*a[st].d;
a[st].c-=p[i];
break;
}

}
ans+=cur*i;
}
long long cur=0;
for(;st<n;st++) cur+=1ll*a[st].c*a[st].d;
ans+=cur*(++t);
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces