#include <bits/stdc++.h>
using namespace std;
int N;
int A[500002], ST[500002];
int main() {
  cin.sync_with_stdio(false);
  cin >> N;
  for (int i = 1; i <= N; ++i) cin >> A[i];
  long long result = 0;
  for (int i = 1; i <= N; ++i) {
    while (ST[0] >= 2 && A[ST[ST[0] - 1]] >= A[ST[ST[0]]] &&
           A[ST[ST[0]]] <= A[i]) {
      result += min(A[ST[ST[0] - 1]], A[i]);
      --ST[0];
    }
    ST[++ST[0]] = i;
  }
  for (int i = 1; i <= ST[0] - 2; ++i) result += min(A[ST[i]], A[ST[i + 2]]);
  cout << result << '\n';
}
