#include <bits/stdc++.h>
using namespace std;
long long a[500005];
int lt[500005];
int rt[500005];
vector<long long> b;
int main() {
  int i, n, j;
  long long ans = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%I64d", &a[i]);
    if (i == 0) {
      lt[i] = -1;
      rt[i] = i + 1;
    } else {
      lt[i] = i - 1;
      rt[i] = i + 1;
    }
    if (i > 1) {
      j = i;
      while (j > 0 && lt[j] != -1 && lt[lt[j]] != -1 && a[lt[j]] <= a[j] &&
             a[lt[j]] <= a[lt[lt[j]]]) {
        ans += min(a[j], a[lt[lt[j]]]);
        rt[lt[lt[j]]] = rt[lt[j]];
        lt[j] = lt[lt[j]];
      }
    }
  }
  for (i = 0; i < n; i = rt[i]) {
    b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  for (i = 0; i < int(b.size()) - 2; i++) ans += b[i];
  printf("%I64d\n", ans);
  return 0;
}
