#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n;
long long a[200005];
map<pair<int, long long>, long long> my;
int getsmall(int l, int r, long long val) {
  if (l > r) return n + 1;
  if (l == r) return l;
  int mid = (l + r) >> 1;
  if (a[mid] > val) return getsmall(mid + 1, r, val);
  return getsmall(l, mid, val);
}
long long fun(int idx, long long limit) {
  long long &res = my[make_pair(idx, limit)];
  if (res) return res;
  if (idx == n) return res = idx * limit;
  if (limit < a[idx + 1]) {
    return res = fun(getsmall(idx + 2, n, limit) - 1, limit);
  }
  res = ((limit % a[idx + 1]) ? fun(idx + 1, limit % a[idx + 1]) : 0) +
        idx * (limit - limit % a[idx + 1]);
  limit -= a[idx + 1] - 1;
  return res = max(res, fun(idx + 1, a[idx + 1] - 1) +
                            idx * (limit - limit % a[idx + 1]));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i > 1) a[i] = min(a[i], a[i - 1]);
  }
  a[n + 1] = 1;
  printf("%lld\n", fun(1, a[1] - 1));
  return 0;
}

### Prompt

Create a solution in CPP for 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 mod = 1e9 + 7;
int n;
long long a[200005];
map<pair<int, long long>, long long> my;
int getsmall(int l, int r, long long val) {
  if (l > r) return n + 1;
  if (l == r) return l;
  int mid = (l + r) >> 1;
  if (a[mid] > val) return getsmall(mid + 1, r, val);
  return getsmall(l, mid, val);
}
long long fun(int idx, long long limit) {
  long long &res = my[make_pair(idx, limit)];
  if (res) return res;
  if (idx == n) return res = idx * limit;
  if (limit < a[idx + 1]) {
    return res = fun(getsmall(idx + 2, n, limit) - 1, limit);
  }
  res = ((limit % a[idx + 1]) ? fun(idx + 1, limit % a[idx + 1]) : 0) +
        idx * (limit - limit % a[idx + 1]);
  limit -= a[idx + 1] - 1;
  return res = max(res, fun(idx + 1, a[idx + 1] - 1) +
                            idx * (limit - limit % a[idx + 1]));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (i > 1) a[i] = min(a[i], a[i - 1]);
  }
  a[n + 1] = 1;
  printf("%lld\n", fun(1, a[1] - 1));
  return 0;
}

```