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;
#define int long long
int32_t main() {
int n;
cin >> n;
vector<int> v(n + 2);
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
vector<int> pf(n + 1), sf(n + 2);
for (int i = 1; i <= n; i++) {
int k = max(0LL, (v[i - 1] - v[i] + 1));
pf[i] = (pf[i - 1] + k);
}
for (int i = n; i >= 1; i--) {
int k = max(0LL, (v[i + 1] - v[i] + 1));
sf[i] = (sf[i + 1] + k);
}
int ans = 0x3f3f3f3f3f3f3f3f;
for (int i = 1; i <= n; i++) {
// i is k
int x = max(pf[i - 1], sf[i + 1]);
int y = max(v[i - 1], v[i + 1]);
int z = max(0LL, y - v[i] + 1);
//cout << x << " " << y << " " << z << " " << x+z << "\n";
ans = min(ans, x + z);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |