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>
using namespace std;
long long n,h;
long long s[200100];
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
cin>>n>>h;
for (long long i = 1;i <= n;i ++){
cin>>s[i];
}
priority_queue <long long> l;
priority_queue <long long,vector <long long> ,greater <long long> > r;
long long add_l = 0,add_r = 0;
long long ans = 0;
long long pos = -1e18;
for (long long i = 1;i <= n;i ++){
add_l -= h;
add_r += h;
pos -= h;
if (s[i] >= pos){
ans += s[i];
r.push(s[i]-add_r);
r.push(s[i]-add_r);
l.push(r.top()+add_r-add_l);
r.pop();
ans -= l.top() + add_l;
}
else{
ans -= s[i];
l.push(s[i]-add_l);
l.push(s[i]-add_l);
r.push(l.top()+add_l-add_r);
l.pop();
ans += r.top()+add_r;
}
pos = l.top() + add_l;
}
cout<<ans<<'\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... |