#include <bits/stdc++.h>
using namespace std;
long long n;
const long long maxn = 500010;
long long stk[maxn], top = 0;
signed main() {
  scanf("%lld", &n);
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long x;
    scanf("%lld", &x);
    while (top >= 2) {
      long long a = stk[top], b = stk[top - 1];
      if (b >= a && a <= x)
        ans += min(x, b), top--;
      else
        break;
    }
    stk[++top] = x;
  }
  sort(stk, stk + 1 + top);
  for (long long i = 1; i <= top - 2; i++) ans += stk[i];
  return printf("%lld\n", ans), 0;
}
