Gym - 101810F Minimum Sum of Array

You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 elements ai and aj in which ai is divisible by aj and transform ai to aj.

A number x is said to be divisible by a number y if x can be divided by y and the result is an exact whole number. For example, 15 is divisible by 3, because 15÷ 3 = 5 exactly, but 9 is not divisible by 2 because 9÷ 2 is 4 with 1 left over.

Your task is to find the minimum sum of the array a that can be obtained by making as many transform operations as you want. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 105), in which n is the size of array a. Then a line follows containing n integers a1, ..., an (1 ≤ ai ≤ 106), giving array a.

The sum of n overall test cases does not exceed 3 × 106.

Output

For each test case, print a single line containing the minimum sum of the array athat can be obtained after making as many transform operations as you want.

Example

Input

1
5
2 2 3 6 6

Output

11

题意:给出一个长度为n的数列a,通过无限次的操作使得这个数列的和最小,操作规则为:若aj能够被ai整除,则aj=ai

解题思路:用vis[i]数组记录i这个数字出现的次数,使i倍增,则i*j必能被i整除,所以i*j都可转化为i,即vis[i]=vis[i]+vis[i*j],vis[i*j]=0,最后求和即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define MOD 1000000007
using namespace std;

int vis[1000006];

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		memset(vis,0,sizeof(vis));
		__int64 i,j;
		__int64 a[100005];
		for(i=1;i<=n;i++){
			scanf("%I64d",&a[i]);
			vis[a[i]]++;
		}
		for(i=1;i<=1e6;i++){
			if(vis[i]>0){
				for(j=2;i*j<=1e6;j++){
					if(vis[i*j]>0){
						vis[i]+=vis[i*j];
						vis[i*j]=0;
					}
				}
			}
		}
		__int64 ans=0;
		for(i=1;i<=1e6;i++){
			ans+=vis[i]*i;
		}
		printf("%I64d\n",ans);
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值