Codeforces Round 858 (Div. 2)
F2
GCD Master (hard version)
Codeforces
2900
原题
← F1→
翻译中…

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

翻译中…

You are given an array $a$ of length $n$ and two integers $m$ and $k$. Each element in $a$ satisfies $1\le a_i \le m$.

翻译中…

In one operation, you choose two indices $i$ and $j$ such that $1 \le i \lt j \le |a|$, then append $\gcd(a_i,a_j)$ to the back of the array and delete $a_i$ and $a_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 $k$ operations.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1\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 $n$, $m$ and $k$ ($2 \le n \le 10^6$; $1\le m \le 9\cdot 10^{18}$; $1 \le k \le n-1$).

翻译中…

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le m$).

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

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

翻译中…

样例

Input #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
Output #1
11
14
1
18000000000000000000
InputCopy OutputCopy
题目信息
题号
1806F2
时间限制
3 s
内存限制
1024 MB
标签
贪心
数学
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。