#include <bits/stdc++.h>
using namespace std;
long long read() {
  char ch;
  for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
    ;
  long long x = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x;
}
const int N = 2 * 1e5 + 5;
struct node {
  long long j, k;
  node(long long _j = 0, long long _k = 0) {
    j = _j;
    k = _k;
  }
  friend bool operator<(node x, node y) {
    return x.j > y.j || x.j == y.j && x.k > y.k;
  }
};
set<node> f;
long long a[N];
int main() {
  int n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  f.insert(node(a[1] - 1, 0));
  for (int i = 1; i <= n - 1; i++) {
    long long m = a[i + 1], Mx = 0;
    bool pty = 0;
    while (1) {
      set<node>::iterator pos = f.begin();
      long long j = (*pos).j, k = (*pos).k;
      if (j < m) break;
      pty = 1;
      Mx = max(Mx, k + (long long)i * ((j + 1) / m * m - m));
      f.insert(node(j % m, k + (long long)i * (j - j % m)));
      f.erase(pos);
    }
    if (pty) f.insert(node(m - 1, Mx));
  }
  long long ans = 0;
  for (set<node>::iterator pos = f.begin(); pos != f.end(); pos++) {
    long long j = (*pos).j, k = (*pos).k;
    ans = max(ans, k + (long long)j * n);
  }
  printf("%lld\n", ans);
}

### 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 namespace std;
long long read() {
  char ch;
  for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
    ;
  long long x = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    x = x * 10 + ch - '0';
  return x;
}
const int N = 2 * 1e5 + 5;
struct node {
  long long j, k;
  node(long long _j = 0, long long _k = 0) {
    j = _j;
    k = _k;
  }
  friend bool operator<(node x, node y) {
    return x.j > y.j || x.j == y.j && x.k > y.k;
  }
};
set<node> f;
long long a[N];
int main() {
  int n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  f.insert(node(a[1] - 1, 0));
  for (int i = 1; i <= n - 1; i++) {
    long long m = a[i + 1], Mx = 0;
    bool pty = 0;
    while (1) {
      set<node>::iterator pos = f.begin();
      long long j = (*pos).j, k = (*pos).k;
      if (j < m) break;
      pty = 1;
      Mx = max(Mx, k + (long long)i * ((j + 1) / m * m - m));
      f.insert(node(j % m, k + (long long)i * (j - j % m)));
      f.erase(pos);
    }
    if (pty) f.insert(node(m - 1, Mx));
  }
  long long ans = 0;
  for (set<node>::iterator pos = f.begin(); pos != f.end(); pos++) {
    long long j = (*pos).j, k = (*pos).k;
    ans = max(ans, k + (long long)j * n);
  }
  printf("%lld\n", ans);
}

```