#include <bits/stdc++.h>
using namespace std;
const long long maxN = 5e5 + 10;
long long n, a[maxN], nxt[maxN], prv[maxN];
set<pair<long long, long long> > s;
long long res = 0;
int32_t main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    prv[i] = i - 1;
    nxt[i] = i + 1;
    s.insert(make_pair(a[i], i));
  }
  long long l = 0, r = n - 1;
  long long res = 0;
  while (s.size() > 2) {
    long long x = s.begin()->first;
    long long pos = s.begin()->second;
    s.erase(s.begin());
    if (pos == l) {
      res += x;
      l = nxt[l];
    } else if (pos == r) {
      res += x;
      r = prv[r];
    } else {
      res += min(a[prv[pos]], a[nxt[pos]]);
      nxt[prv[pos]] = nxt[pos];
      prv[nxt[pos]] = prv[pos];
    }
  }
  cout << res << endl;
  return 0;
}
