#include <bits/stdc++.h>
using namespace std;
long long a, s, d[500002][3], f, g, h, j, k, l, i, n, m;
long long inf = 100000000000000000ll;
pair<long long, int> p[500003], p1;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    scanf("%d", &a);
    d[i][0] = a;
    d[i][1] = i - 1;
    d[i][2] = i + 1;
    p[i] = {a, i};
  }
  sort(p + 1, p + n + 1);
  i = 0;
  long long y = 0;
  int L = 1, R = n;
  while (n - i - 2 > 0) {
    p1 = p[i + 1];
    if (p1.second == L || p1.second == R) {
      k += (p1.first - y) * (n - i - 2);
      y = p1.first;
      if (p1.second == L)
        L = d[p1.second][2];
      else
        R = d[p1.second][1];
    } else {
      k = k + min(d[d[p1.second][1]][0], d[d[p1.second][2]][0]) - y;
      d[d[p1.second][1]][2] = d[p1.second][2];
      d[d[p1.second][2]][1] = d[p1.second][1];
    }
    i++;
  }
  cout << k;
}
