CSU-暑假集训题 Topforces Strikes Back

题目链接:http://codeforces.com/problemset/problem/1183/F

题目

One important contest will take place on the most famous programming platform (Topforces) very soon!

The authors have a pool of n

problems and should choose at most three of them into this contest. The prettiness of the i-th problem is ai

. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible).

But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are x,y,z

, then x should be divisible by neither y, nor z, y should be divisible by neither x, nor z and z should be divisible by neither x, nor y. If the prettinesses of chosen problems are x and y then neither x should be divisible by y nor y should be divisible by x

. Any contest composed from one problem is considered good.

Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool.

You have to answer q

independent queries.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains one integer q

(1q2105

) — the number of queries.

The first line of the query contains one integer n

(1n2105

) — the number of problems.

The second line of the query contains n

integers a1,a2,,an (2ai2105), where ai is the prettiness of the i

-th problem.

It is guaranteed that the sum of n

over all queries does not exceed 2105

.

 

Output

For each query print one integer — the maximum possible cumulative prettiness of the contest composed of at most three problems from the given pool of problems in the query.

Example
Input
3
4
5 6 15 30
4
10 6 30 15
3
3 4 6
Output
30
31
10

思路

先将数据存入数组,然后将数组从大到小排序,用三重循环枚举,看是否互质。

思路很简单,但很容易时间超限,所以用到了unique函数去重,就能大大的提高效率。

而且我代码中间有个致命的逻辑错误,折磨了我好久。

AC代码

#include<iostream>
#include<algorithm>
using namespace std;
int a[200010];
bool cmp(int a,int b){return a>b;}
int main()
{
    int q,n;
    cin>>q;
    while(q--)
    {
        cin>>n;
        for(int i=0;i<n;i++)cin>>a[i];
        sort(a,a+n,cmp);
        n=unique(a,a+n)-a;               //这个去重就能大大的减少时间的限制 
        int ans=0;
        for(int i=0;i<n;i++)
        {
            int sum=a[i];
            if(sum>ans)ans=sum;
            int j;
            for(j=i+1;j<n;j++)
            {
                if(a[i]%a[j]==0)continue;
            //    sum+=a[j];                       这个是个大大的错误,我的天!!出现这个,会把下一个符合题意的a[j]加进去,就是三个了,在加下面的a[k]就是第四个了 
                ans=max(ans,sum+a[j]);
                int k;
                for(k=j+1;k<n;k++)
                {
                    if(a[i]%a[k]==0||a[j]%a[k]==0)continue;
                    ans=max(ans,sum+a[j]+a[k]);
                    break;
                }
                if(k<n)break;
            }
        //    if(j<n)continue;
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xlbfxx/p/11249338.html