#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() {
  vector<long long> v;
  long long n, ans = 0;
  cin >> n;
  if (n == 1) {
    cin >> n;
    cout << 0 << endl;
    return;
  }
  for (long long i = 0; i < n; i++) {
    long long val;
    long long s = v.size();
    cin >> val;
    while (s > 1 && v[s - 1] <= val && v[s - 1] <= v[s - 2]) {
      ans += min(val, v[s - 2]);
      v.pop_back();
      s--;
    }
    v.push_back(val);
  }
  sort(v.begin(), v.end());
  for (long long i = 0; i < v.size() - 2; i++) ans += v[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();
  }
}

### Prompt

Your task is to create a CPP solution to the following problem:

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

### Response

```cpp
#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() {
  vector<long long> v;
  long long n, ans = 0;
  cin >> n;
  if (n == 1) {
    cin >> n;
    cout << 0 << endl;
    return;
  }
  for (long long i = 0; i < n; i++) {
    long long val;
    long long s = v.size();
    cin >> val;
    while (s > 1 && v[s - 1] <= val && v[s - 1] <= v[s - 2]) {
      ans += min(val, v[s - 2]);
      v.pop_back();
      s--;
    }
    v.push_back(val);
  }
  sort(v.begin(), v.end());
  for (long long i = 0; i < v.size() - 2; i++) ans += v[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();
  }
}

```