#include <bits/stdc++.h>
using namespace std;
#define print(l) for(auto i:l) cout<<i<<" ";cout<<endl;
#define input(t,l,n) vector<t>l(n);for(int i = 0;i<n;i++)cin>>l[i];
#define int long long
#define pb push_back
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(l) l.begin(),l.end()
#define pii pair<int,int>
#define fi first
#define se second
const int M = 1e9+7;
const int inf = 1e18;
const int N = 2e5+5;
int bp(int x, int y, int p){
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int MI(int n, int p){
return bp(n, p - 2, p);
}
int mul(int x,int y, int p){
return x * 1ull * y % p;
}
int di(int x,int y, int p){
return mul(x, MI(y, p), p);
}
int n , m , k , q;
map<vector<int>,vector<int>>X;
vector<int>arr;
// void take(){
// int p1 =0 ;
// for(int i = 1;i<=n;i++){
// for(int j = 1;j<=n;j++){
// for(int k = 1;k<=n;k++){
// for(int l = 1;l<=n;l++){
// X[{i,j,k,l}] = arr[p1];
// p1++;
// if(p1 == arr.size())return;
// }
// }
// }
// }
// }
void solve(int testcase_number){
// cin>>n>>m;
// int y;
// while(cin>>y){
// arr.pb(y);
// }
cin>>n;
input(int,l,n);
vector<int>ol = l;
int ans = 0;
int p1 =0;
while(p1<n){
if(l[p1] == 0){
p1++;
continue;
}
int ff = 1;
if(p1+2<n){
if(l[p1]<=l[p1+1] and l[p1+1]<=l[p1+2]){
ans+=l[p1]*7;
l[p1+1]-=l[p1];
l[p1+2]-=l[p1];
l[p1] =0 ;
p1++;
ff = 0;
}
}
if(p1+1<n and ff){
if(l[p1]<=l[p1+1]){
ans+=l[p1]*5;
l[p1+1]-=l[p1];
l[p1] = 0;
p1++;
}
else{
ans+=3*abs(l[p1]-l[p1+1]);
l[p1] = l[p1+1];
}
}
else{
ans+=3*l[p1];
p1++;
}
}
l = ol;
reverse(all(l));
int ans1 = 0;
p1 =0;
while(p1<n){
if(l[p1] == 0){
p1++;
continue;
}
int ff = 1;
if(p1+2<n){
if(l[p1]<=l[p1+1] and l[p1+1]<=l[p1+2]){
ans1+=l[p1]*7;
l[p1+1]-=l[p1];
l[p1+2]-=l[p1];
l[p1] =0 ;
p1++;
ff = 0;
}
}
if(p1+1<n and ff){
if(l[p1]<=l[p1+1]){
ans1+=l[p1]*5;
l[p1+1]-=l[p1];
l[p1] = 0;
p1++;
}
else{
ans1+=3*abs(l[p1]-l[p1+1]);
l[p1] = l[p1+1];
}
}
else{
ans1+=3*l[p1];
p1++;
}
}
cout<<min(ans,ans1)<<endl;
}
signed main(){
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);
cout << fixed<<setprecision(9);
int t = 1;
// cin>>t;
for(int i = 1;i<=t;i++){
solve(i);
}
}