#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<pair<int, int> > ar;
  vector<int> act;
  int pr[1000000];
  int nx[1000000];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int awe;
    scanf("%d", &awe);
    act.push_back(awe);
    ar.push_back(pair<int, int>(awe, i));
  }
  for (int i = 1; i < n; i++) {
    pr[i] = i - 1;
    nx[i - 1] = i;
  }
  int low = 0;
  int high = n - 1;
  sort(ar.begin(), ar.end());
  long long ans = 0;
  for (int i = 0; i < n - 2; i++) {
    int x = ar[i].second;
    if (x == low) {
      ans += ar[i].first;
      low = nx[low];
    } else if (x == high) {
      ans += ar[i].first;
      high = pr[high];
    } else {
      int a = act[nx[x]];
      int b = act[pr[x]];
      ans += min(a, b);
      pr[nx[x]] = pr[x];
      nx[pr[x]] = nx[x];
    }
  }
  printf("%lld\n", ans);
  return 0;
}
