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;
template <class T>
bool chmin(T& _old, T _new) { return _old > _new && (_old = _new, true); }
template <class T>
bool chmax(T& _old, T _new) { return _old < _new && (_old = _new, true); }
constexpr int64_t INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
class binary_indexed_tree {
const size_t n;
vector<T> tree;
public:
binary_indexed_tree(size_t n) : n(n), tree(n + 1, ~INF) {}
void update(size_t i, T val) {
for (++i; i <= n; i += i & -i)
chmax(tree[i], val);
}
T query(size_t i) const {
T ret = ~INF;
for (; i; i &= i - 1)
chmax(ret, tree[i]);
return ret;
}
};
template <typename node_t>
class segtree {
// change this (1/2)
const node_t e = ~INF;
// change this (1/2)
const size_t n;
vector<node_t> tree;
public:
segtree(size_t n) : n(n), tree(n << 1, e) {}
node_t& operator[](size_t i) { return tree[n + i]; }
void build() {
for (size_t i = n; i--;) {
pull(i);
}
}
void set(size_t idx, node_t val) {
assert(0 <= idx and idx < n);
tree[idx += n] = val;
while (idx >>= 1) pull(idx);
}
node_t prod(size_t l, size_t r) const {
assert(0 <= l and l <= r and r <= n);
node_t ret = e;
for (l += n, r += n; l != r; l >>= 1, r >>= 1) {
if (l & 1) ret = op(ret, tree[l++]);
if (r & 1) ret = op(tree[--r], ret);
}
return ret;
}
node_t all_prod() const { return tree[1]; }
void clear() { fill(tree.begin(), tree.end(), e); }
private:
void pull(size_t i) {
tree[i] = op(tree[i << 1], tree[i << 1 | 1]);
}
// change this (2/2)
node_t op(node_t lhs, node_t rhs) const {
return max(lhs, rhs);
}
// change this (2/2)
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef palilo
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
int n;
cin >> n;
vector<pair<int64_t, int64_t>> a(n);
for (auto& [x, y] : a) {
cin >> x >> y;
}
sort(a.begin(), a.end(), [&](const auto& lhs, const auto& rhs) {
return lhs.first < rhs.first;
});
vector<int64_t> pref(n + 1);
for (int i = 0; i < n; ++i) {
pref[i + 1] = pref[i] + a[i].second;
}
segtree<int64_t> seg(n);
for (int i = 0; i < n; ++i) {
seg.set(i, a[i].first - pref[i]);
}
int64_t ans = ~INF;
for (int r = 1; r < n; ++r) {
chmax(ans, pref[r] - a[r - 1].first + seg.prod(0, r));
}
cout << ans;
}
# | 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... |