#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)n; ++i) cin >> a[i];
  vector<int> left(n), right(n);
  for (int i = 0; i < (int)n; ++i) left[i] = i - 1;
  for (int i = 0; i < (int)n; ++i) right[i] = i + 1;
  right[n - 1] = -1;
  long long rval = 0;
  vector<pair<int, int> > V;
  for (int i = 0; i < (int)n; ++i) V.push_back(make_pair(a[i], i));
  sort(V.begin(), V.end());
  int W = 0;
  int size = n;
  for (int pos = 0; pos < (int)V.size() - 2; ++pos) {
    pair<int, int> p = V[pos];
    int diff = p.first - W;
    int x = p.second;
    rval += (size - 2) * diff;
    size--;
    W = a[x];
    rval += max(0, min(((left[x]) != -1 ? a[(left[x])] : 0),
                       ((right[x]) != -1 ? a[(right[x])] : 0)) -
                       W);
    int l = left[x];
    int r = right[x];
    if (r != -1) left[r] = l;
    if (l != -1) right[l] = r;
  }
  cout << rval << endl;
}
