#include <bits/stdc++.h>
// Author: Kazuki_Will_Win_VOI_8703
#define fi first
#define se second
#define pii pair<int, int>
#define int long long
#define all(a) a.begin(), a.end()
using namespace std;
const int mn = 5e5 + 5, bm = (1 << 11) + 1, mod = 1e9 + 7, offset = 5e4, B = 320 + 5;
const int inf = 1e9, base = 311;
int n, a[mn], prefix[mn], suffix[mn];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++){
prefix[i] = prefix[i - 1];
if(i > 1 && a[i] < a[i - 1] + 1) prefix[i] += (a[i - 1] + 1 - a[i]);
}
for(int i = n; i >= 1; i--){
suffix[i] = suffix[i + 1];
if(i < n && a[i] < a[i + 1] + 1) suffix[i] += (a[i + 1] + 1 - a[i]);
}
int res = inf;
for(int i = 1; i <= n; i++){
res = min(res, max(prefix[i], suffix[i]));
}
cout << res << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
while(t --){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |