题意:
每个积木有a,b两个值,求一个排列方法,使得∑(ai) - bj 最大值最小(i<j)。
方法:
从下往上想,如果第i个积木确定了的话那么前i-1的积木的排列就无所谓了。那么求SUM所有的A值,对于新放上去的一个积木,Val值为SUM - A - B。于是就按照 - A - B 升序排列(从下往上),对于 - A - B 相等的情况,按照 A值从大到小(越大的放下面)排列。
从最后一个枚举积木,求Val最大值即可
/*
* =====================================================================================
*
* Author: KissBuaa.DS(AC)
* Company: BUAA-ACMICPC-Group
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <stdbool.h>
#include <math.h>
#define LL long long
#define CLR(x) memset(x,0,sizeof(x))
#define typec double
#define sqr(x) ((x)*(x))
#define abs(x) ((x)<0?(-(x)):(x))
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-(x)))
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define inf 100000000
//For C++
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <list>
#include <iostream>
using namespace std;
const double eps=1e-10;
int dblcmp(typec d) {
if (fabs(d)<eps)
return 0;
return (d>0)?1:-1;
}
int n,m,T,_,__;
const int N = 200000;
LL sum;
struct Meng{
LL pre ,nex , deta;
void input(){
scanf("%I64d%I64d",&pre,&nex);
deta = - pre - nex;
}
bool operator < (const Meng & A) const{
if (deta == A.deta) return pre>A.pre;
return deta<A.deta;
}
}p[N];
void solve(){
sum = 0;
for (int i = 0 ; i < n ;++i){
p[i].input();
sum += p[i].pre;
}
sort(p , p + n);
LL ans = 0;
for (int i = 0 ; i < n ; ++i){
ans = max<LL>(ans , sum + p[i].deta );
sum -= p[i].pre;
}
printf("%I64d\n",ans);
}
int main(){
while (~scanf("%d",&n)) solve();
}