#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
template <class T>
typename T::value_type arr_sum(const T& v, int n) {
  typename T::value_type sum = 0;
  for (int i = (0); i < (n); ++i) sum += v[i];
  return sum;
}
struct Sync_stdio {
  Sync_stdio() {
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
  }
} _sync_stdio;
int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 0;
    return 0;
  }
  list<int> a;
  a.push_back(0);
  for (int i = (0); i < (n); ++i) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  a.push_back(0);
  auto i = a.begin();
  auto j = i;
  ++j;
  auto k = j;
  ++k;
  long long res = 0;
  while (k != a.end()) {
    if (*i >= *j && *j <= *k) {
      res += min(*i, *k);
      a.erase(j);
      j = i;
      --i;
    } else {
      ++i;
      ++j;
      ++k;
    }
  }
  i = a.begin();
  j = i;
  ++j;
  k = j;
  ++k;
  a.pop_back();
  a.pop_front();
  while (a.size() >= 3) {
    if (a.front() < a.back()) {
      res += a.front();
      a.pop_front();
    } else {
      res += a.back();
      a.pop_back();
    }
  }
  cout << res;
}
