#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 4e18;
const int inf = 2e9;
const int N = 5e5 + 5;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int ne[N], pr[N], a[N];
queue<int> q;
bool done[N];
void check(int i) {
  if (done[i]) return;
  if (a[pr[i]] >= a[i] && a[i] <= a[ne[i]]) {
    q.push(i);
    done[i] = 1;
  }
}
int main(void) {
  int n, i;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    pr[i] = i - 1;
    ne[i] = i + 1;
  }
  ne[0] = 1;
  pr[n + 1] = n;
  for (i = 1; i <= n; ++i) check(i);
  long long ans = 0;
  while (q.size()) {
    int now = q.front();
    q.pop();
    ne[pr[now]] = ne[now];
    pr[ne[now]] = pr[now];
    ans += 1LL * min(a[ne[now]], a[pr[now]]);
    check(ne[now]);
    check(pr[now]);
  }
  int now = ne[0];
  while (now <= n) {
    ans += 1LL * min(a[pr[now]], a[ne[now]]);
    now = ne[now];
  }
  cout << ans << endl;
  return 0;
}
