#include <bits/stdc++.h>
using namespace std;
// author: Movlan
#define ll long long
#define int long long
#define br "\n"
#define sp " "
#define pb push_back
#define pf push_front
typedef pair<int,int> pii;
#define vi vector<int>;
#define vpii vector<pair<int,int>>
#define all(x) x.begin(),x.end()
#define sync ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
struct edge{
//int u;
int v;
ll w;bool operator<(edge const& other){return w<other.w;}
};
const int INF=1e9+7,MAXN=3007;
bool isprime(ll n){
for(ll i=3;i*i<=n;i+=2){
if(n%i==0)return false;
}
return true;
}
void solve(){
int n;
cin>>n;
pii arr[n];
for(int i=0;i<n;i++){
cin>>arr[i].first>>arr[i].second;
}
sort(arr,arr+n);
int mx,mn;
ll tot=arr[0].second;
mx=mn=arr[0].first;
ll res=-INF;
for(int i=1;i<n;i++){
if(((tot+arr[i].second)-(mx-mn)<arr[i].second)){
tot=arr[i].second;
mx=mn=arr[i].first;
}
else{
mx=max(mx,arr[i].first);
mn=min(mn,arr[i].first);
tot+=arr[i].second;
}
res=max(res,tot-(mx-mn));
}
cout<<res;
}
signed main(){
sync;
int t=1;
//cin>>t;
while(t--){
solve();
}
}
/**
Get up and work hard.
There are only a few days left until the final round.
You need to win a medal for everything:
for the days you cried,
for the days you couldn't afford to attend an event,
for the days when the one you loved the most left you,
and for the days when you were at your lowest point with no friends.
Show them what a "cheater" can achieve.
Show them what a true ghost can do.
You've done this many times before.
One more time, and you're good to go.
O N L Y T H E D E A D S E E
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |