Submission #578406

#TimeUsernameProblemLanguageResultExecution timeMemory
578406AlexandruabcdePotatoes and fertilizers (LMIO19_bulves)C++14
0 / 100
1 ms212 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>;
    }

    SlopeTrick 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();
        }

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

        return *this;
    }

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

        return *this;
    }
};

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 ) {
        SlopeTrick aux;
        aux.a = 1;
        aux.b = -D[i];

        if (D[i] < 0) D[i] = 0;

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

        ans += aux;
    }

    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...