제출 #670404

#제출 시각아이디문제언어결과실행 시간메모리
670404illyakrArt Exhibition (JOI18_art)C++14
0 / 100
0 ms340 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;

int n;
pair<int, int> t[501010];
int L[501010];
int rmq[50][501010];

int gt(int l, int r) {
    int x = L[r - l + 1];
    return min(rmq[x][l], rmq[x][r - (1 << x) + 1]);
}
int ans = -1010101010101010101;
int32_t main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> t[i].first >> t[i].second;
    sort(t + 1, t + 1 + n, [](pair<int, int> q, pair<int, int> w) {
         return q.first > w.first;
    });
//    cout << endl;
//    for (int i = 1; i <= n; i++)
//        cout << t[i].first << " " << t[i].second << endl;
//        cout << endl;

    L[1] = 0;
    for (int i = 2; i <= n; i++)
        L[i] = L[i / 2] + 1;
    for (int i = 1; i <= n; i++)
        rmq[0][i] = t[i].first;
    for (int i = 1; i <= L[n]; i++) {
        for (int j = 1; j + (1 << i) - 1 <= n; j++) {
            rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
        }
    }

    int sum = 0;
    for (int i = 1; i <= n; i++)
        sum += t[i].second;
    for (int i = 1; i <= n; i++) {
//        cout << i << " --  " << sum << " " << t[i].first << " " << gt(i, n) << endl;
        ans = max(ans, sum - t[i].first + gt(i + 1, n));
        sum -= t[i].second;
    }
    cout << ans;
    return 0;
}

/**
3
2 3
11 2
4 5
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...