#include <bits/stdc++.h>

typedef long long ll;
using namespace std;

int n; 
int r[5005];
int p[5005];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	
		cin >> n;
		
		memset(r,0,sizeof(r));
		for (int i = 0;i < n;i++){
			cin >> r[i];
		}
		int ans = INT_MAX;

		if(n == 3){
			ans = r[0] + r[1] + r[2];
			ans += max(0,r[1] - r[0] - r[2]);
		}else{

		for (int i = 0;i <= r[1];i++){
			for (int j = 0;j <= r[n-2];j++){
				memcpy(p,r,sizeof(r));
				int sum = 0;
				int a = p[1]-i; int b = p[n-2]-j;
				if(i < p[1] - p[0] || j < p[n-2]-p[n-1]) continue;		
				sum += p[0] + a;
				sum += p[n-1] + b;
				p[1] -= a; p[n-2] -= b;
				for (int k = 1;k < n-1;k++){
					sum += 2*p[k];
					p[k+1] = max(0,p[k+1]-p[k]);
				}
				ans = min(sum,ans);
			}
		}

		for (int i = 0;i <= r[1];i++){
			for (int j = 0;j <= r[n-2];j++){
				memcpy(p,r,sizeof(r));
				int sum = 0;
				if(i < p[1] - p[0] || j < p[n-2]-p[n-1]) continue;
				int a = p[1]-i; int b = p[n-2]-j;				
				sum += p[0] + a;
				sum += p[n-1] + b;
				p[1] -= a; p[n-2] -= b;
				for (int k = n-2;k > 0;k--){
					sum += 2*p[k];
					p[k+1] = max(0,p[k+1]-p[k]);
				}
				ans = min(sum,ans);
			}
		}
		}

		cout << ans << endl;

	

	return 0;

}


