제출 #565911

#제출 시각아이디문제언어결과실행 시간메모리
565911hoanghq2004Safety (NOI18_safety)C++14
100 / 100
75 ms5520 KiB
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 2e5 + 10;

int n, h;
priority_queue <long long> L, R;

int main() {
    ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> h;
    long long x, delta = 0;
    cin >> x;
    delta += x - h;
    L.push(x - 1LL * h * n), R.push(- x - 1LL * h * n);
    for (int i = 2; i <= n; ++i) {
        cin >> x;
        delta += x - 1LL * h * i;
        long long add = 1LL * h * (n - i + 1);
        if (L.top() + add <= x && x <= - R.top() - add) {
            L.push(x - add), R.push(- x - add);
        } else if (x < L.top() + add) {
            L.push(x - add), L.push(x - add);
            long long y = L.top() + add * 2;
            L.pop();
            R.push(- y);
        } else {
            R.push(- x - add), R.push(- x - add);
            long long y = - R.top() - add * 2;
            R.pop();
            L.push(y);
        }
    }
    while (L.size()) delta -= L.top(), L.pop();
    cout << delta;
}
#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...