#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 10;
int le[MAXN], ri[MAXN];
int a[MAXN];
struct num {
  int val, pos;
};
num data[MAXN];
bool cmp(num a, num b) { return a.val < b.val; }
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    data[i].val = a[i];
    data[i].pos = i;
    le[i] = i - 1;
    ri[i] = i + 1;
  }
  sort(data, data + n, cmp);
  long long ans = 0;
  int low = 0;
  int hi = n - 1;
  for (int i = 0; i < n - 2; i++) {
    int pos = data[i].pos;
    if (low == pos) {
      ans += a[pos];
      low = ri[pos];
    } else if (hi == pos) {
      ans += a[pos];
      hi = le[pos];
    } else {
      ans += min(a[le[pos]], a[ri[pos]]);
      ri[le[pos]] = ri[pos];
      le[ri[pos]] = le[pos];
    }
  }
  cout << ans << '\n';
  return 0;
}
