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 i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<i64> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
i64 ans = 0;
auto update = [&](int l, int r, i64 val) {
for (; l <= r; l++) {
a[l] += val;
}
};
for (int l = 1, r = n - 1; l <= r; ) {
while (l < n && a[l] > a[l - 1]) l++;
while (r - 1 >= 0 && a[r - 1] > a[r]) r--;
if (l == r) {
ans++;
break;
}
if (l > r) break;
i64 val = std::min(a[l - 1] + 1 - a[l], a[r] + 1 - a[r + 1]);
update(l, r - 1, val);
ans += val;
// std::cout << l << " " << r << "\n";
// for (int i = 0; i < n; i++) {
// std::cout << a[i] << " ";
// } std::cout << "\n";
}
std::cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |