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 N 1000005
typedef long long ll;
using namespace std;
ll n, x, val, h[N], ans;
priority_queue<ll> pq1, pq2;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> x;
cin >> h[1];
pq1.push(h[1]);
pq2.push(-h[1]);
for(ll i = 2; i <= n; i++){
val += x;
cin >> h[i];
ll tp1 = pq1.top();
ll tp2 = -pq2.top();
if(tp1 - val <= h[i] && h[i] <= tp2 + val){
pq1.push(h[i] + val);
pq2.push(-(h[i] - val));
}
else{
if(tp1 - val > h[i]){
ans += (tp1 - val - h[i]);
pq1.push(h[i] + val);
pq2.push(-(tp1 - 2 * val));
pq1.pop();
}
else{
ans += (h[i] - val - tp2);
pq2.push(-(h[i] - val));
pq1.push(tp2 + 2 * val);
pq2.pop();
}
}
}
cout << ans;
}
# | 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... |