#include <bits/stdc++.h>
using namespace std;
long long arr[500005];
long long n, ars, res;
void solve() {
  long long i, x;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld", &x);
    while (ars > 1 && arr[ars - 1] >= arr[ars] && x >= arr[ars]) {
      res += min(arr[ars - 1], x);
      ars--;
    }
    arr[++ars] = x;
  }
  sort(arr + 1, arr + 1 + ars);
  for (i = 1; i <= ars - 2; i++) res += arr[i];
  printf("%lld\n", res);
}
int main() {
  solve();
  return 0;
}
