2015
05-23

# Greedy?

iSea is going to be CRAZY! Recently, he was assigned a lot of works to do, so many that you can’t imagine. Each task costs Ci time as least, and the worst news is, he must do this work no later than time Di!
OMG, how could it be conceivable! After simple estimation, he discovers a fact that if a work is finished after Di, says Ti, he will get a penalty Ti – Di. Though it may be impossible for him to finish every task before its deadline, he wants the maximum penalty of all the tasks to be as small as possible. He can finish those tasks at any order, and once a task begins, it can’t be interrupted. All tasks should begin at integral times, and time begins from 0.

The first line contains a single integer T, indicating the number of test cases.
Each test case includes an integer N. Then N lines following, each line contains two integers Ci and Di.

Technical Specification
1. 1 <= T <= 100
2. 1 <= N <= 100 000
3. 1 <= Ci, Di <= 1 000 000 000

The first line contains a single integer T, indicating the number of test cases.
Each test case includes an integer N. Then N lines following, each line contains two integers Ci and Di.

Technical Specification
1. 1 <= T <= 100
2. 1 <= N <= 100 000
3. 1 <= Ci, Di <= 1 000 000 000

2
2
3 4
2 2
4
3 6
2 7
4 5
3 9

Case 1: 1
Case 2: 3

here~~

#include <stdio.h>
#include <algorithm>
using namespace std;
struct TT
{
int c,d;
bool operator < (const TT& s)const
{
return d < s.d;
}
}A[100002];
int main()
{
int z,n;
__int64 t,ans;
scanf("%d",&z);
for(int k=1;k<=z;k++)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&A[i].c,&A[i].d);
sort(A,A+n);
t = ans = 0;
for(int i=0;i<n;i++)
{
t += A[i].c;
if(ans < t - A[i].d)
ans = t - A[i].d;
}
printf("Case %d: %I64d\n",k,ans);
}
return 0;
}