#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 1000 + 10;
int a[MAXN], b[MAXN], n[MAXN];
bool mark[MAXN];
set<pair<int, int> > st;
int main() {
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    b[i] = i - 1;
    n[i] = i + 1;
    st.insert(make_pair(a[i], i));
  }
  long long ans = 0;
  while (!st.empty()) {
    int i = st.begin()->second;
    st.erase(st.begin());
    if (i == 0 || i == m - 1) continue;
    if (a[i] <= a[b[i]] && a[i] <= a[n[i]]) {
      ans += min(a[b[i]], a[n[i]]);
      n[b[i]] = n[i];
      b[n[i]] = b[i];
      mark[i] = true;
    }
  }
  int mx[3] = {0, 0, 0};
  for (int i = 0; i < m; i++) {
    if (!mark[i]) {
      mx[2] = a[i];
      sort(mx, mx + 3, greater<int>());
      ans += a[i];
      mark[i] = true;
    }
  }
  ans -= (mx[0] + mx[1]);
  cout << ans << endl;
}
