# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1101898 | Warinchai | Safety (NOI18_safety) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define int long long
using namespace std;
priority_queue<int,vector<int>,greater<int>>r;
priority_queue<int>l;
int ar[200005];
int lzl,lzr;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,h;cin>>n>>h;
for(int i=1;i<=n;i++){
cin>>ar[i];
}
int ans=0;
for(int i=1;i<=n;i++){
if(i!=1&&ar[i]<l.top()+lzl){
ans+=l.top()+lzl-ar[i];
l.push(ar[i]-lzl);
l.push(ar[i]-lzl);
r.push(l.top()+lzl-lzr);
l.pop();
}else if(i!=1&&ar[i]>r.top()+lzr){
ans+=ar[i]-r.top()-lzr;
r.push(ar[i]-lzr);
r.push(ar[i]-lzr);
l.push(r.top()+lzr-lzl);
r.pop();
}else{
l.push(ar[i]-lzl);
r.push(ar[i]-lzr);
}
lzl-=h;
lzr+=h;
}
cout<<ans;
}