#include <bits/stdc++.h>

using namespace std;
long long d=2000000001;
int main()
{
    long long n;
    cin >> n;
    long long a[200000];
    for (long long i=0; i<n; i++)
    {
        cin >> a[i];
        if (a[i]<d) d=a[i];
    }
    long long res=0;
    for (long long i=0; i<n; i++)
    {
        if (i==0)
        {
            if (a[i]>a[i+1]) res=res+a[i]-a[i+1];
        }
        else if (i==n-1)
        {
            if (a[i]>a[i-1]) res=res+a[i]-a[i-1];
        }
        else
        {
            if (a[i]>=a[i+1] || a[i]>=a[i-1])
            {
                res=res+max(a[i]-a[i-1], a[i]-a[i+1]);
            }
        }
    }
    cout << res+d;
    return 0;
}
