Codeforces 1183D----Candy Box (easy version)

题意:

This problem is actually a subproblem of problem G from the same contest.

There are nn candies in a candy box. The type of the ii-th candy is aiai (1≤ai≤n1≤ai≤n).

You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type 11 and two candies of type 22 is bad).

It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.

Your task is to find out the maximum possible size of the single gift you can prepare using the candies you have.

You have to answer qq 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 qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries. Each query is represented by two lines.

The first line of each query contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of candies.

The second line of each query contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), where aiaiis the type of the ii-th candy in the box.

It is guaranteed that the sum of nn over all queries does not exceed 2⋅1052⋅105. 

 Output

For each query print one integer — the maximum possible size of the single gift you can compose using candies you got in this query with the restriction described in the problem statement.

Example

Input

3
8
1 4 8 4 5 6 3 8
16
2 1 3 3 4 3 4 4 1 3 2 2 2 4 1 1
9
2 2 4 4 4 7 7 7 7

Output

3
10
9

算法分析:

先用一个数组记录输入的数出现的次数,然后再将数组排序,先取大的,利用贪心的思想,遍历整个数组

 AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int vis[500010],t,num,n;
int main()
{
    cin>>t;
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)vis[i]=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&num);
            vis[num]++;
        }
        sort(vis+1,vis+n+1);
        int now=1000000,flag=0;
        for(int i=n;i>=1&&now&&vis[i];i--){
            if(vis[i]>=now)now--;
            else now=vis[i];
            flag+=now;
        }
        printf("%d\n",flag);
    }
    return 0;
}

 

全部评论

相关推荐

07-15 11:43
门头沟学院 Java
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务