#include <bits/stdc++.h>
using namespace std;
int N, A[555555];
int Next[555555], Prev[555555];
queue<int> q;
long long ans;
bool dip(int i) {
  return i != 0 && i != N - 1 && A[Prev[i]] > A[i] && A[Next[i]] >= A[i];
}
int main(int argc, char *argv[]) {
  scanf("%d", &N);
  for (int i = 0; i < (N); i++) scanf("%d", A + i);
  if (N == 1) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < (N); i++) {
    Next[i] = i + 1;
    Prev[i] = i - 1;
  }
  for (int i = 0; i < (N); i++)
    if (dip(i)) q.push(i);
  while (!q.empty()) {
    int n = q.front(), l = Prev[n], r = Next[n];
    q.pop();
    ans += min(A[l], A[r]);
    Prev[r] = l;
    Next[l] = r;
    if (dip(l)) q.push(l);
    if (dip(r)) q.push(r);
  }
  vector<int> v;
  int p = 0;
  while (p != N) {
    v.push_back(p);
    p = Next[p];
  }
  int l = 0, r = (int)v.size() - 1;
  while (l + 1 != r) {
    ans += min(A[v[l]], A[v[r]]);
    if (min(A[v[l]], A[v[r - 1]]) > min(A[v[l + 1]], A[v[r]]))
      r--;
    else
      l++;
  }
  cout << ans << endl;
  return 0;
}
