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;
#define ll long long int
#define sz size
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
ll h;
cin >> n >> h;
priority_queue<ll> l;
priority_queue<ll,vector<ll>,greater<ll>> r;
ll a;
ll ans = 0;
cin >> a;
l.push(a);
r.push(a);
ll diff;
for (int i = 1; i < n; i++){
cin >> a;
diff = h*i;
if (a+diff < l.top()){
l.push(a+diff);
l.push(a+diff);
ans += abs(l.top()-a-diff);
r.push(l.top()-diff*2);
l.pop();
} else if (a-diff > r.top()){
r.push(a-diff);
r.push(a-diff);
ans += abs(a-diff-r.top());
l.push(r.top()+2*diff);
r.pop();
} else {
l.push(a+diff);
r.push(a-diff);
}
}
cout << ans << "\n";
}
# | 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... |