#include <bits/stdc++.h>
using namespace std;
long long GCD(long long u, long long v) {
  long long r;
  while (v) {
    r = u % v;
    u = v;
    v = r;
  }
  return u;
}
long long modpow(long long a, long long p, long long mod) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = (ret * a) % mod;
    a = (a * a) % mod;
    p /= 2;
  }
  return ret;
}
long long power(long long a, long long p) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = (ret * a);
    a = (a * a);
    p /= 2;
  }
  return ret;
}
vector<int> a;
int main() {
  int i, j, tmp;
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, sz;
  cin >> n;
  if (n <= 2) {
    cout << 0 << '\n';
    return 0;
  }
  long long ans = 0;
  for (i = 0; i < 2; i++) {
    cin >> tmp;
    a.push_back(tmp);
  }
  sz = 2;
  for (i = 0; i < n - 2; i++) {
    cin >> tmp;
    while (sz > 1 && a[sz - 1] <= tmp && a[sz - 1] <= a[sz - 2]) {
      ans += min(a[sz - 2], tmp);
      a.pop_back();
      sz--;
    }
    a.push_back(tmp);
    sz++;
  }
  sz = (int)(a.size());
  sort(a.begin(), a.end());
  for (i = 0; i < sz - 2; i++) ans += a[i];
  cout << ans << '\n';
  return 0;
}
