# include <iostream>
# include <algorithm>
using namespace std;

const int MAX=1000003;
int a[MAX];
int n;
int ans;

int main()
{

    cin>>n;

    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        ans=ans+a[i];
    }

    if(n==0) {cout<<0<<endl;return 0;}
    if(n==1) {cout<<a[1]<<endl;return 0;}

    int x=a[1],y=a[2];
    bool f=0;
    if(n>=2)
    {
        if(x>y) {ans=ans-y;f=1;}
        if(y>x) {ans=ans-x;x=y;f=1;}
    }

    for(int i=3;i<=n;i++)
    {
        y=a[i];
        //cout<<x<<" "<<y<<endl;
        if(x>y and f==1) {x=y;f=0;}
        if(x<y) {ans=ans-x;x=y;f=1;}
    }

    cout<<ans<<endl;
    return 0;
}
