Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

The problem statement has recently been changed. View the changes.

×
E. Mod Mod Mod

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputYou are given a sequence of integers *a*_{1}, *a*_{2}, ..., *a*_{n}. Let , and for 1 ≤ *i* < *n*. Here, 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 *a*_{1}, *a*_{2}, ..., *a*_{n} (1 ≤ *a*_{i} ≤ 10^{13}) — 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.

Codeforces (c) Copyright 2010-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Sep/25/2022 12:10:44 (j3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|