Submission #215987

#TimeUsernameProblemLanguageResultExecution timeMemory
215987dolphingarlicSafety (NOI18_safety)C++14
100 / 100
61 ms7100 KiB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

ll a[200001];
priority_queue<ll> L, R;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    ll h;
    cin >> n >> h;
    FOR(i, 0, n) cin >> a[i];

    ll ans = 0;
    L.push(a[0]); R.push(-a[0]);
    FOR(i, 1, n) {
        ll X = L.top() - h * i, Y = -R.top() + h * i;
        if (X > a[i]) {
            ans += X - a[i];
            L.push(a[i] + h * i);
            L.push(a[i] + h * i);
            R.push(-X + h * i);
            L.pop();
        } else if (Y < a[i]) {
            ans += a[i] - Y;
            R.push(-a[i] + h * i);
            R.push(-a[i] + h * i);
            L.push(Y + h * i);
            R.pop();
        } else {
            L.push(a[i] + h * i);
            R.push(-a[i] + h * i);
        }
    }
    cout << ans;
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...