Vanya and Exams
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 biessays. 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 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).
Output
In the first line print the minimum number of essays.
Examples
5 5 4
5 2
4 7
3 1
3 2
2 5
4
2 5 4
5 2
5 2
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.
sol:挺明显的贪心,把便宜的都尽量升级的更高,一定是最优的,做到平均数够了为止
#include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read() { ll s=0; bool f=0; char ch=' '; while(!isdigit(ch)) { f|=(ch=='-'); ch=getchar(); } while(isdigit(ch)) { s=(s<<3)+(s<<1)+(ch^48); ch=getchar(); } return (f)?(-s):(s); } #define R(x) x=read() inline void write(ll x) { if(x<0) { putchar('-'); x=-x; } if(x<10) { putchar(x+'0'); return; } write(x/10); putchar((x%10)+'0'); return; } #define W(x) write(x),putchar(' ') #define Wl(x) write(x),putchar(' ') const int N=100005; ll n,Up,Yaoq; struct Fens { ll a,b; }Score[N]; inline bool cmp_b(Fens p,Fens q) { return p.b<q.b; } int main() { int i; ll Sum=0,ans=0; R(n); R(Up); R(Yaoq); for(i=1;i<=n;i++) { R(Score[i].a); R(Score[i].b); Sum+=1ll*Score[i].a; } sort(Score+1,Score+n+1,cmp_b); i=0; while(Sum<1ll*n*Yaoq) { ll oo=min(1ll*(Up-Score[++i].a),1ll*(1ll*n*Yaoq-Sum)); Sum+=1ll*oo; ans+=1ll*oo*Score[i].b; } Wl(ans); return 0; } /* input 5 5 4 5 2 4 7 3 1 3 2 2 5 output 4 input 2 5 4 5 2 5 2 output 0 */