#include<bits/stdc++.h>
using namespace std;
int main(){
int n,h[1000],money;
while(cin>>n){
h[0]=1000;
money=0;
for(int i=1;i<=n;i++){
cin>>h[i];
}
h[n+1]=1000;
for(int i=1;i<=n;i++){
if(h[i]==0){
if(h[i-1]<=h[i+1]){
money+=h[i-1];
h[i]=h[i-1];
}
else if(h[i-1]>h[i+1]){
money+=h[i+1];
h[i]=h[i+1];
}
}
}
cout<<money;
}
return 0;
}