| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1284003 | imarn | Safety (NOI18_safety) | C11 | 0 ms | 0 KiB |
//#include "festival.h"
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
#define lb(x,i) lower_bound(all(x),i)-x.begin()
#define sz(x) (ll)x.size()
#define cd complex<double>
#define t3 tuple<ll,ll,ll>
using namespace std;
const int mxn=2e5+5;
priority_queue<ll,vector<ll>,greater<ll>>rq;
priority_queue<ll>lq;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;ll h;cin>>n>>h;
int x;cin>>x;
ll rs=0;lq.push(x),rq.push(x);
ll up=0,lo=0;
for(int i=1;i<n;i++){
up+=h,lo+=h;
cin>>x;
if(lq.top()>x+lo){
rs+=lq.top()-(x+lo);
lq.push(x+lo);
rq.push(lq.top()-lo-up);
lq.push(x+lo);
lq.pop();
}
else if(rq.top()<x-up){
rs+=x-up-(rq.top());
rq.push(x-up);
lq.push(rq.top()+lo+up);
rq.push(x-up);
rq.pop();
}
else {
lq.push(x+lo);
rq.push(x-up);
}
}cout<<rs;
}
