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

#GL0000140. Tổng ƯCLN tiền tố (Prefix GCD)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng ƯCLN tiền tố (Prefix GCD)

Prefix GCD

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n, you may rearrange them in any order, without adding or deleting elements.

For a chosen order, let gig_i be the GCD of the first ii elements. Define

S=g1+g2+⋯+gn.S=g_1+g_2+\cdots+g_n.

Find the minimum possible value of SS over all permutations of the array.

Input

The first line contains nn; the second contains nn positive integers aia_i.

Output

Print the minimum sum of prefix GCD values.

Subtasks

  • Subtask 1 (20 points): 1≤n≤91\le n\le 9; 1≤ai≤401\le a_i\le 40.
  • Subtask 2 (30 points): 1≤n≤3 0001\le n\le 3\,000; 1≤ai≤1 0001\le a_i\le 1\,000.
  • Subtask 3 (50 points): 1≤n≤100 0001\le n\le 100\,000; 1≤ai≤100 0001\le a_i\le 100\,000.

Examples

Input

3
10 15 6

Output

9

Explanation

Ordering the elements as [6,10,15][6,10,15] gives prefix GCDs 66, 22, and 11, for a total of 99. No permutation achieves a smaller sum.