#include <bits/stdc++.h>
using namespace std;
int n, a[500005], id[500005], lef[500005], rig[500005];
long long ans;
int cmp(int i, int j) { return a[i] < a[j]; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    id[i] = i;
    lef[i] = i - 1;
    rig[i] = i + 1;
  }
  rig[0] = 1;
  lef[n + 1] = n;
  sort(id + 1, id + n + 1, cmp);
  ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += max(0, min(a[lef[id[i]]] - a[id[i]], a[rig[id[i]]] - a[id[i]]));
    rig[lef[id[i]]] = rig[id[i]];
    lef[rig[id[i]]] = lef[id[i]];
    if (i < n - 1) {
      ans += a[id[i]];
    }
  }
  cout << ans << "\n";
  return 0;
}
