#include <bits/stdc++.h>
using namespace std;
struct yo {
  int val;
  int id;
  int next, prev;
};
vector<yo> a0, a;
int n, n0, x;
bool operator<(yo a, yo b) { return a.val < b.val; }
int main() {
  cin >> n;
  n0 = n;
  if (n <= 2) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> x;
    yo cur;
    cur.val = x;
    cur.id = i;
    cur.next = i + 1;
    cur.prev = i - 1;
    a0.push_back(cur);
    a.push_back(cur);
  }
  sort(a.begin(), a.end());
  long long v = 0, ans = 0;
  for (int i = 0; i < a.size() - 2; i++) {
    int cur_id = a[i].id;
    if (a0[cur_id].prev == -1) {
      a0[a0[cur_id].next].prev = -1;
      ans += (n - 2) * (a[i].val - v);
      v += (a[i].val - v);
    } else if (a0[cur_id].next == n0) {
      a0[a0[cur_id].prev].next = n0;
      ans += (n - 2) * (a[i].val - v);
      v += (a[i].val - v);
    } else {
      ans += min(a0[a0[cur_id].prev].val, a0[a0[cur_id].next].val) - v;
      a0[a0[cur_id].next].prev = a0[cur_id].prev;
      a0[a0[cur_id].prev].next = a0[cur_id].next;
    }
    n--;
  }
  cout << ans;
}
