#include <iostream>
using namespace std;

int main ()
{
    long long counter = 0;
    long long a[200003];
    int n;
    int i = 0;
    cin >> n;
    while (i < n)
    {
        i++;
        cin >> a[i];
    }
    i = 0;

while (n>1)
{
    int m = n;
    while (i < n)
    {
        i++;
        if (a[i]==a[i+1])
        {
            int k = i;
             while (i < n)
            {
                i++;
                a[i-1] = a[i];
            }
            n = n - 1;
            i = k - 1;
        }
    }
    if (m!=n) n++;

   i = 0;
    int j = 1;
    int max = 0;
    while (i < n)
    {
        i++;
        if (a[i] > max)
        {
            max = a[i];
            j = i;
        }
    }

  if ((j!=0) && (j!=n))
  {
    if ((a[j-1] > a[j+1]))
    {
        counter+= a[j] - a[j-1];
        a[j] = a[j-1];
    }
    else
    {
        counter+= a[j] - a[j+1];
        a[j] = a[j+1];
    }
  }
  else if (j==0)
  {
        counter+= a[j] - a[j+1];
        a[j] = a[j+1];
  }
  else
  {
      counter+= a[j] - a[j-1];
        a[j] = a[j-1];
  }
        i = 0;
}
            cout << counter + a[1];
}
