#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[500500];
int bef[500500];
int aft[500500];
int main() {
  cin >> n;
  m = n;
  pair<int, int> b[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    b[i] = make_pair(a[i], i);
    aft[i] = i + 1;
    bef[i] = i - 1;
  }
  sort(b, b + n);
  long long res = 0;
  int l = 0, h = n - 1;
  for (int i = 0; i < n - 2; i++) {
    if (b[i].second == l) {
      res += b[i].first;
      l = aft[l];
      continue;
    }
    if (b[i].second == h) {
      res += b[i].first;
      h = bef[h];
      continue;
    }
    int x = b[i].second;
    res += min(a[aft[x]], a[bef[x]]);
    aft[bef[x]] = aft[x];
    bef[aft[x]] = bef[x];
  }
  cout << res;
  return 0;
}
