#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, num, val, pos;
  long long int ans = 0;
  vector<pair<int, int> > V;
  cin >> n;
  int arr[n], left[n], right[n];
  for (i = 0; i < n; i++) {
    cin >> num;
    arr[i] = num;
    left[i] = i - 1;
    right[i] = i + 1;
    V.push_back(make_pair(num, i));
  }
  sort(V.begin(), V.end());
  for (i = 0; i < n; i++) {
    val = V[i].first;
    pos = V[i].second;
    if (pos == 0 || pos == n - 1) continue;
    ans += min(arr[left[pos]], arr[right[pos]]);
    if (arr[right[left[pos]]] < arr[right[pos]]) {
      right[left[pos]] = right[pos];
    }
    if (arr[left[right[pos]]] < arr[left[pos]]) {
      left[right[pos]] = left[pos];
    }
  }
  cout << ans << endl;
  return 0;
}
