#include <bits/stdc++.h>
using namespace std;
const int NMAX = 5e5;
int stiva[NMAX + 2];
int top = 0;
int N;
long long ans = 0;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    int x;
    cin >> x;
    while (top > 1 && stiva[top - 1] >= stiva[top] && stiva[top] <= x) {
      ans += min(stiva[top - 1], x);
      --top;
    }
    stiva[++top] = x;
  }
  sort(stiva + 1, stiva + top + 1);
  for (int i = 1; i <= top - 2; ++i) ans += stiva[i];
  cout << ans << '\n';
  return 0;
}
