#P7789. [COCI 2016/2017 #6] Sirni

[COCI 2016/2017 #6] Sirni

Description

There are NN cards, and each card has a value PiP_i written on it.

You may connect any two cards, but the cost is min(PamodPb,PbmodPa)\min(P_a\bmod P_b, P_b\bmod P_a).

Please find the minimum total cost to connect all cards.

Input Format

The first line contains an integer NN, meaning there are NN cards.

The next NN lines each contain a positive integer PiP_i, representing the value written on the ii-th card.

Output Format

One line containing one integer, representing the minimum total cost.

4
2
6
3
11 
1
4
1
2
3
4 
0
3
4
9
15 
4

Hint

Sample Explanation #1

Connect card 11 and card 22, the cost is min(2mod6,6mod2)=0\min(2\bmod 6,6\bmod 2)=0.

Connect card 22 and card 33, the cost is min(3mod6,6mod3)=0\min(3\bmod 6,6\bmod 3)=0.

Connect card 11 and card 44, the cost is min(2mod11,11mod2)=1\min(2\bmod 11,11\bmod 2)=1.

The total cost is 0+0+1=10+0+1=1.

Constraints

For 30%30\% of the testdata, 1N1031\le N\le 10^3.

For another 40%40\% of the testdata, 1Pi1061\le P_i\le 10^6.

For 100%100\% of the testdata, 1N1051\le N\le 10^5, 1Pi1071\le P_i\le 10^7.

Notes

The score of this problem follows the original COCI settings, with a full score of 140140.

Translated from COCI2016_2017 CONTEST #6 T5 SIRNI.

Translated by ChatGPT 5