Submission #476340

#TimeUsernameProblemLanguageResultExecution timeMemory
476340paliloArt Exhibition (JOI18_art)C++17
0 / 100
1 ms204 KiB
#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;
    }
};

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;
    }
    binary_indexed_tree<int64_t> bit(n);
    for (int i = 0; i < n; ++i) {
        bit.update(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 + bit.query(r));
    }
    cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...