#include <bits/stdc++.h>
using namespace std;
int n;
long long ans;
vector<int> a, l, r;
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    q;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  a.resize(n);
  l.resize(n);
  r.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    q.push(make_pair(a[i], i));
    l[i] = i - 1;
    r[i] = i + 1;
  }
  ans = 0;
  long long cnt = n - 2, val = 0;
  while (cnt > 0) {
    cnt--;
    pair<int, int> p = q.top();
    q.pop();
    int x = p.second;
    if (l[x] == -1 || r[x] == n) {
      ans += (cnt + 1) * (a[x] - val);
      val = a[x];
    } else
      ans += min(a[l[x]], a[r[x]]) - val;
    if (r[x] != n) l[r[x]] = l[x];
    if (l[x] != -1) r[l[x]] = r[x];
  }
  cout << ans << endl;
  return 0;
}
