#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define INF 0x3f3f3f3f
#define chmin(a,b)(a)=min((a),(b))
using namespace std;

int dp[5001][4], p[5000];
int main() {
	int n; scanf("%d", &n);
	rep(i, n)scanf("%d", &p[i]);
	memset(dp, 0x3f, sizeof(dp));
	rep(i, 4)dp[0][i] = i;
	rep(i, n)rep(j, 4)chmin(dp[i + 1][max(0, p[i] - j)], dp[i][j] + max(0, p[i] - j) * (i == n - 1 ? 1 : 2));
	printf("%d\n", *min_element(dp[n], dp[n] + 4));
}