贪心,按照截止时间排序,输出时候长整型应该用%I64d.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <memory.h>
using namespace std;
const int maxn=100010;
struct Job{
int C,D;
bool operator<(const Job &rhs)const{
return D<rhs.D;
}
}jobs[maxn];
int n;
int main(){
int t,cas=1;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
for (int i=0;i<n;++i){
scanf("%d%d",&jobs[i].C,&jobs[i].D);
}
sort(jobs,jobs+n);
long long curT=0,ans=0;
for (int i=0;i<n;++i)
{
curT+=jobs[i].C;
if(curT-jobs[i].D>ans){
ans=curT-jobs[i].D;
}
}
printf("Case %d: %I64d\n",cas++,ans);
}
}