#include <bits/stdc++.h>
using namespace std;
int tree[1050000];
int a[500010];
int l[500010], r[500010], first, last;
void ini(int s, int e, int node) {
  if (s == e) {
    tree[node] = s;
    return;
  }
  int mid = (s + e) >> 1, lft = node << 1, rht = (node << 1) | 1;
  ini(s, mid, lft);
  ini(mid + 1, e, rht);
  tree[node] = a[tree[lft]] < a[tree[rht]] ? tree[lft] : tree[rht];
  return;
}
void update(int s, int e, int node, int ix) {
  if (s == e) {
    a[s] = 100000000;
    return;
  }
  int mid = (s + e) >> 1, lft = node << 1, rht = (node << 1) | 1;
  if (mid >= ix)
    update(s, mid, lft, ix);
  else
    update(mid + 1, e, rht, ix);
  tree[node] = a[tree[lft]] < a[tree[rht]] ? tree[lft] : tree[rht];
  return;
}
int main() {
  int n;
  scanf("%d", &n);
  first = 1, last = n;
  for (int i = 1; i <= n; i++) scanf("%d", a + i), l[i] = i - 1, r[i] = i + 1;
  ini(1, n, 1);
  long long ans = 0;
  int m = n - 2;
  while (m--) {
    if (m < 0) break;
    int ix = tree[1];
    if (ix == first) {
      ans += a[ix];
      first = r[ix];
    } else if (ix == last) {
      ans += a[ix];
      last = l[ix];
    } else {
      ans += min(a[l[ix]], a[r[ix]]);
      r[l[ix]] = r[ix];
      l[r[ix]] = l[ix];
    }
    update(1, n, 1, ix);
  }
  cout << ans << endl;
}
