#include <bits/stdc++.h>
using namespace std;
int a[500100];
int pr[500100];
int ne[500100];
pair<int, int> p[500100];
int cmp_pii(const pair<int, int>& x, const pair<int, int>& y) {
  if (x.first == y.first) return x.second < y.second;
  return x.first < y.first;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = int(0); i < int(n); i++) {
    pr[i] = i - 1;
    ne[i] = i + 1;
    scanf("%d", &a[i]);
    p[i] = pair<int, int>(a[i], i);
  }
  sort(p, p + n, cmp_pii);
  int lo = 0, hi = n - 1;
  long long ans = 0;
  for (int i = int(0); i < int(n - 2); i++) {
    int j = p[i].second;
    if (j == lo) {
      ans += a[j];
      lo = ne[lo];
      continue;
    }
    if (j == hi) {
      ans += a[j];
      hi = pr[hi];
      continue;
    }
    int x = a[pr[j]];
    int y = a[ne[j]];
    if (x < y) {
      ans += x;
    } else {
      ans += y;
    }
    pr[ne[j]] = pr[j];
    ne[pr[j]] = ne[j];
  }
  printf("%I64d\n", ans);
}
