[Codeforces492C] Vanya and Exams

描述 Description

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 the i-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?

输入格式 InputFormat

The first line contains three integers n, r, avg (1 ≤ n ≤ 105, 1 ≤ r ≤ 109, 1 ≤ 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 ≤ r, 1 ≤ bi ≤ 106).

输出格式 OutputFormat

In the first line print the minimum number of essays.

样例输入 SampleInput

6 5 5
1 7
2 4
3 5
4 6
5 6
4 7

样例输出 SampleOutput

63


Codeforces 492C


代码 Code

每次选代价最小的加学分方法。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
const ll maxn=100005;
ll i,j,t,n,m,l,r,k,z,y,x;
struct data
{
    ll a,b;
    bool operator < (const data& temp) const
    {
        return b<temp.b;
    }
}a[maxn];
ll avg,sum,ans;
int main()
{
    scanf("%I64d%I64d%I64d",&n,&r,&avg);
    sum=ans=0;
    for (i=1;i<=n;i++) scanf("%I64d%I64d",&a[i].a,&a[i].b),sum+=a[i].a;
    m=avg*n-sum;
    sort(a+1,a+n+1);
    i=1;
    while (m>0)
    {
        t=min(m,r-a[i].a);
        ans+=t*a[i].b;
        m-=t;
        i++;
    }
    printf("%I64d\n",ans);
    return 0;
}