#include <bits/stdc++.h>
using namespace std;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,h;
cin >> n >> h;
int arr[n];
int mxval = 0;
for(int &i : arr){
cin >> i;
mxval=max(mxval,i);
}
multiset<long long>left,right;
long long m = 1;
long long c = -arr[0];
left.insert(arr[0]);
right.insert(arr[0]);
long long lazl = 0;
long long lazr=0;
for(int i = 1;i<n;i++){
lazl-=h;
lazr+=h;
long long lef = *(--left.end())+lazl;
long long rig = *(right.begin())+lazr;
//point addition:
c-=arr[i];
//expansion:
c-=h*m;
m++;
if(lef<=arr[i]&&arr[i]<=rig){
left.insert(arr[i]-lazl);
right.insert(arr[i]-lazr);
continue;
}
if(lef<=arr[i]){
left.insert(rig-lazl);
right.erase(right.begin());
right.insert(arr[i]-lazr);
right.insert(arr[i]-lazr);
}
else{
right.insert(lef-lazr);
left.erase(--left.end());
left.insert(arr[i]-lazl);
left.insert(arr[i]-lazl);
}
}
for(long long i : right){
c+=lazr+i;
}
cout << c;
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... |