/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 200000;
int N, H;
int S[N_MAX + 2];
ll valueAtZero;
int deletedLeft;
multiset <ll> leftPivots;
multiset <ll> rightPivots;
ll shiftLeft, shiftRight;
void addPivot (int 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 |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
158 ms |
14412 KB |
Output is correct |
2 |
Correct |
216 ms |
20388 KB |
Output is correct |
3 |
Correct |
217 ms |
21328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |