Tying All the Cards

Tie all N cards into one connected set where each tie costs the larger number modulo the smaller, with minimum total cost.

Hard8Minimum spanning treeNumber theoryUnion-findNo attempts yetTime limit5sMemory limit768 MB

Problem

Daniel has a bag of candy and NN cards. Each card has one positive integer PiP_i written on it.

While eating his candy, Daniel thought of a game. He can tie together two cards labelled aa and bb, and each tie forces him to eat min(amodb, bmoda)\min(a \bmod b,\ b \bmod a) pieces of candy, where xmodyx \bmod y is the remainder of xx divided by yy.

Daniel wants to tie the cards so that lifting any one card lifts all the others with it. A single card can be tied directly to any number of other cards. Daniel watches his figure, so he does not want to eat much.

Compute the smallest number of candy pieces he must eat to connect all the cards.

Input

The first line contains the positive integer NN. (1N1051 \le N \le 10^5)

Each of the next NN lines contains one positive integer PiP_i written on a card. (1Pi1071 \le P_i \le 10^7)

Output

Print on the first line the smallest number of candy pieces needed to connect all the cards.

Hint

In the first example Daniel ties the first card to the second and eats 0 candy, ties the second card to the third and eats 0 candy, then ties the first card to the fourth and eats 1 candy. One piece of candy connects all four cards.