#include <bits/stdc++.h>
using namespace std;
const int NMAX = 500010;
int N, V[NMAX], Prev[NMAX], Next[NMAX];
long long Ans;
queue<int> Q;
bool Added[NMAX];
void Add(int X) {
  if (Added[X]) return;
  if (V[Prev[X]] >= V[X] && V[X] <= V[Next[X]]) {
    Q.push(X);
    Added[X] = 1;
  }
}
int main() {
  scanf("%i", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%i", &V[i]);
    Prev[i] = i - 1;
    Next[i] = i + 1;
  }
  Next[0] = 1;
  Prev[N + 1] = N;
  for (int i = 2; i < N; ++i) Add(i);
  while (!Q.empty()) {
    int X = Q.front();
    Q.pop();
    Next[Prev[X]] = Next[X];
    Prev[Next[X]] = Prev[X];
    Ans += min(V[Prev[X]], V[Next[X]]);
    Add(Prev[X]);
    Add(Next[X]);
  }
  for (int X = 0; X != N + 1; X = Next[X]) Ans += min(V[Prev[X]], V[Next[X]]);
  printf("%I64d", Ans);
}
