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;
const long long maxn = 2e5 + 12, inf = 1e17 ;
long long n, a[maxn], sum[maxn][2], nat[maxn][2];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i + 1];
for(int i = 1; i <= n; i++){
sum[i][0] = sum[i - 1][0] + (a[i - 1] + 1 - a[i]) * (a[i - 1] + 1 - a[i] >= 0);
nat[i][0] = max(nat[i - 1][0] + 1, a[i]);
}
for(int i = n; i >= 1; i--){
sum[i][1] = sum[i + 1][1] + (a[i + 1] + 1 - a[i]) * (a[i + 1] + 1 - a[i] >= 0);
nat[i][1] = max(nat[i + 1][1] + 1, a[i]);
}
long long ans = inf;
for(int i = 1; i <= n; i++){
long long mx = max(sum[i - 1][0], sum[i + 1][1]);
long long s = sum[i + 1][1] + a[i + 1], f = sum[i - 1][0] + a[i - 1];
ans = min(ans, mx + max(max(f, s) + 1 - mx - a[i], 0ll));
// cout << i << ' ' << f << ' ' << s << ' ' << sum[i - 1][0] << ' ' << sum[i + 1][1] << endl;
// cout << ans << endl;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |