#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
namespace ywy {
inline long long get() {
  long long n = 0;
  char c;
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9') break;
    if (c == '-') goto s;
  }
  n = c - '0';
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 + c - '0';
    else
      return (n);
  }
s:
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 - c + '0';
    else
      return (n);
  }
}
long long ints[222222];
map<long long, long long, greater<long long> > mp;
long long tmp[1000001];
void ywymain() {
  int n = get();
  for (register int i = 1; i <= n; i++) ints[i] = get();
  mp[ints[1] - 1] = 0;
  for (register int i = 1; i < n; i++) {
    int ptr = 0;
    long long mx = 0;
    for (map<long long, long long, greater<long long> >::iterator j =
             mp.begin();
         j != mp.end(); j++) {
      if ((j->first) < ints[i + 1]) break;
      mp[ints[i + 1] - 1] = max(
          mp[ints[i + 1] - 1],
          (j->second) +
              (long long)i * (ints[i + 1] * (((j->first) + 1) / ints[i + 1]) -
                              ints[i + 1]));
      mp[(j->first) % ints[i + 1]] = max(
          mp[(j->first) % ints[i + 1]],
          (j->second) + (long long)i * ((j->first) - (j->first) % ints[i + 1]));
      tmp[ptr] = j->first;
      ptr++;
    }
    while (ptr) ptr--, mp.erase(tmp[ptr]);
  }
  long long ans = 0;
  long long mx = 0;
  for (map<long long, long long, greater<long long> >::iterator i = mp.begin();
       i != mp.end(); i++) {
    ans = max(ans, n * (i->first) + (i->second));
  }
  cout << ans << endl;
}
}  // namespace ywy
int main() {
  ywy::ywymain();
  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>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
namespace ywy {
inline long long get() {
  long long n = 0;
  char c;
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9') break;
    if (c == '-') goto s;
  }
  n = c - '0';
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 + c - '0';
    else
      return (n);
  }
s:
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 - c + '0';
    else
      return (n);
  }
}
long long ints[222222];
map<long long, long long, greater<long long> > mp;
long long tmp[1000001];
void ywymain() {
  int n = get();
  for (register int i = 1; i <= n; i++) ints[i] = get();
  mp[ints[1] - 1] = 0;
  for (register int i = 1; i < n; i++) {
    int ptr = 0;
    long long mx = 0;
    for (map<long long, long long, greater<long long> >::iterator j =
             mp.begin();
         j != mp.end(); j++) {
      if ((j->first) < ints[i + 1]) break;
      mp[ints[i + 1] - 1] = max(
          mp[ints[i + 1] - 1],
          (j->second) +
              (long long)i * (ints[i + 1] * (((j->first) + 1) / ints[i + 1]) -
                              ints[i + 1]));
      mp[(j->first) % ints[i + 1]] = max(
          mp[(j->first) % ints[i + 1]],
          (j->second) + (long long)i * ((j->first) - (j->first) % ints[i + 1]));
      tmp[ptr] = j->first;
      ptr++;
    }
    while (ptr) ptr--, mp.erase(tmp[ptr]);
  }
  long long ans = 0;
  long long mx = 0;
  for (map<long long, long long, greater<long long> >::iterator i = mp.begin();
       i != mp.end(); i++) {
    ans = max(ans, n * (i->first) + (i->second));
  }
  cout << ans << endl;
}
}  // namespace ywy
int main() {
  ywy::ywymain();
  return (0);
}

```