#include <bits/stdc++.h>
using namespace std;
template <class t>
t lcm(t a, t b) {
  return ((a * b) / __gcd(a, b));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long ans = 0;
  long long n, temp;
  cin >> n;
  pair<long long, long long> ip[n], org[n];
  long long prev[n], nxt[n], lcorner = 0, rcorner = n - 1;
  for (long long i = 0; i < n; i++) {
    cin >> ip[i].first;
    ip[i].second = i;
    prev[i] = i - 1;
    nxt[i] = i + 1;
    org[i] = ip[i];
  }
  sort(ip, ip + n);
  for (long long i = 0; i < (n - 2); i++) {
    pair<long long, long long> cur = ip[i];
    if (cur.second == lcorner) {
      lcorner = nxt[lcorner];
      ans += cur.first;
    } else if (cur.second == rcorner) {
      rcorner = prev[rcorner];
      ans += cur.first;
    } else {
      nxt[prev[cur.second]] = nxt[cur.second];
      prev[nxt[cur.second]] = prev[cur.second];
      ans += min(org[prev[cur.second]].first, org[nxt[cur.second]].first);
    }
  }
  cout << ans;
  return 0;
}
