#include <bits/stdc++.h>
using namespace std;
const long long linf = 2e18 + 5;
int mod = (int)1e9 + 7;
const int logN = 18;
const int inf = 1e9 + 9;
const int N = 2e5 + 6;
int n;
long long a[N];
map<long long, long long> dp[N];
int get(int l, int r, long long x) {
  while (l < r) {
    int mid = (l + r) >> 1;
    if (a[mid] > x)
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
long long f(int i, long long r) {
  if (i == n) return r * i;
  if (dp[i].find(r) != dp[i].end()) return dp[i][r];
  if (a[i + 1] > r) return dp[i][r] = f(get(1, n + 1, r) - 1, r);
  long long ans =
      max(f(i + 1, a[i + 1] - 1) + i * (r - r % a[i + 1] - a[i + 1]),
          f(i + 1, r % a[i + 1]) + i * (r - r % a[i + 1]));
  return dp[i][r] = ans;
}
int main() {
  scanf("%d", &n);
  a[0] = linf;
  a[n + 1] = -linf;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] = min(a[i - 1], a[i]);
  }
  cout << f(1, a[1] - 1) << '\n';
}

### Prompt

In Cpp, your task is to solve 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 long long linf = 2e18 + 5;
int mod = (int)1e9 + 7;
const int logN = 18;
const int inf = 1e9 + 9;
const int N = 2e5 + 6;
int n;
long long a[N];
map<long long, long long> dp[N];
int get(int l, int r, long long x) {
  while (l < r) {
    int mid = (l + r) >> 1;
    if (a[mid] > x)
      l = mid + 1;
    else
      r = mid;
  }
  return l;
}
long long f(int i, long long r) {
  if (i == n) return r * i;
  if (dp[i].find(r) != dp[i].end()) return dp[i][r];
  if (a[i + 1] > r) return dp[i][r] = f(get(1, n + 1, r) - 1, r);
  long long ans =
      max(f(i + 1, a[i + 1] - 1) + i * (r - r % a[i + 1] - a[i + 1]),
          f(i + 1, r % a[i + 1]) + i * (r - r % a[i + 1]));
  return dp[i][r] = ans;
}
int main() {
  scanf("%d", &n);
  a[0] = linf;
  a[n + 1] = -linf;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] = min(a[i - 1], a[i]);
  }
  cout << f(1, a[1] - 1) << '\n';
}

```