"Problem description"
FJ raised n cows, and they numbered 1 to N in turn. What FJ didn't know was that all his cows ' dreams were to escape from the farm and go to the circus show. But the cows ' doors soon found that their clumsy hooves could not stand on a wire or a rocking swing (they tried to shoot themselves in a cannon, but the result was tragic). In the end, they decided to practice one of the simplest acrobatics: putting all the cows together, for example, the first cow standing on the second cow, and the second cow standing on the third cow ... the bottom is the nth cow (the cow is really not creative).
Each cow has its own weight and strength, the ox with the number i is the weight of WI, the power is SI. When a cow is standing on the other side of the cow it will be squashed to a certain extent, we may as well flatten it is called the flattening index. For any cow, its flattening index is equal to the total weight of all the cows on top of it (and of course excluding itself) minus its strength. After the cows were stacked together in a certain order, their total flattening index was the flattened index of the cow that was squashed the most. Your task is to help the cows find a stacking order that minimizes the total flattening index.
"Input Format"
Line 1th: A separate positive integer n.
2nd to n+1: line i+1 gives the weight of the cows numbered I in power Wi and Si, separated by a space.
"Output Format"
An integer that represents the minimum value of the total flattening index of the cow door.
"Input Sample"
3
3
2 5
3 3
"Output Example"
2
"Sample Interpretation"
The cow with a weight of 10 was placed at the bottom, and her flattening index was 2+3-3=2. The flattening index of the other 2 cows is less than this value.
"Data Range"
1<=n<=50,000
1<=wi<=10,000
1<=si<=1,000,000,000
Idea: Sort by weight plus strength value, large at the bottom, and then traverse I to subtract the weight of all cows above the first cow and find the maximum value.
/* Name:cow_acrobats.cpp
copyright:twitter & Instagram @stevebieberjr
Author: @stevebieberjr
DATE:26/07/16 10:24
* *
#include <cstdio>
#include <algorithm>
#include <cstring>
#define INF 1000000000
using namespace std;
int n,tot=0;
struct cow
{
int w,s;
} A[50005];
BOOL CMP (Cow A,cow b)
{
return a.w+a.s>b.w+b.s;
}
void init ()
{
scanf ("%d", &n);
for (int i=1;i<=n;i++)
{
scanf ("%d%d", &A[I].W,&A[I].S);
TOT+=A[I].W;
}
}
void Solve ()
{
init ();
Sort (a+1,a+1+n,cmp);
int ans=-inf;
for (int i=1;i<=n;i++)
{
tot-=a[i].w;
Ans=max (ANS,TOT-A[I].S);
}
printf ("%d\n", ans);
}
int main ()
{
freopen ("cow_acrobats.in", "R", stdin);
Freopen ("Cow_acrobats.out", "w", stdout);
Solve ();
return 0;
}