#include <bits/stdc++.h>
using namespace std;
int INT_MAX_VAL = (int)1e20;
int INT_MIN_VAL = (int)-1e20;
long long LONG_MAX_VAL = (long long)1e20;
long long LONG_MIN_VAL = (long long)-1e20;
long long a[500005], pr[500005], ne[500005];
bool added[500005];
long long n;
long long res = 0;
queue<long long> q;
void check(long long i) {
  if (added[i]) return;
  if (a[pr[i]] >= a[i] && a[ne[i]] >= a[i]) {
    added[i] = 1;
    q.push(i);
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pr[i] = i - 1;
    ne[i] = i + 1;
  }
  ne[0] = 1;
  pr[n + 1] = n;
  for (int i = 1; i <= n; i++) {
    check(i);
  }
  while (q.size()) {
    long long e = q.front();
    q.pop();
    res += min(a[pr[e]], a[ne[e]]);
    pr[ne[e]] = pr[e];
    ne[pr[e]] = ne[e];
    check(pr[e]);
    check(ne[e]);
  }
  long long c = ne[0];
  while (c != n + 1) {
    res += min(a[pr[c]], a[ne[c]]);
    c = ne[c];
  }
  cout << res << '\n';
  return 0;
}
