#include <bits/stdc++.h>
using namespace std;
const int N = 5 * 10e5 + 5;
int a[N];
int n;
int cnt;
long long ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  while (n--) {
    cin >> a[cnt];
    while (cnt >= 2 && a[cnt - 1] <= a[cnt] && a[cnt - 1] <= a[cnt - 2]) {
      ans += min(a[cnt], a[cnt - 2]);
      cnt--;
      a[cnt] = a[cnt + 1];
    }
    cnt++;
  }
  sort(a, a + cnt);
  for (int i = 0; i < cnt - 2; ++i) {
    ans += a[i];
  }
  cout << ans;
}
