Mod Mod Mod

题意翻译

$$f(x,n) = x \mod a_n$$ $$f(x,i) = ( x \mod a_i ) + f(x \mod a_i,i+1)$$ 给出a序列,当x取遍所有非负整数时$f(x,1)$的最大值。

题目描述

You are given a sequence of integers $ a_{1},a_{2},...,a_{n} $ . Let ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF889E/babd3332060a4ee6973a9fa3f688c744930d9a0a.png), and ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF889E/3a61ca4726dc1db34df92b20859e100704661de5.png) for $ 1<=i&lt;n $ . Here, ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF889E/609a42354b344d4b30f3720c48d42a71dbe37fb8.png) denotes the modulus operation. Find the maximum value of $ f(x,1) $ over all nonnegative integers $ x $ .

输入输出格式

输入格式


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 a single integer — the maximum value of $ f(x,1) $ over all nonnegative integers $ x $ .

输入输出样例

输入样例 #1

2
10 5

输出样例 #1

13

输入样例 #2

5
5 4 3 2 1

输出样例 #2

6

输入样例 #3

4
5 10 5 10

输出样例 #3

16

说明

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 $ .