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;
using pq_max = priority_queue<ll>;
using pq_min = priority_queue<ll, vector<ll>, greater<ll>>;
pq_max heap1;
pq_min heap2;
ll min_val = 0;
void add(int x, ll shift)
{
if(x + shift >= heap1.top() && x - shift <= heap2.top())
{
heap1.push(x + shift);
heap2.push(x - shift);
return;
}
if(x + shift < heap1.top())
{
heap1.push(x + shift);
heap1.push(x + shift);
ll out = heap1.top();
heap1.pop();
min_val += out - (x + shift);
heap2.push(out - 2 * shift);
}
else if(x - shift > heap2.top())
{
heap2.push(x - shift);
heap2.push(x - shift);
ll out = heap2.top();
heap2.pop();
min_val += (x-shift) - out;
heap1.push(out + 2 * shift);
}
else assert(0);
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
int i, x, n, H;
cin >> n >> H;
cin >> x;
heap1.push(x);
heap2.push(x);
for(i=2; i<=n; ++i)
{
cin >> x;
add(x, (ll) H * (i-1));
}
cout << min_val << '\n';
return 0;
}
# | 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... |