#include <bits/stdc++.h>
using namespace std;
double _st;
const int N = 1234567;
int a[N];
pair<int, int> p[N];
int pr[N], ne[N];
void solve() {
  int n;
  cin >> n;
  for (__typeof(n) i = 0; i < (n); i++) {
    cin >> a[i];
    p[i] = make_pair(a[i], i);
  }
  sort(p, p + n);
  for (__typeof(n) i = 0; i < (n); i++) {
    pr[i] = i - 1;
    ne[i] = i + 1;
  }
  int low = 0, high = n - 1;
  long long ans = 0;
  for (__typeof(n - 2) i = 0; i < (n - 2); i++) {
    int j = p[i].second;
    if (j == low) {
      ans += p[i].first;
      low = ne[low];
      continue;
    }
    if (j == high) {
      ans += p[i].first;
      high = pr[high];
      continue;
    }
    int x = a[pr[j]];
    int y = a[ne[j]];
    if (x < y)
      ans += x;
    else
      ans += y;
    pr[ne[j]] = pr[j];
    ne[pr[j]] = ne[j];
  }
  cout << ans;
}
void pre() { solve(); }
void end() {}
int main() {
  pre();
  end();
  return 0;
}
