/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
const ll INF = 1e13;
ll n, h, a[N];
void solve(){
cin >> n >> h;
for(int i = 1; i <= n; ++i) cin >> a[i];
priority_queue<ll> L, R;
ll l_shift = 0, r_shift = 0;
L.push(a[1]);
R.push(-a[1]);
ll ans = 0;
for(int i = 2; i <= n; ++i){
l_shift += h;
r_shift += h;
ll left_top = L.top() - l_shift;
ll right_top = -R.top() + r_shift;
assert(left_top <= right_top);
if(left_top <= a[i] && a[i] <= right_top){
L.push(a[i] + l_shift);
R.push(-(a[i] - r_shift));
}else if(a[i] < left_top){
L.push(a[i] + l_shift);
L.push(a[i] + l_shift);
ll val = L.top() - l_shift; L.pop();
R.push(- (val - r_shift));
ans += abs(a[i] - (-R.top() + r_shift));
}else{
R.push(-(a[i] - r_shift));
R.push(-(a[i] - r_shift));
ll val = -R.top() + r_shift; R.pop();
L.push(val + l_shift);
ans += abs(a[i] - (L.top() - l_shift));
}
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\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... |