#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define len(x) (x).size()
#define lsb(x) (x) & (-x)
#define l(x) (x << 1) + 1
#define r(x) (x << 1) + 2
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
template <typename T>
void prll(T t) {
cout << t << "\n";
}
template <typename T, typename... Args>
void prll(T t, Args ...args) {
cout << t << " ";
prll(args...);
}
const ll N = 2e5 + 1;
ll n, h, shift, ans, a[N];
priority_queue <ll> lef;
priority_queue <ll, vector<ll>, greater<ll>> rig;
ll ltop() { return lef.top() - shift; }
ll rtop() { return rig.top() + shift; }
void add_slope(ll x) {
if (x <= ltop()) {
rig.push(ltop() - shift);
ans += abs(x - ltop());
lef.pop();
lef.push(x + shift);
lef.push(x + shift);
return;
}
if (x >= rtop()) {
lef.push(rtop() + shift);
ans += abs(x - rtop());
rig.pop();
rig.push(x - shift);
rig.push(x - shift);
return;
}
lef.push(x + shift);
rig.push(x - shift);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> h;
for (ll i = 1; i <= n; ++i) {
cin >> a[i];
}
lef.push(a[1]);
rig.push(a[1]);
for (ll i = 2; i <= n; ++i) {
shift += h;
add_slope(a[i]);
}
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... |