#include <bits/stdc++.h>
using i64 = long long;
template <typename _Tp>
inline void chkMax(_Tp &x, const _Tp &y) {
  (x < y) and (x = y);
}
namespace IOManager {
constexpr int FILESZ = 131072;
char buf[FILESZ];
const char *ibuf = buf, *tbuf = buf;
struct IOManager {
  inline char gc() {
    return (ibuf == tbuf) and
                   (tbuf = (ibuf = buf) + fread(buf, 1, FILESZ, stdin),
                    ibuf == tbuf)
               ? EOF
               : *ibuf++;
  }
  template <typename _Tp>
  inline operator _Tp() {
    _Tp s = 0u;
    char c = gc();
    for (; c < 48; c = gc())
      ;
    for (; c > 47; c = gc()) s = (_Tp)(s * 10u + c - 48u);
    return s;
  }
};
}  // namespace IOManager
IOManager::IOManager io;
int main() {
  static std::map<i64, i64> f;
  const int n = io;
  for (int i = 1; i <= n; ++i) {
    const i64 a = io;
    if (i == 1) f[a - 1] = 0;
    for (auto it = f.rbegin(); it not_eq f.rend() and it->first >= a; ++it) {
      const i64 v = it->first;
      chkMax(f[v % a], it->second + (v - v % a) * (i - 1ll));
      chkMax(f[a - 1], it->second + ((v - a + 1ll) / a * a) * (i - 1ll));
    }
    f.erase(f.lower_bound(a), f.end());
  }
  i64 ans = 0;
  for (const auto it : f) chkMax(ans, it.second + it.first * n);
  printf("%lld\n", ans);
  return 0;
}

### 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 i64 = long long;
template <typename _Tp>
inline void chkMax(_Tp &x, const _Tp &y) {
  (x < y) and (x = y);
}
namespace IOManager {
constexpr int FILESZ = 131072;
char buf[FILESZ];
const char *ibuf = buf, *tbuf = buf;
struct IOManager {
  inline char gc() {
    return (ibuf == tbuf) and
                   (tbuf = (ibuf = buf) + fread(buf, 1, FILESZ, stdin),
                    ibuf == tbuf)
               ? EOF
               : *ibuf++;
  }
  template <typename _Tp>
  inline operator _Tp() {
    _Tp s = 0u;
    char c = gc();
    for (; c < 48; c = gc())
      ;
    for (; c > 47; c = gc()) s = (_Tp)(s * 10u + c - 48u);
    return s;
  }
};
}  // namespace IOManager
IOManager::IOManager io;
int main() {
  static std::map<i64, i64> f;
  const int n = io;
  for (int i = 1; i <= n; ++i) {
    const i64 a = io;
    if (i == 1) f[a - 1] = 0;
    for (auto it = f.rbegin(); it not_eq f.rend() and it->first >= a; ++it) {
      const i64 v = it->first;
      chkMax(f[v % a], it->second + (v - v % a) * (i - 1ll));
      chkMax(f[a - 1], it->second + ((v - a + 1ll) / a * a) * (i - 1ll));
    }
    f.erase(f.lower_bound(a), f.end());
  }
  i64 ans = 0;
  for (const auto it : f) chkMax(ans, it.second + it.first * n);
  printf("%lld\n", ans);
  return 0;
}

```