# | TimeUTC-0 | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1166117 | spycoderyt | Safety (NOI18_safety) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#define int long long
using namespace std;
int main() {
int n,h,x,ans=0;
cin >> n >> h;
priority_queue<int> lq;
priority_queue<int,vector<int>,greater<int> > rq;
for(int i = 0;i<n;i++) {
cin >> x;
if(i==0){
lq.push(x);
rq.push(x);
continue;
}
int range = i * h;
if(x + range < lq.top()) {
lq.push(x+range);
ans += abs(lq.top() - (x+range));
lq.push(x+range);
rq.push(lq.top() - 2 * range);
lq.pop();
} else if (x - range > rq.top()) {
rq.push(x-range);
ans += abs(rq.top() - (x - range));
rq.push(x-range);
lq.push(rq.top() + 2 * range);
rq.pop();
} else {