#include <bits/stdc++.h>
using namespace std;
long long int n, x[500090], ans, ba[500090], pr[500090];
priority_queue<pair<long long int, long long int>,
               vector<pair<long long int, long long int> >,
               greater<pair<long long int, long long int> > >
    pq;
int main() {
  scanf("%lld", &n);
  long long int l = 1, r = n, tag = 0;
  long long int sum = (r - l + 1) - 2;
  for (int i = 1; i < n + 1; i++)
    scanf("%lld", &x[i]), ba[i] = i + 1, pr[i] = i - 1;
  for (int i = 1; i < n + 1; i++) pq.push(make_pair(x[i], i));
  while (pq.size() > 2) {
    long long int v = pq.top().first, pos = pq.top().second;
    pq.pop();
    ans += sum * (v - tag), tag = v;
    if (pos == l)
      l = ba[pos];
    else if (pos == r)
      r = pr[pos];
    else {
      ba[pr[pos]] = ba[pos];
      pr[ba[pos]] = pr[pos];
      ans += min(x[ba[pos]], x[pr[pos]]) - tag;
    }
    sum--;
  }
  printf("%lld\n", ans);
  return 0;
}
