#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
long long int n, a[maxn], nx[maxn], pr[maxn];
vector<pair<long long int, long long int> > v;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    nx[i] = i + 1;
    pr[i] = i - 1;
    v.push_back(make_pair(a[i], i));
  }
  long long int l = 0, r = n - 1;
  long long int answer = 0;
  sort(v.begin(), v.end());
  for (int i = 0; i < n - 2; i++) {
    long long int x = v[i].first;
    long long int j = v[i].second;
    if (j == l) {
      answer += x;
      l = nx[j];
    } else if (j == r) {
      answer += x;
      r = pr[j];
    } else {
      answer += min(a[pr[j]], a[nx[j]]);
      nx[pr[j]] = nx[j];
      pr[nx[j]] = pr[j];
    }
  }
  cout << answer;
}
