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 int long long
const int N = 2e5 + 5;
int a[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,h;
cin >> n >> h;
for (int i = 1; i <= n; i++) cin >> a[i];
priority_queue<int,vector<int>,greater<int>>q;
priority_queue<int>p;
q.push(a[1]);
p.push(a[1]);
int ans = 0;
for (int i = 2; i <= n; i++) {
int tmp = (i - 1) * h,x = p.top(),y = q.top();
if (x - tmp > a[i]) {
p.push(a[i] + tmp);
p.push(a[i] + tmp);
x -= tmp;
ans += x - a[i];
p.pop();
q.push(x - tmp);
}
else if (y + tmp < a[i]) {
q.push(a[i] - tmp);
q.push(a[i] - tmp);
y += tmp;
ans += a[i] - y;
q.pop();
p.push(y + tmp);
}
else {
p.push(a[i] + tmp);
q.push(a[i] - tmp);
}
}
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... |