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"
typedef long long ll;
using namespace std;
#define int long long
signed main(){
int n;
cin >> n;
int a[n];
int maxx = 0, ii = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
if(a[i] > maxx){
maxx = a[i]; ii = i;
}
}
int ans = 0;
int l = ii, r = ii;
while(true){
if(l > 0 && a[l-1] < a[l]) l--;
if(r < n-1 && a[r+1] < a[r]) r++;
if(r < n-1 && l > 0 && a[l-1] >= a[l] && a[r+1] >= a[r]){
ans+= max(a[l-1]-a[l]+1, a[r+1]-a[r]+1);
a[l] += a[l-1]-a[l]+1;
a[r] += a[r+1]-a[r]+1;
l--;
r++;
}
if(r < n-1 && a[r+1] >= a[r]){
ans += a[r+1]-a[r]+1;
a[r] += a[r+1]-a[r]+1;
r++;
}
if(l > 0 && a[l-1] >= a[l]){
ans += a[l-1]-a[l]+1;
a[l] += a[l-1]-a[l]+1;
l--;
}
if(l == 0 && r == n-1) break;
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |