#include <bits/stdc++.h>
using namespace std;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, c;
int f;
template <class I>
inline void gi(I &x) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 1) + (x << 3) + (c & 15);
  x *= f;
}
}  // namespace io
using io ::gi;
map<long long, long long> f;
map<long long, long long>::iterator it, pos;
vector<pair<long long, long long>> add;
int main() {
  register int n, i;
  register long long m, j, ans;
  register pair<long long, long long> x;
  gi(n);
  gi(m);
  f[m - 1] = 0;
  for (i = 1; i < n; ++i) {
    gi(m);
    pos = f.lower_bound(m);
    add.clear();
    for (it = pos; it != f.end(); ++it) {
      j = it->first;
      add.push_back(make_pair(j % m, it->second + i * (j - j % m)));
      add.push_back(make_pair(m - 1, it->second + i * ((j + 1) / m * m - m)));
    }
    f.erase(pos, f.end());
    for (auto x : add) f[x.first] = max(f[x.first], x.second);
  }
  ans = 0;
  for (it = f.begin(); it != f.end(); ++it)
    ans = max(ans, it->first * n + it->second);
  printf("%lld\n", ans);
  return 0;
}

### Prompt

Generate a cpp solution 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;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, c;
int f;
template <class I>
inline void gi(I &x) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-') f = -1;
  for (x = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = (x << 1) + (x << 3) + (c & 15);
  x *= f;
}
}  // namespace io
using io ::gi;
map<long long, long long> f;
map<long long, long long>::iterator it, pos;
vector<pair<long long, long long>> add;
int main() {
  register int n, i;
  register long long m, j, ans;
  register pair<long long, long long> x;
  gi(n);
  gi(m);
  f[m - 1] = 0;
  for (i = 1; i < n; ++i) {
    gi(m);
    pos = f.lower_bound(m);
    add.clear();
    for (it = pos; it != f.end(); ++it) {
      j = it->first;
      add.push_back(make_pair(j % m, it->second + i * (j - j % m)));
      add.push_back(make_pair(m - 1, it->second + i * ((j + 1) / m * m - m)));
    }
    f.erase(pos, f.end());
    for (auto x : add) f[x.first] = max(f[x.first], x.second);
  }
  ans = 0;
  for (it = f.begin(); it != f.end(); ++it)
    ans = max(ans, it->first * n + it->second);
  printf("%lld\n", ans);
  return 0;
}

```