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 <iostream>
#include <vector>
using namespace std;
const long long INF = 1e18;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
vector<int> a(n + 2);
for (int i = 1; i <= n; i++){
cin >> a[i];
}
vector<int> b = a;
long long pref[n + 2]{};
for (int i = 1; i <= n; i++){
pref[i] = pref[i - 1] + max(b[i], b[i - 1] + 1) - b[i];
b[i] = max(b[i], b[i - 1] + 1);
}
vector<int> c = a;
long long suff[n + 2]{};
for (int i = n; i >= 1; i--){
suff[i] = suff[i + 1] + max(c[i], c[i + 1] + 1) - c[i];
c[i] = max(c[i], c[i + 1] + 1);
}
long long ans = INF;
for (int i = 0; i <= n; i++){
long long res = max(pref[i], suff[i + 1]) + (pref[i] == suff[i + 1] && b[i] == c[i + 1]);
ans = min(ans, res);
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |