PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000364. Tối đa hóa tổng GCD tiền tố – Bản dễ (Divan and Kostomuksha (Easy))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tối đa hóa tổng GCD tiền tố – Bản dễ (Divan and Kostomuksha (Easy))

Divan and Kostomuksha (Easy)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn positive integers. You may reorder its elements arbitrarily, but must use every element.

For the chosen order let

gi=gcd⁡(a1,a2,…,ai),1≤i≤n.g_i=\gcd(a_1,a_2,\ldots,a_i),\quad 1\le i\le n.

Find the maximum possible value of g1+g2+⋯+gng_1+g_2+\cdots+g_n over all permutations. Equal-valued elements at different positions are still separate elements. This is the easy version, with ai≤5⋅106a_i\le5\cdot10^6.

Input

  • Line 1: nn.
  • Line 2: nn positive integers a1,…,ana_1,\ldots,a_n.

Output

One integer: the maximum prefix-GCD sum. The answer may exceed 32-bit signed integer range.

Subtasks

  • Subtask 1 (20 points): 1<=n<=8;1<=ai<=301 <= n <= 8; 1 <= a_i <= 30.
  • Subtask 2 (30 points): 1<=n<=1000;1<=ai<=1000001 <= n <= 1000; 1 <= a_i <= 100000.
  • Subtask 3 (50 points): 1<=n<=100000;1<=ai<=50000001 <= n <= 100000; 1 <= a_i <= 5000000.

Examples

Example 1

Input

6
2 3 1 2 6 2

Output

14

Explanation

The order [6,2,2,2,3,1] yields prefix gcd values 6,2,2,2,1,1; sum 14.

Example 2

Input

10
5 7 10 3 1 10 100 3 42 54

Output

131

Explanation

The order [100,10,10,5,1,3,3,7,42,54] gives prefix gcd values 100,10,10,5,1,1,1,1,1,1: total 131.