#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 5e5 + 10;
long long a[MAXN];
int main() {
  long long n, t = 0, res = 0;
  scanf("%I64d", &n);
  for (long long i = 0; i < n; i++) {
    scanf("%I64d", &a[t++]);
    while (t > 2 && min(a[t - 1], a[t - 3]) >= a[t - 2]) {
      res += min(a[t - 1], a[t - 3]);
      a[t - 2] = a[t - 1];
      t--;
    }
  }
  if (t > 0) {
    long long L = a[0], R = a[t - 1], l = 1, r = t - 2;
    while (l <= r) {
      while (r >= l && R <= L) {
        res += R;
        R = a[r--];
      }
      if (l <= r) {
        res += L;
        L = a[l++];
      }
    }
  }
  printf("%I64d", res);
}
