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;
int a[200001];
int l[200001], r[200001];
int cl[200001], cr[200001];
int main(){
int n;
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
long long ans = 0;
int l = 1, r = n - 1;
while(l <= r){
int d = max(0, min((a[l - 1] + 1) - a[l], (a[r + 1] + 1) - a[r]));
a[l - 1] -= d;
a[r + 1] -= d;
ans += d;
if(a[l] > a[l - 1]) l++;
if(a[r] > a[r + 1]) r--;
}
if(l == r + 1 && a[l] == a[r]) ans++;
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |