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>
#define ff first
#define ss second
using namespace std;
using ll = long long;
int n;
vector<ll> a, pref, suff;
int main(){
cin >> n;
a.resize(n);
pref.resize(n, 0);
suff.resize(n, 0);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 1; i < n; i++) if(a[i - 1] >= a[i]) pref[i] = a[i - 1] - a[i] + 1;
for(int i = n - 2; i >= 0; i--) if(a[i] <= a[i + 1]) suff[i] = a[i + 1] - a[i] + 1;
for(int i = 1; i < n; i++) pref[i] += pref[i - 1];
for(int i = n - 2; i >= 0; i--) suff[i] += suff[i + 1];
if(pref[n - 1] == 0 || suff[0] == 0){
cout << 0; return 0;
}
ll ans = 1e14;
for(int i = 1; i < n - 1; i++){
ans = min(ans, max(pref[i], suff[i]));
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |