Codeforces Round 1010 (Div. 1, Unrated)
D
MST in Modulo Graph
Codeforces
2600
原题
← CE →
翻译中…

You are given a complete graph with $n$ vertices, where the $i$-th vertex has a weight $p_i$. The weight of the edge connecting vertex $x$ and vertex $y$ is equal to $\operatorname{max}(p_x, p_y) \bmod \operatorname{min}(p_x, p_y)$.

翻译中…

Find the smallest total weight of a set of $n - 1$ edges that connect all $n$ vertices in this graph.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test contains an integer $n$ ($1 \le n \le 5 \cdot 10^5$).

翻译中…

The next line of each test contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le 5 \cdot 10^5$).

翻译中…

The sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

翻译中…

The sum of $\max(p_1,p_2,\ldots,p_n)$ over all test cases does not exceed $5 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the weight of the minimum spanning tree.

翻译中…

Note

翻译中…

In the first test case, one of the possible ways to connect the edges is to draw the edges $(1, 2)$, $(1, 4)$, $(1, 5)$, $(2, 3)$. The weight of the first edge is $\operatorname{max}(p_1, p_2) \bmod \operatorname{min}(p_1, p_2)=4 \bmod 3 = 1$, and the weights of all other edges are $0$.

翻译中…

样例

Input #1
4
5
4 3 3 4 4
10
2 10 3 2 9 9 4 6 4 6
12
33 56 48 41 89 73 99 150 55 100 111 130
7
11 45 14 19 19 8 10
Output #1
1
0
44
10
InputCopy OutputCopy
题目信息
题号
2081D
时间限制
3 s
内存限制
512 MB
标签
构造
并查集
图论
贪心
数学
数论
排序
树

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