#include <bits/stdc++.h>
using namespace std;
int n, nxt[500005], prv[500005], a[500005];
long long rez;
pair<int, int> v[500005];
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    v[i] = make_pair(a[i], i);
    nxt[i] = i + 1;
    prv[i] = i - 1;
  }
  sort(v, v + n);
  int f = 0, l = n - 1;
  for (int i = 0; i < n - 2; ++i) {
    int ind = v[i].second;
    if (ind == f) {
      rez += v[i].first;
      f = nxt[f];
      continue;
    }
    if (ind == l) {
      rez += v[i].first;
      l = prv[l];
      continue;
    }
    if (ind == 0 || ind == n - 1) continue;
    rez += min(a[prv[ind]], a[nxt[ind]]);
    prv[nxt[ind]] = prv[ind];
    nxt[prv[ind]] = nxt[ind];
  }
  cout << rez;
}
