#include <bits/stdc++.h>
using namespace std;
int n;
int in[10000009];
int lef[1000009], ri[1000009];
int fixx[1000009], val[1000009];
vector<pair<int, int> > data;
int main() {
  scanf("%d", &n);
  if (n == 1) {
    scanf("%d", &n);
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < (int)n; i++) {
    scanf("%d", &in[i]);
    ;
    fixx[i] = 0;
    val[i] = 0;
    data.push_back(make_pair(in[i], i));
  }
  for (int i = 0; i < (int)n; i++) {
    if (i == 0) {
      lef[i] = -1;
      ri[i] = i + 1;
    } else if (i == n - 1) {
      ri[i] = -1;
      lef[i] = i - 1;
    } else {
      lef[i] = i - 1;
      ri[i] = i + 1;
    }
  }
  sort(data.begin(), data.end());
  long long sum = 0;
  for (int i = 0; i < (int)n; i++) {
    int x = data[i].first;
    int pos = data[i].second;
    if (pos == 0 || pos == n - 1) {
      fixx[pos] = 1;
      val[pos] = 0;
      continue;
    }
    if (fixx[ri[pos]] == 1 || fixx[lef[pos]] == 1) {
      fixx[pos] = 1;
      val[pos] = min(in[ri[pos]], in[lef[pos]]);
      continue;
    }
    sum += min(in[ri[pos]], in[lef[pos]]);
    ri[lef[pos]] = ri[pos];
    lef[ri[pos]] = lef[pos];
  }
  for (int i = 0; i < (int)n; i++) {
    if (fixx[i] == 1) {
      sum += val[i];
    }
  }
  cout << sum << endl;
}
