#include <bits/stdc++.h>
using namespace std;
clock_t time_p = clock();
void aryanc403() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
  return uniform_int_distribution<long long int>(l, r)(rng);
}
class CMP {
 public:
  bool operator()(pair<long long int, long long int> a,
                  pair<long long int, long long int> b) {
    return !(a.first < b.first || a.first == b.first && a.second <= b.second);
  }
};
void add(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt == m.end())
    m.insert({x, cnt});
  else
    jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt->second <= cnt)
    m.erase(jt);
  else
    jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
         const pair<long long int, long long int> &b) {
  return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int mod = 1000000007L;
long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;
long long int m;
string s;
vector<long long int> a;
map<long long int, long long int> dp;
long long int solve(long long int x) {
  auto it = dp.find(x);
  if (it != dp.end()) return it->second;
  long long int j = lower_bound(a.begin(), a.end(), x + 1) - a.begin();
  j--;
  if (j < 0) return n * x;
  ;
  long long int ans =
      (n - j - 1) * (x - x % a[j] - a[j]) +
      max((n - j - 1) * a[j] + solve(x % a[j]), solve(a[j] - 1));
  return dp[x] = ans;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  {
    cin >> n;
    a.clear();
    a.reserve(n);
    a.push_back(INF);
    for (i = 0; i < (n); ++i) {
      cin >> in;
      if (in >= a.back()) {
        a.push_back(a.back());
        continue;
      }
      a.push_back(in);
    }
    reverse(a.begin(), a.end());
    a.pop_back();
    cout << solve(a.back() - 1) << "\n";
    ;
  }
  aryanc403();
  return 0;
}

### Prompt

Please formulate 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;
clock_t time_p = clock();
void aryanc403() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
  return uniform_int_distribution<long long int>(l, r)(rng);
}
class CMP {
 public:
  bool operator()(pair<long long int, long long int> a,
                  pair<long long int, long long int> b) {
    return !(a.first < b.first || a.first == b.first && a.second <= b.second);
  }
};
void add(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt == m.end())
    m.insert({x, cnt});
  else
    jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt->second <= cnt)
    m.erase(jt);
  else
    jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
         const pair<long long int, long long int> &b) {
  return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int mod = 1000000007L;
long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;
long long int m;
string s;
vector<long long int> a;
map<long long int, long long int> dp;
long long int solve(long long int x) {
  auto it = dp.find(x);
  if (it != dp.end()) return it->second;
  long long int j = lower_bound(a.begin(), a.end(), x + 1) - a.begin();
  j--;
  if (j < 0) return n * x;
  ;
  long long int ans =
      (n - j - 1) * (x - x % a[j] - a[j]) +
      max((n - j - 1) * a[j] + solve(x % a[j]), solve(a[j] - 1));
  return dp[x] = ans;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  {
    cin >> n;
    a.clear();
    a.reserve(n);
    a.push_back(INF);
    for (i = 0; i < (n); ++i) {
      cin >> in;
      if (in >= a.back()) {
        a.push_back(a.back());
        continue;
      }
      a.push_back(in);
    }
    reverse(a.begin(), a.end());
    a.pop_back();
    cout << solve(a.back() - 1) << "\n";
    ;
  }
  aryanc403();
  return 0;
}

```