#include <bits/stdc++.h>
using namespace std;
struct node {
  int id, s;
} a[500010];
int l[500010], r[500010], b[500010], n, i, L, R;
long long ans;
bool cmp(const node& a, const node& b) { return a.s < b.s; }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i].s);
    a[i].id = i;
    l[i] = i - 1;
    r[i] = i + 1;
    b[i] = a[i].s;
  }
  sort(a + 1, a + n + 1, cmp);
  L = 1;
  R = n;
  for (i = 1; i <= n - 2; i++)
    if (a[i].id == L)
      ans += a[i].s, L = r[a[i].id];
    else if (a[i].id == R)
      ans += a[i].s, R = l[a[i].id];
    else {
      ans += min(b[l[a[i].id]], b[r[a[i].id]]);
      r[l[a[i].id]] = r[a[i].id];
      l[r[a[i].id]] = l[a[i].id];
    }
  printf("%I64d\n", ans);
  return 0;
}
