#include <iostream>
using namespace std;
constexpr int N = 2e5 + 5;
int a[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i - 1] -= a[i];
}
a[0] = -1;
int low = 1;
int high = n - 1;
a[n] = 1;
long long res = 0;
while (true) {
while (a[low] < 0) low++;
while (a[high] > 0) high--;
if (low > high) break;
if (low == high) {
res++;
break;
}
int t = min(a[low], -a[high]) + 1;
a[low] -= t;
a[high] += t;
res += t;
}
cout << res;
return 0;
}