#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1000005];
int c[1000005];
long long ans = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  m = 0;
  for (int i = 1; i <= n; i++) {
    while (m > 1 && c[m] <= c[m - 1] && a[i] >= c[m]) {
      m--;
      ans += min(a[i], c[m]);
    }
    m++;
    c[m] = a[i];
  }
  sort(c + 1, c + m + 1);
  for (int i = 1; i <= m - 2; i++) ans += c[i];
  cout << ans;
  return 0;
}
