#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, -1, 0, 1, -1, 1, 1, -1};
template <class T>
inline bool get(T& n) {
  char c, mul = 1;
  n = 0;
  while (!isdigit(c = getchar_unlocked()) && (c != EOF) && (c != '-'))
    ;
  if (c == EOF) return false;
  if (c == '-') {
    c = getchar_unlocked();
    mul = -1;
  }
  for (; isdigit(c); c = getchar_unlocked()) n = n * 10 + c - '0';
  n *= mul;
  return true;
}
inline void get(char& c) {
  while (isspace(c = getchar_unlocked()))
    ;
}
int n;
int v[500050];
int l[500050], r[500050];
set<pair<int, int> > s;
int main() {
  get(n);
  for (int(i) = 0, _ = (n); (i) < _; ++i) {
    get(v[i]);
    s.insert(make_pair(v[i], i));
    l[i] = i - 1;
    r[i] = i + 1;
  }
  r[n - 1] = -1;
  long long ans = 0;
  int prev = 0;
  while ((int)s.size() > 1) {
    int pos = s.begin()->second;
    int val = s.begin()->first;
    s.erase(s.begin());
    int left = l[pos], right = r[pos];
    if (left >= 0 && right >= 0) ans += min(v[left], v[right]) - val;
    ans += (val - prev) * ((int)s.size() - 1);
    if (left >= 0) r[left] = right;
    if (right >= 0) l[right] = left;
    prev = val;
  }
  printf("%I64d\n", ans);
  return 0;
}
