#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
const int maxn = 5e5 + 5;
stack<int> s;
long long a[maxn];
long long n, ans, x, y, tot;
void init() {
  n = read();
  for (int i(1); i <= n; ++i) {
    a[i] = read();
    while (s.size() >= 2) {
      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]);
  }
}
signed main() {
  init();
  while (!s.empty()) a[++tot] = s.top(), s.pop();
  sort(a + 1, a + 1 + tot);
  for (int i(1); i < tot - 1; ++i) ans += a[i];
  printf("%lld", ans);
  return 0;
}
