Codeforces 492C Vanya and Exams【贪心】

来源:互联网 发布:java工程师证书有用吗 编辑:程序博客网 时间:2024/05/16 12:12

C. Vanya and Exams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya wants to pass n exams and get the academic scholarship. He will get the scholarship if the average grade mark for all the exams is at least avg. The exam grade cannot exceed r. Vanya has passed the exams and got grade ai for the i-th exam. To increase the grade for thei-th exam by 1 point, Vanya must write bi essays. He can raise the exam grade multiple times.

What is the minimum number of essays that Vanya needs to write to get scholarship?

Input

The first line contains three integers nravg (1 ≤ n ≤ 1051 ≤ r ≤ 1091 ≤ avg ≤ min(r, 106)) — the number of exams, the maximum grade and the required grade point average, respectively.

Each of the following n lines contains space-separated integers ai and bi (1 ≤ ai ≤ r1 ≤ bi ≤ 106).

Output

In the first line print the minimum number of essays.

Examples
input
5 5 45 24 73 13 22 5
output
4
input
2 5 45 25 2
output
0
Note

In the first sample Vanya can write 2 essays for the 3rd exam to raise his grade by 2 points and 2 essays for the 4th exam to raise his grade by 1 point.

In the second sample, Vanya doesn't need to write any essays as his general point average already is above average.


题目大意:

一共有N个科目,对应所有科目的最高分数为r,如果我们得到的分数的平均值大于avg的话,就算通过了考试。

我们的目的就是通过考试,现在我们如果想要对应将一个科目的分数+1,那么对应就需要花费这门科目的bi值。

问最少需要消耗的bi值,使得我们能够通过考试、


思路:


1、首先判定我们是否已经通过了考试,如果已经通过了,输出0,否则进行贪心步骤。


2、我们将其按照bi值从小到大排序,那么我们对应贪心:

将花费小的科目的分数尽量增大即可。

那么我们对应增大花费小的科目的分数,直到能够通过考试即可。


Ac代码:


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll __int64struct node{    ll ai,bi;}a[150000];int cmp(node a,node b){    return a.bi<b.bi;}int main(){    ll n,r,avg;    while(~scanf("%I64d%I64d%I64d",&n,&r,&avg))    {        ll mubiao=avg*n;        ll sum=0;        for(int i=0;i<n;i++)        {            scanf("%I64d%I64d",&a[i].ai,&a[i].bi);            sum+=a[i].ai;        }        ll output=0;        sort(a,a+n,cmp);        for(int i=0;i<n;i++)        {            if(sum>=mubiao)break;            if(a[i].ai<r)            {                if(r-a[i].ai+sum>=mubiao)                {                    output+=a[i].bi*(mubiao-sum);                    sum=mubiao;                }                else                {                    output+=a[i].bi*(r-a[i].ai);                    sum+=r-a[i].ai;                }            }        }        printf("%I64d\n",output);    }}







0 0