//#pragma GCC target ("avx2")
//#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pb push_back
const int N = 1e6, NN=26;
const int mod=1e9+7;
//int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); }
//int lcm(int a, int b) { return a / gcd(a, b) * b; }
//int binpow(int a,int b){if(!b)return 1; if(b&1)return a*binpow(a,b-1)%mod; int x=binpow(a,b/2); return x*x%mod;}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
pair < int ,int > a[n+1];
for(int i=1;i<=n;i++) cin >> a[i].F >> a[i].S;
sort(a+1, a+n+1);
int ans=0, sum=0;
for(int i=1;i<=n;i++){ sum+=a[i].S; ans=max(ans, a[i].S);}
int l=1, r=n;
ans=max(ans, sum-(a[n].F-a[1].F));
while(l<=r){
if(l==r){
ans=max(ans, sum);
break;
}
if((sum-a[r].S)-(a[r-1].F-a[l].F)>=(sum-a[l].S)-(a[r].F-a[l+1].F)){
ans=max(ans, (sum-a[r].S)-(a[r-1].F-a[l].F));
sum-=a[r].S;
r--;
}
else{
ans=max(ans, (sum-a[l].S)-(a[r].F-a[l+1].F));
sum-=a[l].S;
l++;
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |