CF1614D2.Divan and Kostomuksha (hard version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The only difference is maximum value of ai .
Once in Kostomuksha Divan found an array a consisting of positive integers. Now he wants to reorder the elements of a to maximize the value of the following function:
i=1∑ngcd(a1,a2,…,ai),
where gcd(x1,x2,…,xk) denotes the greatest common divisor of integers x1,x2,…,xk , and gcd(x)=x for any integer x.
Reordering elements of an array means changing the order of elements in the array arbitrary, or leaving the initial order.
Of course, Divan can solve this problem. However, he found it interesting, so he decided to share it with you.
输入格式
The first line contains a single integer n ( 1≤n≤105 ) — the size of the array a .
The second line contains n integers a1,a2,…,an ( 1≤ai≤2⋅107 ) — the array a .
输出格式
Output the maximum value of the function that you can get by reordering elements of the array a .
输入输出样例
输入#1
6 2 3 1 2 6 2
输出#1
14
输入#2
10 5 7 10 3 1 10 100 3 42 54
输出#2
131
说明/提示
In the first example, it's optimal to rearrange the elements of the given array in the following order: [6,2,2,2,3,1] :
gcd(a1)+gcd(a1,a2)+gcd(a1,a2,a3)+gcd(a1,a2,a3,a4)+gcd(a1,a2,a3,a4,a5)+gcd(a1,a2,a3,a4,a5,a6)=6+2+2+2+1+1=14.
It can be shown that it is impossible to get a better answer.
In the second example, it's optimal to rearrange the elements of a given array in the following order: [100,10,10,5,1,3,3,7,42,54].