#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define popcount __builtin_popcount
#define all(a) (a).begin(), (a).end()
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T,null_type,less_equal<>,rb_tree_tag,tree_order_statistics_node_update>;
void solve(int n, int *A, long long *pref) {
pref[0] = 0;
for (int i = 1; i < n; i++) {
pref[i] = pref[i-1] + max(0, A[i-1] - A[i] + 1);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
int A[n];
for (int& x : A) cin >> x;
long long pref[n], suff[n];
solve(n, A, pref);
reverse(A, A + n);
solve(n, A, suff);
reverse(suff, suff + n);
long long ans = 5e18;
for (int i = 0; i < n; i++)
ans = min(ans, max(pref[i], suff[i]));
cout << ans;
}