#include <bits/stdc++.h>
using namespace std;
int n, t, k, a[550006], cur;
pair<int, int> b[500005], c[500005];
int main(int argc, char *argv[]) {
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    b[i].first = a[i];
    b[i].second = i;
    c[i].first = i - 1;
    c[i].second = i + 1;
  }
  sort(b + 1, b + 1 + n);
  for (int i = 1; i <= n - 2; ++i) {
    cur = b[i].second;
    t = min(a[c[cur].first], a[c[cur].second]);
    if (t)
      ans += t;
    else
      ans += a[cur];
    c[c[cur].second].first = c[cur].first;
    c[c[cur].first].second = c[cur].second;
  }
  cout << ans << endl;
}
