#include <bits/stdc++.h>
using namespace std;
const int MN = 500500;
int a[MN];
struct D {
  int l, r;
  int d;
  void setmin() {
    if (MN <= r) return;
    d = min(a[l], a[r]);
  }
};
D d[MN];
int main() {
  int n;
  cin >> n;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    d[i].l = i - 1;
    d[i].r = i + 1;
    q.push(pair<int, int>(a[i], i));
  }
  for (int i = 1; i < n - 1; i++) {
    d[i].setmin();
  }
  long long r = 0;
  while (q.size() > 2) {
    pair<int, int> p = q.top();
    q.pop();
    D dd = d[p.second];
    if (dd.l < 0 || n <= dd.r) {
      r += a[p.second];
      if (dd.l < 0) {
        d[dd.r].l = -1;
      } else {
        d[dd.l].r = n;
      }
    } else {
      r += dd.d;
      d[dd.l].r = dd.r;
      d[dd.l].setmin();
      d[dd.r].l = dd.l;
      d[dd.r].setmin();
    }
  }
  cout << r << endl;
  return 0;
}
