This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
int N;
ll H;
ll S[N_MAX + 2];
ll valueAtZero;
int deletedLeft;
multiset <ll> leftPivots;
multiset <ll> rightPivots;
ll shiftLeft, shiftRight;
void addPivot (ll piv) {
if (rightPivots.empty() == true
|| *rightPivots.begin() + shiftRight <= piv) {
rightPivots.insert(piv - shiftRight);
} else {
leftPivots.insert(piv - shiftLeft);
}
if ((int) leftPivots.size() + deletedLeft > (int) rightPivots.size()) {
rightPivots.insert(*prev(leftPivots.end()) + shiftLeft - shiftRight);
leftPivots.erase(prev(leftPivots.end()));
}
if ((int) rightPivots.size() > (int) leftPivots.size() + deletedLeft + 1) {
leftPivots.insert(*rightPivots.begin() + shiftRight - shiftLeft);
rightPivots.erase(rightPivots.begin());
}
}
void expand () {
shiftLeft -= H;
shiftRight += H;
while (leftPivots.empty() == false && *leftPivots.begin() < 0) {
leftPivots.erase(leftPivots.begin());
deletedLeft++;
}
valueAtZero -= H * (int) leftPivots.size();
}
ll getMin () {
ll value = valueAtZero;
for (ll piv : leftPivots) {
value -= (piv + shiftLeft);
}
return value;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> H;
for (int i = 1; i <= N; i++) {
cin >> S[i];
}
valueAtZero += S[1];
addPivot(S[1]);
addPivot(S[1]);
for (int i = 2; i <= N; i++) {
valueAtZero += S[i];
expand();
addPivot(S[i]);
addPivot(S[i]);
}
cout << getMin() << "\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... |