#include <bits/stdc++.h>
using namespace std;
const int A = (int)1e9, N = 528228;
long long o;
set<pair<int, int> > g;
int i, e[2], d[2], t[2][N * 4], j, n, m, a[N];
void modi(int v, int l, int r, int g, int h) {
  if (l == r) {
    if (g)
      t[0][v] = h, t[1][v] = h;
    else
      t[0][v] = -A, t[1][v] = A;
    return;
  }
  int mid = (l + r) / 2;
  if (h <= mid)
    modi(v * 2, l, mid, g, h);
  else
    modi(v * 2 + 1, mid + 1, r, g, h);
  t[0][v] = max(t[0][v * 2], t[0][v * 2 + 1]);
  t[1][v] = min(t[1][v * 2], t[1][v * 2 + 1]);
}
long long get(int v, int l, int r, int _l, int _r) {
  if (l > r || _l > _r) return A;
  if (l == _l && r == _r) return t[1][v];
  int mid = (l + r) / 2;
  return min(get(v * 2, l, mid, _l, min(_r, mid)),
             get(v * 2 + 1, mid + 1, r, max(_l, mid + 1), _r));
}
long long _get(int v, int l, int r, int _l, int _r) {
  if (l > r || _l > _r) return -A;
  if (l == _l && r == _r) return t[0][v];
  int mid = (l + r) / 2;
  return max(_get(v * 2, l, mid, _l, min(_r, mid)),
             _get(v * 2 + 1, mid + 1, r, max(_l, mid + 1), _r));
}
int main() {
  cin >> n;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    g.insert(make_pair(a[i], i + 1));
    modi(1, 1, n, 1, i + 1);
  }
  for (i = 0; i < n - 2; i++) {
    e[0] = (*g.begin()).first, e[1] = (*g.begin()).second, g.erase(*g.begin());
    modi(1, 1, n, 0, e[1]);
    d[0] = _get(1, 1, n, 1, e[1]), d[1] = get(1, 1, n, e[1], n);
    if (d[0] != -A && d[1] != A)
      o += min(a[d[0] - 1], a[d[1] - 1]);
    else
      o += e[0];
  }
  cout << o << "\n";
}
