#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int INF = 1000000;
const int N = 500005;
int a[N];
int n;
int top;
long long ans;
void add(int x) {
  a[++top] = x;
  while (top > 2 && a[top - 2] >= a[top - 1] && a[top - 1] <= a[top]) {
    ans += min(a[top - 2], a[top]);
    a[top - 1] = a[top];
    top--;
  }
}
int main() {
  scanf("%d", &n);
  int i;
  for (i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    add(x);
  }
  sort(a + 1, a + top + 1);
  for (i = 1; i <= top - 2; i++) ans += a[i];
  printf("%I64d\n", ans);
}
