#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)5e5 + 5;
vector<int> v;
int main(void) {
  int n;
  int x;
  long long int res = 0;
  v.push_back(0);
  scanf(" %d", &n);
  for (int i = 1; i <= n; i++) {
    scanf(" %d", &x);
    while ((int)v.size() >= 2 && v[(int)v.size() - 2] >= v.back() &&
           v.back() <= x) {
      v.pop_back();
      res += min(x, v.back());
    }
    v.push_back(x);
  }
  int l = 1;
  int r = (int)v.size() - 1;
  while (l <= r - 2) {
    if (v[l] <= v[r]) {
      res += v[l++];
    } else {
      res += v[r--];
    }
  }
  printf("%lld\n", res);
  return 0;
}
