#include<bits/stdc++.h>
using namespace std;
#define int long long
void test_case(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++) {cin >> a[i]; }
int pre[n]={}, suf[n]={};
for(int i = 1; i < n; i++){
int ok = a[i-1] - a[i] +1;
if(ok<0) ok= 0;
pre[i] = pre[i-1]+ok;
}
// for(int i = 0; i < n; i++) cout << pre[i] <<" ";
// cout << endl;
int ans =1e18;
for(int i = n-2; i >=0; i--){
//cout << a[i] <<" "<< a[i+1]<<endl;
int ok = a[i+1] - a[i] +1;
if(ok<0) ok = 0;
suf[i] = suf[i+1]+ok;
//cout << a[i]<< " ";
}
//cout << endl;
// for(int i = 0; i < n; i++) cout << suf[i] <<" ";
// cout << endl;
for(int i = 0; i < n; i++){
int mx = max(pre[i],suf[i]);
ans = min(ans, mx);
}
cout << ans;
}
signed main(){
int t;
t=1;
while(t--) test_case();
}