#include <bits/stdc++.h>
using namespace std;
long long arr[500010];
int left_ind[500010], right_ind[500010];
int main() {
  int numbers;
  cin >> numbers;
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      ref;
  for (int i{1}; i <= numbers; ++i) {
    cin >> arr[i];
    ref.push(pair<long long, int>(arr[i], i));
    left_ind[i] = i - 1;
    right_ind[i] = i + 1;
  }
  long long output = 0;
  int left = 1, right = numbers;
  while (ref.size() > 2) {
    pair<long long, int> it = ref.top();
    ref.pop();
    if (it.second == left || it.second == right) {
      output += it.first;
      if (it.second == left)
        left = right_ind[left];
      else
        right = left_ind[right];
    } else
      output += min(arr[left_ind[it.second]], arr[right_ind[it.second]]);
    right_ind[left_ind[it.second]] = right_ind[it.second];
    left_ind[right_ind[it.second]] = left_ind[it.second];
  }
  cout << output;
  return 0;
}
