#include <bits/stdc++.h>
using namespace std;
ifstream f("wow.in");
long long result = 0, D;
int a[500666], ind[500666];
set<int> SET;
set<int>::iterator it;
bool comp(int i1, int i2) { return a[i1] < a[i2]; }
void REMV(int pos) {
  it = SET.find(pos);
  ++it;
  int vdr = a[*it];
  --it;
  --it;
  int vst = a[*it];
  result += max(min(vdr - (int)D, vst - (int)D), 0);
  ++it;
  SET.erase(it);
}
int main() {
  int n, i;
  cin >> n;
  for (i = 1; i <= n; ++i) {
    cin >> a[i];
    SET.insert(i);
    ind[i] = i;
  }
  sort(ind + 1, ind + n + 1, comp);
  SET.insert(n + 1);
  SET.insert(0);
  for (i = 1; i <= n - 2; ++i) {
    if (a[ind[i]] > D) {
      result += (long long)((n - i + 1) - 2) * (long long)(a[ind[i]] - D);
      D = a[ind[i]];
      REMV(ind[i]);
    } else
      REMV(ind[i]);
  }
  cout << result << '\n';
  return 0;
}
