Submission #430772

#TimeUsernameProblemLanguageResultExecution timeMemory
430772smaxPotatoes and fertilizers (LMIO19_bulves)C++17
100 / 100
196 ms15344 KiB
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif

template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}

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

    int n;
    cin >> n;
    vector<long long> diff(n);
    for (int i=0; i<n; i++) {
        int a, b;
        cin >> a >> b;
        diff[i] = a - b + (i > 0 ? diff[i-1] : 0);
    }

    assert(diff[n-1] >= 0);
    long long ret = 0;
    priority_queue<long long> pq;
    for (int i=0; i<n-1; i++) {
        if (diff[i] < 0) {
            ret -= diff[i];
            diff[i] = 0;
        } else if (diff[i] > diff[n-1]) {
            ret += diff[i] - diff[n-1];
            diff[i] = diff[n-1];
        }
        pq.push(diff[i]);
        pq.push(diff[i]);
        ret += pq.top() - diff[i];
        pq.pop();
    }
    cout << ret << "\n";

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