#include <bits/stdc++.h>
using namespace std;
const int N = 505000;
int n, a[N], l[N], r[N];
pair<int, int> z[N];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    z[i] = make_pair(a[i], i);
  }
  sort(z, z + n);
  for (int i = 0; i < n; ++i) {
    l[i] = i - 1;
    r[i] = i + 1;
  }
  int low = 0, high = n - 1;
  long long ans = 0;
  for (int i = 0; i < n - 2; ++i) {
    int id = z[i].second;
    if (id == low) {
      ans += z[i].first;
      low = r[low];
      continue;
    }
    if (id == high) {
      ans += z[i].first;
      high = l[high];
      continue;
    }
    ans += min(a[l[id]], a[r[id]]);
    l[r[id]] = l[id];
    r[l[id]] = r[id];
  }
  printf("%I64d\n", ans);
  return 0;
}
