#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, he, en;
int L[500010], R[500010], a[500010], id[500010];
long long an;
inline bool cc1(const int &A, const int &B) { return a[A] < a[B]; }
inline void del(int x) {
  L[R[x]] = L[x];
  R[L[x]] = R[x];
  if (x == he)
    he = R[x];
  else if (x == en)
    en = L[x];
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    L[i] = i - 1;
    R[i] = i + 1;
    id[i] = i;
  }
  he = 1, en = n;
  sort(id + 1, id + n + 1, cc1);
  for (i = 1; i <= n; i++)
    if (he != en && R[he] != en) {
      int A = id[i];
      if (A == he)
        an += a[he];
      else if (A == en)
        an += a[en];
      else
        an += min(a[L[A]], a[R[A]]);
      del(A);
    }
  printf("%I64d\n", an);
  return 0;
}
