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;
using ll = long long;
const int inf = 1<<30;
struct sset : public multiset<ll> {
ll bias = 0;
void insert(int x) {
multiset<ll>::insert(x-bias);
}
ll get_min() {
return *begin() + bias;
}
void pop_min() {
erase(find(*begin()));
}
ll get_max() {
return *rbegin() + bias;
}
void pop_max() {
erase(find(*rbegin()));
}
void add(ll x) {
bias += x;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
ll n, h, x;
sset L, R;
cin >> n >> h >> x;
L.insert(x);
R.insert(x);
ll ans = 0;
for(int I = 1; I < n; I++) {
R.add(h);
L.add(-h);
cin >> x;
if(x <= L.get_max()) {
L.insert(x);
L.insert(x);
ans += abs(L.get_max()-x);
R.insert(L.get_max());
L.pop_max();
} else {
R.insert(x);
R.insert(x);
ans += abs(R.get_min()-x);
L.insert(R.get_min());
R.pop_min();
}
}
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... |