#include <bits/stdc++.h>
using namespace std;
using namespace std;
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
stack<long long> s;
long long a[500005], n, ans = 0, tot = 0;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    while (s.size() >= 2) {
      long long x, y;
      x = s.top(), s.pop();
      y = s.top();
      if (y >= x && x <= a[i]) {
        ans += min(a[i], y);
      } else {
        s.push(x);
        break;
      }
    }
    s.push(a[i]);
  }
  while (!s.empty()) {
    a[++tot] = s.top();
    s.pop();
  }
  sort(a + 1, a + 1 + tot);
  for (int i = 1; i <= tot - 2; i++) {
    ans += a[i];
  }
  cout << ans;
}
