Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. 

After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.

Input

The first line contains a single integer n (1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai (1 ≤ ai ≤ 106) — the values of the array elements.

Output

In a single line print a single integer — the maximum number of points Artem can get.

Examples

Input

5
3 1 5 2 6


Output

11


Input

5
1 2 3 4 5


Output

6


Input

5
1 100 101 100 1


Output

102
9
72 49 39 50 68 35 75 94 56
 4
2 3 1 2
 8
3 4 3 1 1 3 4 1
 1
4
 7
2 1 2 2 2 2 2
 6
1 7 3 1 6 2
 8
77 84 26 34 17 56 76 3
 2
93 51
 1
87
 4
86 21 58 60
 10
96 66 8 18 30 48 34 11 37 42
 6
46 30 38 9 65 23
 5
21 6 54 69 32
 3
1 2 1
 3
31 19 5
 10
4 2 2 4 1 2 2 4 2 1
 7
82 60 92 4 2 13 15
 9
4 5 2 2 3 1 3 3 5
 5
2 6 2 1 2
 2
3 1

435 4 15 0 10 12 279 0 0 118 299 145 74 1 5 21 129 23 6 0
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
long long comb(long long n, long long r) {
  if (r < 0 || r > n) return 0;
  if ((n - r) < r) r = n - r;
  long long vec = 1;
  for (long long i = n; i > n - r; --i) {
    vec = vec * i;
  }
  for (long long i = 1; i < r + 1; ++i) {
    vec = vec / i;
  }
  return vec;
}
template <typename T>
void press(T &v) {
  v.erase(unique(v.begin(), v.end()), v.end());
}
long long small(vector<long long> &vec, long long first) {
  return lower_bound(vec.begin(), vec.end(), first) - vec.begin();
}
long long osmall(vector<long long> &vec, long long first) {
  return upper_bound(vec.begin(), vec.end(), first) - vec.begin();
}
long long big(vector<long long> &vec, long long first) {
  return vec.size() - osmall(vec, first);
}
long long obig(vector<long long> &vec, long long first) {
  return vec.size() - small(vec, first);
}
long long freq(vector<long long> &vec, long long first) {
  return upper_bound(vec.begin(), vec.end(), first) -
         lower_bound(vec.begin(), vec.end(), first);
}
long long maxa(vector<long long> &vec) {
  return max_element(vec.begin(), vec.end()) - vec.begin();
}
long long mina(vector<long long> &vec) {
  return min_element(vec.begin(), vec.end()) - vec.begin();
}
long long Set(long long qq, long long pos) { return (qq | (1 << pos)); }
long long chk(long long qq, long long pos) { return (qq & (1 << pos)); }
long long bits(long long qq) { return __builtin_popcount(qq); }
long long p2[32];
long long modpow(long long first, long long n, long long m) {
  if (n == 0) return 1 % m;
  long long u = modpow(first, n / 2, m);
  u = (u * u) % m;
  if (n % 2 == 1) u = (u * first) % m;
  return u;
}
long long _pow(long long vec, long long b) {
  if (!b) return 1;
  long long temp = _pow(vec, b / 2);
  temp = (temp * temp);
  if (b % 2) return (vec * temp);
  return temp;
}
long long cl(long long vec, long long b) {
  if (vec % b) return 1 + vec / b;
  return vec / b;
}
long long dx[] = {0, 0, -1, 1};
long long dy[] = {1, -1, 0, 0};
const long long MOD = 1e9 + 7;
const long long inf = 1e18;
const long long N = 1e4 + 5;
const long long K = 1e3 + 10;
void solve() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  if (n <= 2) {
    cout << 0 << endl;
    return;
  }
  long long p[n];
  long long nx[n];
  set<pair<long long, long long> > s;
  set<long long> fin;
  for (long long i = 1; i < n - 1; i++) {
    p[i] = i + 1;
    nx[i] = i - 1;
    if (v[i - 1] > v[i] && v[i] < v[i + 1]) s.insert(make_pair(v[i], i));
  }
  nx[0] = 1;
  p[n - 1] = n - 2;
  long long ans = 0;
  for (long long i = 0; i < n; i++) fin.insert(i);
  while (s.size() > 0) {
    auto j = *s.begin();
    long long i = j.second;
    ans += min(v[p[i]], v[nx[i]]);
    nx[p[i]] = nx[i];
    p[nx[i]] = p[i];
    s.erase(j);
    fin.erase(i);
    if (p[i] == 0 || nx[i] == n - 1) continue;
    if (v[p[i]] < v[nx[i]] && v[p[i]] < v[p[p[i]]])
      s.insert(make_pair(v[p[i]], p[i]));
    if (v[nx[i]] < v[p[i]] && v[nx[i]] < v[nx[nx[i]]])
      s.insert(make_pair(v[nx[i]], nx[i]));
  }
  vector<long long> res;
  for (auto i : fin) res.push_back(v[i]);
  sort(res.begin(), res.end());
  for (long long i = 0; i < res.size() - 2; i++) ans += res[i];
  cout << ans << endl;
}
signed main() {
  for (long long i = 0; i < 32; i++) p2[i] = (1 << i);
  cout << fixed << setprecision(12);
  long long t;
  t = 1;
  for (long long i = 1; i <= t; i++) {
    solve();
  }
}
