#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
int N;
int A[500111];
pair<int, int> P[500111];
set<int> S;
int main() {
  scanf("%d", &N);
  for (int i = 0, i_len = (N); i < i_len; ++i) scanf("%d", A + i);
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    P[i] = make_pair(A[i], i);
    S.insert(i);
  }
  long long ans = 0;
  long long offset = 0;
  sort(P, P + N);
  for (int i = 0, i_len = (N - 2); i < i_len; ++i) {
    long long height = P[i].first - offset;
    offset = P[i].first;
    ans += height * ((int)S.size() - 2);
    int k = P[i].second;
    set<int>::iterator it = S.find(k);
    if (it != S.begin()) {
      int tmp = A[*prev(it)];
      ++it;
      if (it != S.end()) {
        amin(tmp, A[*it]);
        ans += tmp - offset;
      }
    }
    S.erase(k);
  }
  printf("%lld\n", ans);
  return 0;
}
