제출 #375378

#제출 시각아이디문제언어결과실행 시간메모리
375378Alex_tz307Art Exhibition (JOI18_art)C++17
0 / 100
1 ms364 KiB
#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

const int NMAX = 5e5 + 5;
int N, lg2[NMAX];
int64 pref_sum[NMAX], rmq_max[NMAX][20], ans;
pair<int64,int> a[NMAX];

void compute_lg2() {
    lg2[1] = 0;
    for(int i = 2; i <= N; ++i)
        lg2[i] = lg2[i >> 1] + 1;
}

void compute_rmq() {
    for(int i = 1; i <= N; ++i)
        rmq_max[i][0] = pref_sum[i] - a[i].first;
    for(int j = 1; (1 << j) <= N; ++j)
        for(int i = 1; i + (1 << j) - 1 <= N; ++i)
            rmq_max[i][j] = max(rmq_max[i][j - 1], rmq_max[i + (1 << (j - 1))][j - 1]);
}

int query_max(int l, int r) {
    int k = lg2[r - l + 1];
    int dif = (r - l + 1) - (1 << k);
    return max(rmq_max[l][k], rmq_max[l + dif][k]);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> N;
    for(int i = 1; i <= N; ++i)
        cin >> a[i].first >> a[i].second;
    sort(a + 1, a + N + 1);
    for(int i = 1; i <= N; ++i)
        pref_sum[i] = pref_sum[i - 1] + a[i].second;
    compute_lg2();
    compute_rmq();
    for(int i = 1; i <= N; ++i) {
        int64 best = a[i].first + query_max(i, N) - pref_sum[i - 1];
        ans = max(ans, best);
    }
    cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...