#include <bits/stdc++.h>
using namespace std;
using std::bitset;
const double Pi = acos(-1);
namespace fastIO {
template <class T>
inline void read(T& x) {
  x = 0;
  bool fu = 0;
  char ch = 0;
  while (ch > '9' || ch < '0') {
    ch = getchar();
    if (ch == '-') fu = 1;
  }
  while (ch <= '9' && ch >= '0') x = (x * 10 - 48 + ch), ch = getchar();
  if (fu) x = -x;
}
inline int read() {
  int x = 0;
  bool fu = 0;
  char ch = 0;
  while (ch > '9' || ch < '0') {
    ch = getchar();
    if (ch == '-') fu = 1;
  }
  while (ch <= '9' && ch >= '0') x = (x * 10 - 48 + ch), ch = getchar();
  return fu ? -x : x;
}
template <class T, class... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
char _n_u_m_[40];
template <class T>
inline void write(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  T tmp = x > 0 ? x : -x;
  if (x < 0) putchar('-');
  register int cnt = 0;
  while (tmp > 0) {
    _n_u_m_[cnt++] = tmp % 10 + '0';
    tmp /= 10;
  }
  while (cnt > 0) putchar(_n_u_m_[--cnt]);
}
template <class T>
inline void write(T x, char ch) {
  write(x);
  putchar(ch);
}
}  // namespace fastIO
using namespace fastIO;
map<long long, long long> dp;
long long fuck, temp, a, b, ans, n;
map<long long, long long>::iterator it, lst;
int main() {
  dp[1000000000000000ll] = 0;
  for (register int i = 1, iend = n = read(); i <= iend; ++i) {
    read(fuck);
    it = dp.end();
    --it;
    temp = 0;
    while (it->first >= fuck) {
      a = it->first, b = it->second;
      dp[a % fuck] = max(dp[a % fuck], b + (i - 1) * (a - a % fuck));
      dp[fuck - 1] =
          max(dp[fuck - 1], b + (i - 1) * ((a - (fuck - 1)) / fuck * fuck));
      lst = it--;
      dp.erase(lst);
      if (it == dp.begin()) break;
    }
  }
  for (auto orz : dp) ans = max(ans, orz.second + n * orz.first);
  cout << ans;
  return 0;
}

### Prompt

Generate a cpp solution 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;
using std::bitset;
const double Pi = acos(-1);
namespace fastIO {
template <class T>
inline void read(T& x) {
  x = 0;
  bool fu = 0;
  char ch = 0;
  while (ch > '9' || ch < '0') {
    ch = getchar();
    if (ch == '-') fu = 1;
  }
  while (ch <= '9' && ch >= '0') x = (x * 10 - 48 + ch), ch = getchar();
  if (fu) x = -x;
}
inline int read() {
  int x = 0;
  bool fu = 0;
  char ch = 0;
  while (ch > '9' || ch < '0') {
    ch = getchar();
    if (ch == '-') fu = 1;
  }
  while (ch <= '9' && ch >= '0') x = (x * 10 - 48 + ch), ch = getchar();
  return fu ? -x : x;
}
template <class T, class... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
char _n_u_m_[40];
template <class T>
inline void write(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  T tmp = x > 0 ? x : -x;
  if (x < 0) putchar('-');
  register int cnt = 0;
  while (tmp > 0) {
    _n_u_m_[cnt++] = tmp % 10 + '0';
    tmp /= 10;
  }
  while (cnt > 0) putchar(_n_u_m_[--cnt]);
}
template <class T>
inline void write(T x, char ch) {
  write(x);
  putchar(ch);
}
}  // namespace fastIO
using namespace fastIO;
map<long long, long long> dp;
long long fuck, temp, a, b, ans, n;
map<long long, long long>::iterator it, lst;
int main() {
  dp[1000000000000000ll] = 0;
  for (register int i = 1, iend = n = read(); i <= iend; ++i) {
    read(fuck);
    it = dp.end();
    --it;
    temp = 0;
    while (it->first >= fuck) {
      a = it->first, b = it->second;
      dp[a % fuck] = max(dp[a % fuck], b + (i - 1) * (a - a % fuck));
      dp[fuck - 1] =
          max(dp[fuck - 1], b + (i - 1) * ((a - (fuck - 1)) / fuck * fuck));
      lst = it--;
      dp.erase(lst);
      if (it == dp.begin()) break;
    }
  }
  for (auto orz : dp) ans = max(ans, orz.second + n * orz.first);
  cout << ans;
  return 0;
}

```