#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = 2 * acos((long double)0);
long long gcd(long long a, long long b) {
  long long r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long fpow(long long n, long long k, int p = MOD) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
void addmod(int& a, int val, int p = MOD) {
  if ((a = (a + val)) >= p) a -= p;
}
void submod(int& a, int val, int p = MOD) {
  if ((a = (a - val)) < 0) a += p;
}
int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
const int maxn = 500010;
int n;
int a[maxn];
void solve() {
  cin >> n;
  set<pair<int, int> > st1, st2;
  for (int i = (0); i < (n); i++) {
    cin >> a[i];
    st1.insert(make_pair(i, a[i]));
    st2.insert(make_pair(a[i], i));
  }
  long long ans = 0;
  for (int i = (0); i < (n - 2); i++) {
    int u = st2.begin()->second;
    st2.erase(st2.begin());
    set<pair<int, int> >::iterator it = st1.find(make_pair(u, a[u]));
    if (it == st1.begin() || it->first == st1.rbegin()->first) {
      ans += a[u];
    } else {
      it--;
      int x = it->first;
      it++;
      it++;
      int y = it->first;
      ans += min(a[x], a[y]);
    }
    st1.erase(make_pair(u, a[u]));
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
