#include <bits/stdc++.h>
using namespace std;
const int L = 5e5 + 5;
int A[L], pre[L], ne[L];
bool in[L];
queue<int> Q;
void check(int i) {
  if (in[i]) return;
  if (A[i] <= A[pre[i]] && A[i] <= A[ne[i]]) in[i] = true, Q.push(i);
}
int main() {
  std::ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 1; i <= (int)(N); ++i)
    cin >> A[i], pre[i] = i - 1, ne[i] = i + 1;
  ne[0] = 1, pre[N + 1] = N;
  long long ans = 0;
  for (int i = 1; i <= (int)(N); ++i) check(i);
  while (!Q.empty()) {
    int c = Q.front();
    Q.pop();
    ne[pre[c]] = ne[c];
    pre[ne[c]] = pre[c];
    ans += min(A[pre[c]], A[ne[c]]);
    check(pre[c]), check(ne[c]);
  }
  int c = ne[0];
  while (c != N + 1) ans += min(A[pre[c]], A[ne[c]]), c = ne[c];
  cout << ans << endl;
  return 0;
}
