#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define siz(v) ((int)(v).size())
#define all(v) begin((v)), end(v)
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define dbg(x) "[" #x " = " << x << "]"
#define lli pair<long long, int>
#define left __left
#define right __right
using namespace std;
const int MAXN = 2e5 + 5, MAXK = 16, LOG = 20, infINT = 1e9 + 23737;
const long long inf = 1e18 + 5;
const int dx[4] = {-1, 0, 0, 1};
const int dy[4] = {0, -1, 1, 0};
template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
template<class X, class Y> bool maximize(X &x, const Y &y){return x < y ? x = y, 1: 0;}
int numVal, h[MAXN], left[MAXN], right[MAXN];
long long f[MAXN], g[MAXN];
void input(){
cin >> numVal;
for(int i = 1; i <= numVal; i++) cin >> h[i];
}
void solve(){
for(int i = 1; i <= numVal; i++){
if (h[i - 1] < h[i]) left[i] = left[i - 1] + 1;
else left[i] = 1;
}
for(int i = numVal; i >= 1; i--){
if (h[i] > h[i + 1]) right[i] = right[i + 1] + 1;
else right[i] = 1;
}
for(int i = 1; i <= numVal; i++){
f[i] = f[i - left[i]] + max(0, h[i - left[i]] + 1 - h[i - left[i] + 1]);
}
for(int i = numVal; i >= 1; i--){
g[i] = g[i + right[i]] + max(0, h[i + right[i]] + 1 - h[i + right[i] - 1]);
}
long long res = inf;
for(int i = 1; i <= numVal; i++){
res = min(res, f[i - left[i]] + g[i + right[i]] + max({0, h[i - left[i]] + 1 - h[i - left[i] + 1], h[i + right[i]] + 1 - h[i + right[i] - 1]}));
}
cout << res << '\n';
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t--){
input();
solve();
}
cerr << TIME << ".s\n";
}