#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, l[N], r[N], tl, tr, a[N], ind[N];
long long ans;
inline void update(int x) {
  r[l[x]] = r[x];
  l[r[x]] = l[x];
  if (x == tl) {
    tl = r[x];
  } else if (x == tr) {
    tr = l[x];
  }
}
inline bool cmp(int x, int y) { return a[x] < a[y]; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    l[i] = i - 1, r[i] = i + 1;
    ind[i] = i;
  }
  sort(ind + 1, ind + n + 1, cmp);
  tl = 1, tr = n;
  for (int i = 1; i <= n; i++) {
    if (tl == tr || r[tl] == tr) {
      break;
    }
    if (ind[i] == tl || ind[i] == tr) {
      ans += a[ind[i]];
    } else {
      ans += min(a[l[ind[i]]], a[r[ind[i]]]);
    }
    update(ind[i]);
  }
  cout << ans;
}
