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