codeforce 492C. Vanya and Exams

题目:http://codeforces.com/problemset/problem/492/C

还是数据和问题,注意会超过int,哎,以后常常用long long吧,真是~

#include <iostream>
#include<cstdio> 
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=1e5+5;
struct obj{
	int a,b;
}sub[maxn];
bool cmp(obj p1,obj p2){
	return p1.b<p2.b;
}
int main(int argc, char *argv[]) {
	//freopen("cin.txt","r",stdin);
	LL n,r,avg,sum,i;
	while(cin>>n>>r>>avg){
		sum=0;
		for(i=0;i<n;i++){
			scanf("%d%d",&sub[i].a,&sub[i].b);
			sum+=sub[i].a;
		}
		avg=avg*n;
		sort(sub,sub+n,cmp);
		i=0;
		LL tol=0;
		while(sum<avg){
			LL res=min(avg-sum,r-sub[i].a);
			sum+=res;
			tol+=res*sub[i].b;
			i++;
		}
		printf("%lld\n",tol);
	}
	return 0;
}


你可能感兴趣的:(codeforce)