| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1228954 | nvt2k9 | Art Exhibition (JOI18_art) | C++20 | 0 ms | 0 KiB | 
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
int n;
pair<int,int> a[N];
int pre[N];
signed main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
  sort(a + 1, a + n + 1);
  for(int i = 1; i <= n; i++){
    pre[i]=pre[i-1]+ a[i].second;
  }
  int gm = 0, res = 0;
  // S = 
  for(int i = 1; i <= n; i++){
    amin= max(gm, a[i].first - pre[i-1]);
    res = max(res, pre[i] - a[i].first + amin);
  }
  cout << res << "\n";
}
