Submission #567934

#TimeUsernameProblemLanguageResultExecution timeMemory
567934four_specksPotatoes and fertilizers (LMIO19_bulves)C++17
100 / 100
156 ms19128 KiB
#include <bits/stdc++.h>

using namespace std;

inline namespace
{
} // namespace

void solve()
{
    int n;
    cin >> n;

    vector<long> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];

    long c = 0;
    for (int i = 0; i < n; i++)
        c += a[i] - b[i];

    long cost = 0;

    priority_queue<long> pq;
    long d = 0;
    for (int i = 0; i < n; i++)
    {
        d += a[i] - b[i];

        long e = d;
        if (e < 0)
        {
            cost -= e;
            e = 0;
        }
        else if (e > c)
        {
            cost += e - c;
            e = c;
        }

        pq.push(e), pq.push(e);
        cost += pq.top() - e;
        pq.pop();
    }

    cout << cost << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);

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