#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
const long long MAXN = 1e6 + 10;
const long long INF = 8e18;
const long long MOD = 1e9 + 7;
pair<long long, long long> A[MAXN];
long long n;
set<pair<long long, long long> > st;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i].first;
    A[i].second = i;
    st.insert({i, A[i].first});
  }
  sort(A, A + n);
  long long ans = 0, val = 0;
  long long rem = n;
  for (int i = 0; i < n - 2; i++) {
    auto it = st.find({A[i].second, A[i].first});
    auto ite = st.end();
    ite--;
    if (it == st.begin() || it == ite) {
      ans += (A[i].first - val) * (rem - 2);
      val += (A[i].first - val);
      st.erase(it);
      rem--;
    } else {
      auto nxt = it, prv = it;
      nxt++;
      prv--;
      ans += min(nxt->second, prv->second) - val;
      st.erase(it);
      rem--;
    }
  }
  cout << ans << '\n';
  return 0;
}
