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