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