#include <bits/stdc++.h>
using namespace std;
void _fill_int(int* p, int val, int rep) {
  int i;
  for (i = 0; i < rep; i++) p[i] = val;
}
int N;
vector<int> V;
void solve() {
  int f, i, j, k, l, x, y;
  signed long long ret = 0;
  cin >> N;
  for (i = 0; i < N; i++) {
    cin >> x;
    while (V.size() >= 2 && V[V.size() - 2] >= V[V.size() - 1] &&
           x >= V[V.size() - 1]) {
      ret += min(V[V.size() - 2], x);
      V.pop_back();
    }
    V.push_back(x);
  }
  sort(V.begin(), V.end());
  for (i = 1; i < V.size() - 1; i++) ret += V[i - 1];
  cout << ret << endl;
}
int main(int argc, char** argv) {
  string s;
  if (argc == 1) ios::sync_with_stdio(false);
  for (int i = 1; i < argc; i++) s += argv[i], s += '\n';
  for (int i = s.size() - 1; i >= 0; i--) ungetc(s[i], stdin);
  solve();
  return 0;
}
