CF1806F2.GCD Master (hard version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is the hard version of the problem. The only difference between the two versions is the constraint on mm . You can make hacks only if both versions of the problem are solved.

You are given an array aa of length nn and two integers mm and kk . Each element in aa satisfies 1aim1\le a_i \le m .

In one operation, you choose two indices ii and jj such that 1i<ja1 \le i < j \le |a| , then append gcd(ai,aj)\gcd(a_i,a_j) to the back of the array and delete aia_i and aja_j from the array. Note that the length of the array decreases by one after this operation.

Find the maximum possible sum of the array after performing exactly kk operations.

输入格式

The first line contains a single integer tt ( 1t1051\le t\le 10^5 ) — the number of test cases. The description of test cases follows.

The first line of each test case contains three integers nn , mm and kk ( 2n1062 \le n \le 10^6 ; 1m910181\le m \le 9\cdot 10^{18} ; 1kn11 \le k \le n-1 ).

The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n ( 1aim1 \le a_i \le m ).

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6 .

输出格式

For each test case, output the maximum possible sum of the array after performing kk operations optimally.

输入输出样例

  • 输入#1

    4
    3 8 1
    4 7 8
    5 114514 2
    7 2 4 1 6
    3 1919810 2
    2 3 3
    3 9000000000000000000 1
    9000000000000000000 9000000000000000000 9000000000000000000

    输出#1

    11
    14
    1
    18000000000000000000

说明/提示

In the first test case, the best way is to choose i=1i=1 , j=3j=3 in the first operation. The final sequence is [7,4][7,4] .

首页