#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 501000;
struct ArrayItem {
  long long val, num;
  long long prev, next;
} A[MAXN];
long long N;
long long Sum = 0;
bool operator<(ArrayItem A, ArrayItem B) { return A.val > B.val; }
priority_queue<ArrayItem> Q;
void Delete(ArrayItem t) {
  long long i = t.num;
  A[A[i].prev].next = A[i].next;
  A[A[i].next].prev = A[i].prev;
  if (A[i].prev == 0 || A[i].next == N + 1)
    Sum += A[i].val;
  else
    Sum += min(A[A[i].prev].val, A[A[i].next].val);
}
int main() {
  cin >> N;
  for (long long i = 1; i <= N; i++) {
    cin >> A[i].val;
    A[i].num = i;
    A[i].prev = i - 1;
    A[i].next = i + 1;
    Q.push(A[i]);
  }
  long long n = N;
  while (n-- > 2) {
    Delete(Q.top());
    Q.pop();
  }
  cout << Sum << endl;
  return 0;
}
