Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- constexpr int N = 5e5 + 5;
- int n, id[N];
- ll a[N], b[N], c[N], d[N];
- void Read()
- {
- cin >> n;
- for (int i = 1; i <= n; ++i)
- cin >> a[id[i] = i] >> b[i];
- }
- void Solve()
- {
- sort(id + 1, id + n + 1, [&](const int &x, const int &y)
- { return a[x] < a[y]; });
- ll ans = 0;
- for (int i = 1; i <= n; ++i)
- {
- c[i] = c[i - 1] + b[id[i]];
- d[i] = max(d[i - 1], a[id[i]] - c[i - 1]);
- ans = max(ans, (c[i] - a[id[i]]) + d[i]);
- }
- cout << ans;
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment