#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a(n);
int cnt = 0;
for(int i = 0; i < n; i++)
{
cin >> a[i];
cnt = (a[i] == 0);
}
int ans = 0;
while(cnt != n)
{
int mx = 0;
for(int l = 0; l < n; l++){
for(auto len : {2, 3})
{
int r = l + len - 1;
if(r >= n) break;
long long sum = 0;
for(int i = l; i <= r; i++)
sum += a[i];
mx = max((long long)mx, (sum + len - 1) / len);
}
}
for(int i = 0; i < n; i++)
a[i] = max(0, a[i] - mx);
cnt = 0;
for(int i = 0; i < n; i++)
if(a[i] == 0)
cnt++;
ans++;
}
cout << ans << '\n';
return 0;
}