#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
int a[501010], nxt[501010], pre[501010];
pair<int, int> p[501010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 1; i <= n; i++) {
    p[i] = pair<int, int>(a[i], i);
    nxt[i] = i + 1, pre[i] = i - 1;
  }
  sort(p + 1, p + n + 1);
  long long ans = 0;
  int st = 1, ed = n;
  for (int i = 1; i < n - 1; i++) {
    int v = p[i].second;
    if (v == st) {
      ans += p[i].first;
      st = nxt[st];
    } else if (v == ed) {
      ans += p[i].first;
      ed = pre[ed];
    } else {
      ans += min(a[pre[v]], a[nxt[v]]);
      pre[nxt[v]] = pre[v];
      nxt[pre[v]] = nxt[v];
    }
  }
  cout << ans << endl;
  return 0;
}
