#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int maxN = 5e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
pii a[maxN]; // sz, val
int pfs[maxN];
struct ST {
int st[maxN * 4];
ST () {
memset (st, 0, szf (st));
}
void upd (int id, int l, int r, int pos, int val) {
if (pos < l || pos > r) {
return;
}
if (l == r) {
st[id] = val;
return;
}
int mid = (l + r) >> 1;
upd (id * 2, l, mid, pos, val);
upd (id * 2 + 1, mid + 1, r, pos, val);
st[id] = max (st[id * 2], st[id * 2 + 1]);
}
int get (int id, int l, int r, int u, int v) {
if (v < l || u > r) {
return -inf;
}
if (u <= l && r <= v) {
return st[id];
}
int mid = (l + r) >> 1;
int tL = get (id * 2, l, mid, u, v);
int tR = get (id * 2 + 1, mid + 1, r, u, v);
return max (tL, tR);
}
};
ST T;
bool cmp (pii A, pii B) {
if (A.fi == B.fi) {
return A.se < B.se;
}
return A.fi < B.fi;
}
void Solve () {
int n;
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i].fi >> a[i].se;
}
sort (a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i ++) {
pfs[i] = pfs[i - 1] + a[i].se;
T.upd (1, 1, n, i, pfs[i] - a[i].fi);
}
int res = 0;
for (int i = 1; i <= n; i ++) {
maxi (res, T.get (1, 1, n, i, n) + a[i].fi - pfs[i - 1]);
}
cout << res;
return;
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
Solve ();
}
return 0;
}
// Belligerent :)
Compilation message (stderr)
art.cpp: In function 'int main()':
art.cpp:90:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
90 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
art.cpp:91:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
91 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |