#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
bool smax(T &x, L y) {
  return x < y ? (x = y, 1) : 0;
}
template <class T, class L>
bool smin(T &x, L y) {
  return y < x ? (x = y, 1) : 0;
}
const int maxn = 5e5 + 17;
list<int> a;
list<int>::iterator per[maxn];
int n;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 0, x; i < n; i++) cin >> x, a.push_back(x), per[i] = --a.end();
  sort(per, per + n,
       [](list<int>::iterator &i, list<int>::iterator &j) { return *i < *j; });
  long long ans = 0, dam = 0;
  for_each(per, per + max(0, n - 2), [&](list<int>::iterator &i) {
    if (i == a.begin() || i == --a.end()) {
      ans += (a.size() - 2) * (*i - dam);
      dam = *i;
    } else {
      auto prv = i, nxt = i;
      prv--, nxt++;
      ans += min(*prv, *nxt) - dam;
    }
    a.erase(i);
  });
  cout << ans << '\n';
  return 0;
}
