#include <bits/stdc++.h>
using namespace std;
int n, a[500005], b[500005], cnt, mx1, mx2;
long long ans;
int main() {
  scanf("%d", &n);
  cnt = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    while (cnt > 1 && b[cnt - 2] >= b[cnt - 1] && b[cnt - 1] <= a[i]) {
      ans += min(b[cnt - 2], a[i]);
      cnt--;
    }
    b[cnt++] = a[i];
  }
  if (cnt > 2) {
    for (int i = 1; i < cnt; i++) {
      ans += b[i];
      if (mx2 < b[i]) {
        mx2 = b[i];
        if (mx1 < mx2) swap(mx1, mx2);
      }
    }
    ans -= (mx1 + mx2);
  }
  printf("%I64d", ans);
}
