#include <bits/stdc++.h>
using namespace std;
int n;
int idxs[500500];
long long vals[500500];
long long l[500500], r[500500];
bool cmp_idx(int a, int b) { return vals[a] < vals[b]; }
void rem(int idx) {
  l[r[idx]] = l[idx];
  r[l[idx]] = r[idx];
}
int main() {
  while (cin >> n) {
    for (int i = 1; i <= n; ++i) {
      cin >> vals[i];
      idxs[i] = i;
      l[i] = i - 1;
      r[i] = i + 1;
    }
    vals[0] = vals[n + 1] = 0;
    sort(idxs + 1, idxs + n + 1, cmp_idx);
    int l_idx = 1, r_idx = n;
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      int idx = idxs[i];
      ans += min(vals[l[idx]], vals[r[idx]]);
      if (l_idx == idx) {
        l_idx = r[idx];
      } else if (r_idx == idx) {
        r_idx = l[idx];
      } else {
        rem(idx);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
