#include <bits/stdc++.h>
using namespace std;
template <typename T>
T BigMod(T b, T p, T m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T>
T ModInv(T b, T m) {
  return BigMod(b, m - 2, m);
}
template <typename T>
void ia(T a[], int n) {
  for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void pa(T a[], int n) {
  for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T printf) {
  if (printf == 0) return 1;
  if (printf & 1)
    return B * POW(B, printf - 1);
  else
    return (POW(B, printf / 2) * POW(B, printf / 2));
}
template <typename T>
inline T Bigmod(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
inline T Dis(T x1, T y1, T x2, T y2) {
  return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
inline T Angle(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
inline T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T>
inline T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
inline T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <typename T>
inline T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
int toInt(string s) {
  int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int toLlint(string s) {
  long long int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int cdigittoint(char ch) { return ch - '0'; }
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int tree[5 * 500007][2];
int Lft[5 * 500007], Rgt[5 * 500007], mp[500007];
int a[500007];
void combined(int idx, int lft, int rgt) {
  if (tree[lft][0] < tree[rgt][0]) {
    tree[idx][0] = tree[lft][0];
    tree[idx][1] = tree[lft][1];
  } else {
    tree[idx][0] = tree[rgt][0];
    tree[idx][1] = tree[rgt][1];
  }
}
void build(int idx, int l, int r) {
  if (l == r) {
    Lft[idx] = l - 1;
    Rgt[idx] = l + 1;
    mp[l] = idx;
    tree[idx][0] = a[l];
    tree[idx][1] = l;
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  build(lft, l, mid);
  build(rgt, mid + 1, r);
  combined(idx, lft, rgt);
}
void del(int idx, int l, int r, int pos) {
  if (l == r) {
    tree[idx][0] = (1 << 28);
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  if (pos <= mid)
    del(lft, l, mid, pos);
  else
    del(rgt, mid + 1, r, pos);
  combined(idx, lft, rgt);
}
void updateR(int idx, int l, int r, int pos, int val) {
  if (l == r) {
    Rgt[idx] = val;
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  if (pos <= mid)
    updateR(lft, l, mid, pos, val);
  else
    updateR(rgt, mid + 1, r, pos, val);
  combined(idx, lft, rgt);
}
void updateL(int idx, int l, int r, int pos, int val) {
  if (l == r) {
    Lft[idx] = val;
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  if (pos <= mid)
    updateL(lft, l, mid, pos, val);
  else
    updateL(rgt, mid + 1, r, pos, val);
  combined(idx, lft, rgt);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  build(1, 1, n);
  long long int ans = 0;
  for (int i = 1; i <= n - 2; i++) {
    int id = tree[1][1];
    del(1, 1, n, id);
    int id2 = mp[id];
    int ladj = Lft[id2];
    int radj = Rgt[id2];
    if ((a[ladj] < a[radj] ? a[ladj] : a[radj]) == 0)
      ans += a[id];
    else
      ans += (a[ladj] < a[radj] ? a[ladj] : a[radj]);
    if (ladj) updateR(1, 1, n, ladj, radj);
    if (radj <= n) updateL(1, 1, n, radj, ladj);
  }
  printf("%lld\n", ans);
  return 0;
}

### Prompt

Please formulate 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;
template <typename T>
T BigMod(T b, T p, T m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    T s = BigMod(b, p / 2, m);
    return ((s % m) * (s % m)) % m;
  }
  return ((b % m) * (BigMod(b, p - 1, m) % m)) % m;
}
template <typename T>
T ModInv(T b, T m) {
  return BigMod(b, m - 2, m);
}
template <typename T>
void ia(T a[], int n) {
  for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void pa(T a[], int n) {
  for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
template <typename T>
long long int isLeft(T a, T b, T c) {
  return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y);
}
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T printf) {
  if (printf == 0) return 1;
  if (printf & 1)
    return B * POW(B, printf - 1);
  else
    return (POW(B, printf / 2) * POW(B, printf / 2));
}
template <typename T>
inline T Bigmod(T b, T p, T m) {
  if (p == 0)
    return 1;
  else if (!(p & 1))
    return (Bigmod(b, p / 2, m) * Bigmod(b, p / 2, m)) % m;
  else
    return ((b % m) * Bigmod(b, p - 1, m)) % m;
}
template <typename T>
inline T Dis(T x1, T y1, T x2, T y2) {
  return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2));
}
template <typename T>
inline T Angle(T x1, T y1, T x2, T y2) {
  return atan(double(y1 - y2) / double(x1 - x2));
}
template <typename T>
inline T DIFF(T a, T b) {
  T d = a - b;
  if (d < 0)
    return -d;
  else
    return d;
}
template <typename T>
inline T ABS(T a) {
  if (a < 0)
    return -a;
  else
    return a;
}
template <typename T>
inline T gcd(T a, T b) {
  if (a < 0) return gcd(-a, b);
  if (b < 0) return gcd(a, -b);
  return (b == 0) ? a : gcd(b, a % b);
}
template <typename T>
inline T lcm(T a, T b) {
  if (a < 0) return lcm(-a, b);
  if (b < 0) return lcm(a, -b);
  return a * (b / gcd(a, b));
}
template <typename T>
inline T euclide(T a, T b, T &x, T &y) {
  if (a < 0) {
    T d = euclide(-a, b, x, y);
    x = -x;
    return d;
  }
  if (b < 0) {
    T d = euclide(a, -b, x, y);
    y = -y;
    return d;
  }
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  } else {
    T d = euclide(b, a % b, x, y);
    T t = x;
    x = y;
    y = t - (a / b) * y;
    return d;
  }
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Check(int N, int pos) { return (bool)(N & (1 << pos)); }
int toInt(string s) {
  int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int toLlint(string s) {
  long long int sm;
  stringstream ss(s);
  ss >> sm;
  return sm;
}
int cdigittoint(char ch) { return ch - '0'; }
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int tree[5 * 500007][2];
int Lft[5 * 500007], Rgt[5 * 500007], mp[500007];
int a[500007];
void combined(int idx, int lft, int rgt) {
  if (tree[lft][0] < tree[rgt][0]) {
    tree[idx][0] = tree[lft][0];
    tree[idx][1] = tree[lft][1];
  } else {
    tree[idx][0] = tree[rgt][0];
    tree[idx][1] = tree[rgt][1];
  }
}
void build(int idx, int l, int r) {
  if (l == r) {
    Lft[idx] = l - 1;
    Rgt[idx] = l + 1;
    mp[l] = idx;
    tree[idx][0] = a[l];
    tree[idx][1] = l;
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  build(lft, l, mid);
  build(rgt, mid + 1, r);
  combined(idx, lft, rgt);
}
void del(int idx, int l, int r, int pos) {
  if (l == r) {
    tree[idx][0] = (1 << 28);
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  if (pos <= mid)
    del(lft, l, mid, pos);
  else
    del(rgt, mid + 1, r, pos);
  combined(idx, lft, rgt);
}
void updateR(int idx, int l, int r, int pos, int val) {
  if (l == r) {
    Rgt[idx] = val;
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  if (pos <= mid)
    updateR(lft, l, mid, pos, val);
  else
    updateR(rgt, mid + 1, r, pos, val);
  combined(idx, lft, rgt);
}
void updateL(int idx, int l, int r, int pos, int val) {
  if (l == r) {
    Lft[idx] = val;
    return;
  }
  int mid = (l + r) >> 1, lft = idx << 1, rgt = lft | 1;
  if (pos <= mid)
    updateL(lft, l, mid, pos, val);
  else
    updateL(rgt, mid + 1, r, pos, val);
  combined(idx, lft, rgt);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  build(1, 1, n);
  long long int ans = 0;
  for (int i = 1; i <= n - 2; i++) {
    int id = tree[1][1];
    del(1, 1, n, id);
    int id2 = mp[id];
    int ladj = Lft[id2];
    int radj = Rgt[id2];
    if ((a[ladj] < a[radj] ? a[ladj] : a[radj]) == 0)
      ans += a[id];
    else
      ans += (a[ladj] < a[radj] ? a[ladj] : a[radj]);
    if (ladj) updateR(1, 1, n, ladj, radj);
    if (radj <= n) updateL(1, 1, n, radj, ladj);
  }
  printf("%lld\n", ans);
  return 0;
}

```