#include <bits/stdc++.h>
using namespace std;
const int RLEN = (1 << 20) | 5;
inline char gc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? EOF : *ib++;
}
inline int read() {
  char ch = getchar();
  int res = 0, f = 1;
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();
  return f ? res : -res;
}
inline long long readl() {
  char ch = getchar();
  long long res = 0;
  bool f = 1;
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();
  return f ? res : -res;
}
template <class tp>
inline void chemx(tp &a, tp b) {
  a < b ? a = b : 0;
}
template <class tp>
inline void chemn(tp &a, tp b) {
  a > b ? a = b : 0;
}
const int N = 200005;
int n;
long long a[N];
map<long long, long long> mp;
map<long long, long long>::iterator it;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = readl();
  mp[a[1] - 1] = 0;
  for (int i = 2; i <= n; i++) {
    for (it = --mp.end(); it->first >= a[i]; mp.erase(it), it = --mp.end()) {
      chemx(mp[a[i] - 1],
            (it->first - it->first % a[i] - a[i]) * (i - 1) + it->second);
      chemx(mp[it->first % a[i]],
            (it->first - it->first % a[i]) * (i - 1) + it->second);
    }
  }
  long long res = 0;
  for (it = mp.begin(); it != mp.end(); ++it)
    chemx(res, it->first * n + it->second);
  cout << res;
}

### 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>
using namespace std;
const int RLEN = (1 << 20) | 5;
inline char gc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? EOF : *ib++;
}
inline int read() {
  char ch = getchar();
  int res = 0, f = 1;
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();
  return f ? res : -res;
}
inline long long readl() {
  char ch = getchar();
  long long res = 0;
  bool f = 1;
  while (!isdigit(ch)) f ^= ch == '-', ch = getchar();
  while (isdigit(ch)) res = (res + (res << 2) << 1) + (ch ^ 48), ch = getchar();
  return f ? res : -res;
}
template <class tp>
inline void chemx(tp &a, tp b) {
  a < b ? a = b : 0;
}
template <class tp>
inline void chemn(tp &a, tp b) {
  a > b ? a = b : 0;
}
const int N = 200005;
int n;
long long a[N];
map<long long, long long> mp;
map<long long, long long>::iterator it;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = readl();
  mp[a[1] - 1] = 0;
  for (int i = 2; i <= n; i++) {
    for (it = --mp.end(); it->first >= a[i]; mp.erase(it), it = --mp.end()) {
      chemx(mp[a[i] - 1],
            (it->first - it->first % a[i] - a[i]) * (i - 1) + it->second);
      chemx(mp[it->first % a[i]],
            (it->first - it->first % a[i]) * (i - 1) + it->second);
    }
  }
  long long res = 0;
  for (it = mp.begin(); it != mp.end(); ++it)
    chemx(res, it->first * n + it->second);
  cout << res;
}

```