This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    pair<long long, long long> refarr[n];
    for(int i=0; i<n; i++){
        cin >> refarr[i].first >> refarr[i].second;
    }
    sort(refarr, refarr+n);
    for(int i=1; i<n; i++){
        refarr[i].second+=refarr[i-1].second;
    }
    pair<long long, long long> arr[n];
    arr[0].first=refarr[0].first;
    arr[0].second=refarr[0].second-refarr[0].first;
    for(int i=1; i<n; i++){
        arr[i].first=refarr[i].first-refarr[i-1].second;
        arr[i].second=refarr[i].second-refarr[i].first;
    }
    long long maxfirst = arr[0].first;
    long long finans = maxfirst+arr[0].second;
    for(int i=1; i<n; i++){
        maxfirst=max(maxfirst, arr[i].first);
        finans=max(finans, arr[i].second+maxfirst);
    }
    cout << max((long long) 0, finans) << '\n';
    return 0;
}
| # | 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... |