#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
#define ppf pop_front
#define ppb pop_back
#define pf push_front
#define pb push_back
#define fr first
#define sc second
#define mnto(x,y) x = min(x, (__typeof__(x))y)
#define mxto(x,y) x = max(x, (__typeof__(x))y)
#define setval(arr, x) memset(arr, x, sizeof(arr))
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
ll h;
cin >> n >> h;
priority_queue<ll> lq;
priority_queue<ll, vector<ll>, greater<ll>> rq;
ll ans = 0;
for (int i = 0; i < n; ++i) {
ll a;
cin >> a;
if (i == 0) {
lq.push(a);
rq.push(a);
continue;
}
ll range = (ll)i * h;
if (a + range < lq.top()) {
lq.push(a + range);
ans += lq.top() - a - range;
ll topv = lq.top();
lq.pop();
lq.push(a + range);
rq.push(topv - range * 2ll);
} else if (a - range > rq.top()) {
rq.push(a - range);
ans += a - range - rq.top();
ll topv = rq.top();
rq.pop();
rq.push(a - range);
lq.push(topv + range * 2ll);
} else {
lq.push(a + range);
rq.push(a - range);
}
}
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... |