#include <bits/stdc++.h>
using namespace std;
int n, a[500010], b[500010];
int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    long long ans = 0;
    if (n > 2) {
      int id = 0;
      b[id++] = a[0];
      b[id++] = a[1];
      for (int i = 2; i < n; i++) {
        while (b[id - 1] <= b[id - 2] && b[id - 1] <= a[i]) {
          ans += min(b[id - 2], a[i]);
          id--;
        }
        b[id++] = a[i];
      }
      sort(b, b + id);
      for (int i = 0; i < id - 2; i++) ans += b[i];
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
