#include <bits/stdc++.h>
using namespace std;
const int Imx = 2147483647;
const long long Lbig = 2e18;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
set<pair<long long, long long> > st;
long long sz, K;
int n;
void modmodmod(long long m) {
  if (m >= sz) {
    K++;
    return;
  }
  sz = m;
  long long mx = 0;
  while (st.size() > 0) {
    pair<long long, long long> cur = *(--st.end());
    if (cur.first <= m) break;
    st.erase(--st.end());
    mx = max(mx, cur.second + 1ll * (cur.first - cur.first % m - m) * K);
    if (cur.first % m != 0)
      st.insert(make_pair(cur.first % m,
                          1ll * (cur.first - cur.first % m) * K + cur.second));
  }
  st.insert(make_pair(m, mx));
  K++;
}
int main() {
  n = getnum();
  sz = 10000000000000ll;
  st.insert(make_pair(sz, 0));
  for (int i = 1; i <= n; i++) {
    long long x;
    x = getnum();
    modmodmod(x);
  }
  long long ans = 0;
  for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); it++) {
    ans = max(ans, 1ll * (it->first - 1) * n + it->second);
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Please create a solution in CPP to the following problem:

You are given a sequence of integers a1, a2, ..., an. Let <image>, and <image> for 1 ≤ i < n. Here, <image> denotes the modulus operation. Find the maximum value of f(x, 1) over all nonnegative integers x. 

Input

The first line contains a single integer n (1 ≤ n ≤ 200000) — the length of the sequence.

The second lines contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1013) — the elements of the sequence.

Output

Output a single integer — the maximum value of f(x, 1) over all nonnegative integers x.

Examples

Input

2
10 5


Output

13


Input

5
5 4 3 2 1


Output

6


Input

4
5 10 5 10


Output

16

Note

In the first example you can choose, for example, x = 19.

In the second example you can choose, for example, x = 3 or x = 2.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int Imx = 2147483647;
const long long Lbig = 2e18;
const int mod = 1e9 + 7;
struct fastio {
  char s[100000];
  int it, len;
  fastio() { it = len = 0; }
  inline char get() {
    if (it < len) return s[it++];
    it = 0;
    len = fread(s, 1, 100000, stdin);
    if (len == 0)
      return EOF;
    else
      return s[it++];
  }
  bool notend() {
    char c = get();
    while (c == ' ' || c == '\n') c = get();
    if (it > 0) it--;
    return c != EOF;
  }
} _buff;
inline long long getnum() {
  long long r = 0;
  bool ng = 0;
  char c;
  c = _buff.get();
  while (c != '-' && (c < '0' || c > '9')) c = _buff.get();
  if (c == '-') ng = 1, c = _buff.get();
  while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get();
  return ng ? -r : r;
}
template <class T>
inline void putnum(T x) {
  if (x < 0) putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x) a[sz++] = x % 10, x /= 10;
  if (sz == 0) putchar('0');
  for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline char getreal() {
  char c = _buff.get();
  while (c <= 32) c = _buff.get();
  return c;
}
set<pair<long long, long long> > st;
long long sz, K;
int n;
void modmodmod(long long m) {
  if (m >= sz) {
    K++;
    return;
  }
  sz = m;
  long long mx = 0;
  while (st.size() > 0) {
    pair<long long, long long> cur = *(--st.end());
    if (cur.first <= m) break;
    st.erase(--st.end());
    mx = max(mx, cur.second + 1ll * (cur.first - cur.first % m - m) * K);
    if (cur.first % m != 0)
      st.insert(make_pair(cur.first % m,
                          1ll * (cur.first - cur.first % m) * K + cur.second));
  }
  st.insert(make_pair(m, mx));
  K++;
}
int main() {
  n = getnum();
  sz = 10000000000000ll;
  st.insert(make_pair(sz, 0));
  for (int i = 1; i <= n; i++) {
    long long x;
    x = getnum();
    modmodmod(x);
  }
  long long ans = 0;
  for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); it++) {
    ans = max(ans, 1ll * (it->first - 1) * n + it->second);
  }
  cout << ans << endl;
  return 0;
}

```