#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 500000;
int data[MAX_SIZE + 10], n;
int s[MAX_SIZE + 10], top;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", data + i);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    while (top > 1 && s[top - 2] >= s[top - 1] && data[i] >= s[top - 1]) {
      ans += min(s[top - 2], data[i]);
      --top;
    }
    s[top++] = data[i];
  }
  sort(s, s + top);
  for (int i = 0; i < top - 2; i++) ans += s[i];
  printf(
      "%I64d"
      "\n",
      ans);
  return 0;
}
