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;
template <class T>
inline T MIN_3(T a, T b, T c) {
  return min(min(a, b), c);
}
template <class T>
inline T MAX_3(T a, T b, T c) {
  return max(max(a, b), c);
}
template <class T>
inline T BIGMOD(T n, T m, T mod) {
  long long int ans = 1;
  long long int k = n;
  while (m) {
    if (m & 1) {
      ans *= k;
      if (ans > mod) ans %= mod;
    }
    k *= k;
    if (k > mod) k %= mod;
    m >>= 1;
  }
  return ans;
}
inline int DBLCMP(double a, double b) {
  if (fabs(a - b) <= 1e-11) return 0;
  if (a < b) return -1;
  return 1;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline int countbit(T n) {
  return (n == 0) ? 0 : (1 + countbit(n & (n - 1)));
}
template <class 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;
  }
}
template <class T>
string toString(T n) {
  ostringstream ost;
  ost << n;
  ost.flush();
  return ost.str();
}
template <class T>
string toBinary(T n) {
  string ret = "";
  while (n) {
    if (n % 2 == 1)
      ret += '1';
    else
      ret += '0';
    n >>= 1;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}
void combination(int n, vector<vector<int> > &ret) {
  ret.resize(n + 1, vector<int>(n + 1, 0));
  for (int i = 1; i <= n; i++) {
    ret[i][0] = ret[i][i] = 1;
    for (int j = 1; j < i; j++) {
      ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1];
    }
  }
}
int toInt(string s) {
  int r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
long long int toLInt(string s) {
  long long int r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
double toDouble(string s) {
  double r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
vector<string> parse(string temp) {
  vector<string> ans;
  ans.clear();
  string s;
  istringstream iss(temp);
  while (iss >> s) ans.push_back(s);
  return ans;
}
template <class 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 <class 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 <class T>
inline T power(T b, T p) {
  if (p < 0) return -1;
  if (b <= 0) return -2;
  if (!p) return 1;
  return b * power(b, p - 1);
}
template <class T>
inline void asd(T &ret) {
  char r;
  bool start = false, neg = false;
  ret = 0;
  while (true) {
    r = getchar();
    if ((r - '0' < 0 || r - '0' > 9) && r != '-' && !start) {
      continue;
    }
    if ((r - '0' < 0 || r - '0' > 9) && r != '-' && start) {
      break;
    }
    if (start) ret *= 10;
    start = true;
    if (r == '-')
      neg = true;
    else
      ret += r - '0';
  }
  if (neg) ret *= -1;
  return;
}
template <class T>
inline void asdasd(T x, char y) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  char buf[21], *p = buf;
  do {
    *p++ = '0' + x % 10;
    x /= 10;
  } while (x);
  do {
    putchar(*--p);
  } while (p > buf);
  if (y == 0)
    putchar('\n');
  else if (y == 1)
    putchar(' ');
}
int inf = 10000000;
int N;
int arr[500009];
struct Z {
  int val, pos;
  int lid, rid;
  int sum;
  Z() {}
} tree[500009 * 4];
void build(int cn, int st, int ed) {
  if (st == ed) {
    if (st == 1) {
      tree[cn].val = -inf;
      tree[cn].pos = st;
      tree[cn].lid = -1;
      tree[cn].rid = st + 1;
    } else if (st == N) {
      tree[cn].lid = st - 1;
      tree[cn].rid = -1;
      tree[cn].val = -inf;
      tree[cn].pos = st;
    } else {
      tree[cn].lid = st - 1;
      tree[cn].rid = st + 1;
      tree[cn].val = min(arr[st + 1], arr[st - 1]);
      if (arr[st + 1] < arr[st - 1])
        tree[cn].pos = st;
      else
        tree[cn].pos = st;
    }
    tree[cn].sum = 1;
    return;
  }
  int mid = (st + ed) >> 1;
  int lft = cn << 1;
  int rgt = 1 | lft;
  build(lft, st, mid);
  build(rgt, mid + 1, ed);
  if (tree[lft].val > tree[rgt].val) {
    tree[cn].val = tree[lft].val;
    tree[cn].pos = tree[lft].pos;
  } else {
    tree[cn].val = tree[rgt].val;
    tree[cn].pos = tree[rgt].pos;
  }
  tree[cn].sum = tree[lft].sum + tree[rgt].sum;
}
pair<int, int> insert(int cn, int st, int ed, int pos) {
  if (st == ed) {
    tree[cn].sum = 0;
    tree[cn].val = -inf;
    return make_pair(tree[cn].lid, tree[cn].rid);
  }
  int mid = (st + ed) >> 1;
  int lft = cn << 1;
  int rgt = lft | 1;
  if (pos <= mid)
    return insert(lft, st, mid, pos);
  else
    return insert(rgt, mid + 1, ed, pos);
}
void UpdateRight(int cn, int st, int ed, int pos, int val) {
  if (pos < 1 || pos > N) return;
  if (st == ed) {
    tree[cn].rid = val;
    if (tree[cn].lid == -1 || tree[cn].rid == -1) {
      tree[cn].val = -inf;
      return;
    }
    if (arr[tree[cn].lid] < arr[tree[cn].rid]) {
      tree[cn].val = arr[tree[cn].lid];
    } else {
      tree[cn].val = arr[tree[cn].rid];
    }
    return;
  }
  int mid = (st + ed) >> 1;
  int lft = cn << 1;
  int rgt = lft | 1;
  if (pos <= mid)
    UpdateRight(lft, st, mid, pos, val);
  else
    UpdateRight(rgt, mid + 1, ed, pos, val);
  if (tree[lft].val > tree[rgt].val) {
    tree[cn].val = tree[lft].val;
    tree[cn].pos = tree[lft].pos;
  } else {
    tree[cn].val = tree[rgt].val;
    tree[cn].pos = tree[rgt].pos;
  }
}
void UpdateLeft(int cn, int st, int ed, int pos, int val) {
  if (pos < 1 || pos > N) return;
  if (st == ed) {
    tree[cn].lid = val;
    if (tree[cn].lid == -1 || tree[cn].rid == -1) {
      tree[cn].val = -inf;
      return;
    }
    if (arr[tree[cn].lid] < arr[tree[cn].rid]) {
      tree[cn].val = arr[tree[cn].lid];
    } else {
      tree[cn].val = arr[tree[cn].rid];
    }
    return;
  }
  int mid = (st + ed) >> 1;
  int lft = cn << 1;
  int rgt = lft | 1;
  if (pos <= mid)
    UpdateLeft(lft, st, mid, pos, val);
  else
    UpdateLeft(rgt, mid + 1, ed, pos, val);
  if (tree[lft].val > tree[rgt].val) {
    tree[cn].val = tree[lft].val;
    tree[cn].pos = tree[lft].pos;
  } else {
    tree[cn].val = tree[rgt].val;
    tree[cn].pos = tree[rgt].pos;
  }
}
void update(int pos) {
  pair<int, int> p = insert(1, 1, N, pos);
  UpdateRight(1, 1, N, p.first, p.second);
  UpdateLeft(1, 1, N, p.second, p.first);
}
int main() {
  asd(N);
  for (int i = 1; i <= N; i++) asd(arr[i]);
  if (N <= 2) {
    asdasd(0, 0);
    return 0;
  } else if (N == 3) {
    asdasd(min(arr[1], arr[3]), 0);
    return 0;
  }
  build(1, 1, N);
  long long res = 0;
  for (int i = 1; i <= N - 2; i++) {
    res += tree[1].val;
    update(tree[1].pos);
  }
  asdasd(res, 0);
}
