#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200100;
int n;
long long a[maxn + 5];
map<pair<int, long long>, long long> f;
long long work(int first, const long long &lim) {
  if (f.count(make_pair(first, lim))) return f[make_pair(first, lim)];
  long long &ret = f[make_pair(first, lim)];
  if (first == n - 1) {
    return ret = (long long)(lim - 1) * (first + 1);
  }
  if (lim <= a[first + 1]) {
    int l = first + 1, r = n;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (a[mid] >= lim)
        l = mid + 1;
      else
        r = mid;
    }
    return ret = work(l - 1, lim);
  }
  long long tmp = lim - lim % a[first + 1] - a[first + 1];
  ret = work(first + 1, a[first + 1]) + (long long)tmp * (first + 1);
  if ((lim % a[first + 1]))
    chkmax(ret, work(first + 1, lim % a[first + 1]) +
                    (long long)(tmp + a[first + 1]) * (first + 1));
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) cin >> a[i];
  for (int i = (1), i_end_ = (n); i < i_end_; ++i) chkmin(a[i], a[i - 1]);
  cout << work(0, a[0]) << endl;
  return 0;
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200100;
int n;
long long a[maxn + 5];
map<pair<int, long long>, long long> f;
long long work(int first, const long long &lim) {
  if (f.count(make_pair(first, lim))) return f[make_pair(first, lim)];
  long long &ret = f[make_pair(first, lim)];
  if (first == n - 1) {
    return ret = (long long)(lim - 1) * (first + 1);
  }
  if (lim <= a[first + 1]) {
    int l = first + 1, r = n;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (a[mid] >= lim)
        l = mid + 1;
      else
        r = mid;
    }
    return ret = work(l - 1, lim);
  }
  long long tmp = lim - lim % a[first + 1] - a[first + 1];
  ret = work(first + 1, a[first + 1]) + (long long)tmp * (first + 1);
  if ((lim % a[first + 1]))
    chkmax(ret, work(first + 1, lim % a[first + 1]) +
                    (long long)(tmp + a[first + 1]) * (first + 1));
  return ret;
}
int main() {
  scanf("%d", &n);
  for (int i = (0), i_end_ = (n); i < i_end_; ++i) cin >> a[i];
  for (int i = (1), i_end_ = (n); i < i_end_; ++i) chkmin(a[i], a[i - 1]);
  cout << work(0, a[0]) << endl;
  return 0;
}

```