This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// Created by Mihai145 on 10/25/2021.
//
#include <iostream>
#include <set>
using namespace std;
int N, H, x;
long long ans;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> H >> x;
multiset<long long> lefts, rights;
lefts.insert(x), rights.insert(x);
for(int i = 1; i < N; i++) {
cin >> x;
long long shift = 1LL * i * H;
long long lfb = *lefts.rbegin();
long long lfb_val = lfb - shift;
long long rb = *rights.begin();
long long rb_val = rb + shift;
if(x <= lfb_val) {
ans += (lfb_val - x);
lefts.insert(x + shift);
lefts.insert(x + shift);
lefts.erase(lefts.find(lfb));
rights.insert(rb);
} else if(x >= rb_val) {
ans += (x - rb_val);
rights.insert(x - shift);
rights.insert(x - shift);
rights.erase(rights.find(rb));
lefts.insert(lfb);
} else {
lefts.insert(lfb);
rights.insert(rb);
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |