#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define REP(i, n) for (int i = (0), _n = (n); i < _n; ++i)
#define MASK(x) (1LL << (x))
#define ON(mask, i) (((mask) >> (i)) & (1LL))
#define ALL(x) begin(x), end(x)
#define el cout << '\n'
template <class X, class Y> inline bool maximize(X &x, const Y &y) { return (x < y) ? x = y, 1 : 0; }
template <class X, class Y> inline bool minimize(X &x, const Y &y) { return (x > y) ? x = y, 1 : 0; }
constexpr int MAXN = 2e5 + 10;
int n;
int a[MAXN], L[MAXN], R[MAXN];
long long pref[MAXN], suff[MAXN];
void load_input(void)
{
cin >> n;
FOR(i, 1, n) cin >> a[i];
}
void prepare(void)
{
L[1] = a[1];
FOR(i, 2, n)
{
L[i] = max(L[i - 1] + 1, a[i]);
pref[i] = pref[i - 1] + (L[i] - a[i]);
}
R[n] = a[n];
FORD(i, n - 1, 1)
{
R[i] = max(R[i + 1] + 1, a[i]);
suff[i] = suff[i + 1] + (R[i] - a[i]);
}
}
void process(void)
{
prepare();
long long res = 1e18;
FOR(i, 1, n) minimize(res, pref[i] + suff[i] + (max(L[i], R[i]) - a[i]));
cout << res << '\n';
}
signed main(void)
{
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
#define task "B"
if (fopen(task".INP", "r"))
{
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int testcase = 1;
while (testcase--)
{
load_input();
process();
}
return 1 ^ 1;
}