Submission #1054566

#TimeUsernameProblemLanguageResultExecution timeMemory
1054566unnickSafety (NOI18_safety)C++14
100 / 100
59 ms5492 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

typedef long long int ll;

int main() {
    int n, h;
    cin >> n >> h;
    
    priority_queue<ll> lq;
    priority_queue<ll, vector<ll>, greater<ll> > hq;
    ll off = 0;

    ll val = 0;

    for (int i = 0; i < n; i++) {
        int e;
        cin >> e;
        if (i > 0) off += h;
        else {
            hq.push(e);
            lq.push(e);
            continue;
        }
        ll cut = lq.top() - off;
        if (e <= cut) {
            val += cut - e;
            lq.push(e+off);
            lq.push(e+off);
            hq.push(lq.top() - off - off);
            lq.pop();
        } else {
            if (e > hq.top() + off) val -= hq.top() + off - e;
            hq.push(e-off);
            hq.push(e-off);
            lq.push(hq.top() + off + off);
            hq.pop();
        }
    }

    cout << val << "\n";
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...