Submission #578415

#TimeUsernameProblemLanguageResultExecution timeMemory
578415AlexandruabcdePotatoes and fertilizers (LMIO19_bulves)C++14
100 / 100
146 ms15176 KiB
#include <bits/stdc++.h>

using namespace std;

constexpr int NMAX = 5e5 + 5;
typedef long long LL;

struct SlopeTrick {
    LL a, b;

    priority_queue <LL> *H;

    SlopeTrick () {
        a = b = 0;

        H = new priority_queue<LL>;
    }

    void operator += (SlopeTrick other) {
        a += other.a;
        b += other.b;

        if (H->size() > other.H->size())
            swap(H, other.H);

        while (!other.H->empty()) {
            H->push(other.H->top());
            other.H->pop();
        }
    }

    void Evaluate () {
        while (a > 0) {
            -- a;
            b += H->top();
            H->pop();
        }
    }
};

int N;
LL D[NMAX];

void Read () {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N;

    for (int i = 1; i <= N; ++ i ) {
        LL A, B;
        cin >> A >> B;

        D[i] = D[i-1] + A - B;
    }
}

void Solve () {
    SlopeTrick ans;
    for (int i = 1; i < N; ++ i ) {
        ans.a ++;
        if (D[i] > D[N]) {
            ans.b += (D[i] - D[N]);
            D[i] = D[N];
        }
        ans.b += (-D[i]);
        if (D[i] < 0) D[i] = 0;

        ans.H->push(D[i]);
        ans.H->push(D[i]);

        ans.Evaluate();
    }

    cout << ans.b << '\n';
}

int main () {
    Read();
    Solve();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...