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;
using ll = long long;
struct segtree {
vector<ll> tree, lazy;
int n;
segtree(int _n) : n(_n) {
tree = vector<ll>(2 * n, 0);
lazy = vector<ll>(2 * n, 0);
}
void push(int x, int l, int r) {
tree[x] += lazy[x];
if (l != r) {
int mid = (r + l) / 2;
int z = x + 2 * (mid - l + 1);
lazy[x + 1] += lazy[x];
lazy[z] += lazy[x];
}
lazy[x] = 0;
}
void pull(int x, int L, int R) {
int mid = (L + R) / 2;
int z = x + 2 * (mid - L + 1);
tree[x] = max(tree[x + 1], tree[z]);
}
void upd(int l, int r, ll v, int L, int R, int x) {
push(x, L, R);
if (r < L || R < l) return;
if (l <= L && R <= r) {
lazy[x] = v;
push(x, L, R);
return;
}
int mid = (L + R) / 2;
int z = x + 2 * (mid - L + 1);
upd(l, r, v, L, mid, x + 1);
upd(l, r, v, mid + 1, R, z);
pull(x, L, R);
}
void upd(int l, int r, ll v) { upd(l, r, v, 0, n - 1, 0); }
ll get(int l, int r, int L, int R, int x) {
push(x, L, R);
if (r < L || R < l) return -9e18;
if (l <= L && R <= r) return tree[x];
int mid = (L + R) / 2;
int z = x + 2 * (mid - L + 1);
return max(get(l, r, L, mid, x + 1), get(l, r, mid + 1, R, z));
}
ll get(int l, int r) { return get(l, r, 0, n - 1, 0); }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<pair<ll, ll>> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
}
sort(a.begin(), a.end());
segtree st(n);
ll S = 0;
for (int i = 0; i < n; ++i) {
S += a[i].second;
st.upd(i, i, S - (a[i].first - a[0].first));
}
ll ans = st.get(0, n - 1);
for (int i = 0; i < n - 1; ++i) {
st.upd(i + 1, n - 1, a[i + 1].first - a[i].first - a[i].second);
ans = max(ans, st.get(i + 1, n - 1));
}
cout << ans << "\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... |