description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for f in range(int(input())): junk = input() data = [int(i) for i in input().split()] dic = {} for d in data: if d in dic: dic[d] += 1 else: dic[d] = 1 array = sorted(dic.values(), reverse=True) ret = 0 curr = array[0] + 1 for a in array: if a < curr: curr = a else: curr -= 1 ret += curr if curr == 1: break print(ret)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout def solve(): n = int(stdin.readline()) x = [] l = list(map(int, stdin.readline().split())) d = {} for i in l: d[i] = d.get(i, 0) + 1 for i in d: x.append(d[i]) x.sort(reverse=True) s = set() c = 0 for i in x: if i not in s: c += i s.add(i) else: while i in s: i -= 1 if i > 0: s.add(i) c += i print(c) for _ in range(int(stdin.readline())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) a = [int(x) for x in input().split()] a.sort() sizes = set() index = 0 while index < n: counter = 0 temp = a[index] while index + counter < n and a[index + counter] == temp: counter += 1 index += counter while counter > 0 and counter in sizes: counter -= 1 sizes.add(counter) ans = 0 for j in sizes: ans += j print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) while t > 0: n = int(input()) a = list(map(int, input().split())) P = (1 + n) * [0] for i in a: P[i] += 1 b = [] for i in range(1 + n): if P[i] != 0: b.append(P[i]) b.sort() m = len(b) - 1 ans = 0 take = b[m] for i in range(m, -1, -1): if take < 0: break if 0 < b[i] <= take: ans += b[i] take = b[i] - 1 elif b[i] > take: ans += take take -= 1 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR LIST NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
test = int(input()) for i in range(test): n = int(input()) ls = list(map(int, input().split())) d = {} for j in range(n): if ls[j] in d.keys(): d[ls[j]] = d[ls[j]] + 1 else: d[ls[j]] = 1 k = set() s = 0 for key, value in d.items(): while value in k and value > 0: value = value - 1 k.add(value) print(str(sum(k)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for i in range(t): n = int(input()) l = list(map(int, input().split())) l.sort() count = 1 truth = [(False) for j in range(n + 1)] fill = 0 total = 0 for j in range(1, n): if truth[count] == False: fill = count if l[j] == l[j - 1]: count += 1 else: if truth[fill] == False: truth[fill] = True total += fill fill = 0 count = 1 if truth[count] == False: fill = count if truth[fill] == False: truth[fill] = True total += fill print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for _ in range(0, t): n = int(input()) l = list(map(int, input().split())) g = max(l) a = [0] * (g + 1) for i in l: a[i] = a[i] + 1 k = list() for i in a: if i > 0: k.append(i) k.sort() m = k[-1] s = m f = len(k) if f == 1: print(k[0]) continue for i in range(f - 2, -1, -1): if k[i] >= m: k[i] = m - 1 m = m - 1 else: m = k[i] s = s + m if m <= 0: break print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys def addInDict(x, y): global d, d2 if x not in d: d[x] = 0 if x not in d2[y]: d2[y][x] = 0 d[x] += 1 d2[y][x] += 1 def calcSum1(): global d, d2, d3, d4, d4Keys, s1, s2 for kol in d4Keys: for numPart in d4[kol]: kol1 = d[numPart] while kol1 in d3 and kol1 > 0: kol1 -= 1 if kol1 > 0: d3[kol1] = 1 s1 += kol1 s2 += min(kol, kol1) if numPart in d2[0]: del d2[0][numPart] def calcSum0(): global d, d2, d3, s1, s2 for key in d2[0]: kol = d[key] while kol in d3 and kol > 0: kol -= 1 if kol > 0: d3[kol] = 1 s1 += kol for _ in range(int(input())): n = int(sys.stdin.readline()) d, d2, d3, d4 = {}, {}, {}, {} d2[0] = {} d2[1] = {} for __ in range(n): a, f = map(int, sys.stdin.readline().split()) addInDict(a, f) s1, s2 = 0, 0 for i in d2[1]: if d2[1][i] not in d4: d4[d2[1][i]] = {} d4[d2[1][i]][i] = i d4Keys = list(d4.keys()) d4Keys.sort(reverse=True) calcSum1() calcSum0() print(s1, s2)
IMPORT FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR FUNC_DEF FOR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR DICT DICT DICT DICT ASSIGN VAR NUMBER DICT ASSIGN VAR NUMBER DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR DICT ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys class DCandyBoxEasyVersion: def solve(self): q = int(input()) for _ in range(q): n = int(input()) a = [int(_) for _ in input().split()] d = {key: (0) for key in a} for x in a: d[x] += 1 rev_d = {key: (0) for key in d.values()} for x in d: rev_d[d[x]] += 1 v = [x for x in rev_d] v.sort() cur = max(rev_d) cnt = max(rev_d) ans = 0 while 1: if cnt == 0 or cur == 0: break if cur > cnt: cur -= 1 continue if cnt not in rev_d or rev_d[cnt] == 0: cnt -= 1 continue rev_d[cnt] -= 1 ans += cur cur -= 1 print(ans) solver = DCandyBoxEasyVersion() input = sys.stdin.readline solver.solve()
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for i in range(1, t + 1): n = int(input()) box = list(map(int, input().split())) d = {} for candy in box: if candy in d: d[candy] += 1 else: d[candy] = 1 s = sorted(d.items(), key=lambda k: k[1], reverse=True) taken = [] sum = 0 pick = 1 flag = 0 for it in s: if it[1] not in taken: sum += it[1] taken.append(it[1]) else: pick = it[1] - 1 while pick > 0: if pick not in taken: sum += pick taken.append(pick) break else: pick -= 1 print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def solve_querry(): size = int(input()) alist = input().split() adict = {} for i in range(size): if alist[i] not in adict: adict[alist[i]] = 1 else: adict[alist[i]] += 1 all_count = [] for x in adict: all_count.append(adict[x]) all_count = sorted(all_count) unique = [] for i in range(len(all_count) - 1, -1, -1): temp = all_count[i] while temp in unique and temp > 0: temp -= 1 if temp == 0: break unique.append(temp) return sum(unique) def solve(): no_querries = int(input()) solution = [] for i in range(no_querries): solution.append(solve_querry()) for x in solution: print(x) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
a = [] for q in range(int(input())): n = int(input()) arr = sorted([int(s) for s in input().split()]) arr1 = [] count, last = 0, arr[0] for i in arr: if last == i: count += 1 else: arr1.append(count) last = i count = 1 arr1.append(count) arr = sorted(arr1) ans, last = 0, arr[len(arr) - 1] + 1 for i in range(len(arr) - 1, -1, -1): if last == 1: break if arr[i] < last: last = arr[i] else: last = min(arr[i], last - 1) ans += last a.append(ans) for i in a: print(i)
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout input = stdin.readline print = stdout.write q = int(input()) for x in range(q): n = int(input()) a = list(map(int, input().split())) tab = [0] * (max(a) + 1) for item in a: tab[item] += 1 tab.sort() i = len(tab) - 1 o = tab[-1] s = 0 while i >= 0 and o > 0: k = min(o, tab[i]) o = k - 1 s += k i -= 1 print(str(s)) print("\n")
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin length = 2 * 10**5 + 1 l = [0] * length for _ in range(int(stdin.readline())): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) for i in range(n + 1): l[i] = 0 for i in a: l[i] += 1 arr = sorted(l[1 : n + 1]) count = 0 ptr = n - 1 no = arr[ptr] while ptr > -1: if no == 0 or arr[ptr] == 0: break if arr[ptr] >= no: count += no no -= 1 else: count += arr[ptr] no = arr[ptr] - 1 ptr -= 1 print(count)
ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): a = int(input()) c = [int(x) for x in input().split()] n = [0] * (a + 1) for j in c: n[j] += 1 n.sort(reverse=True) s = n[0] p = n[0] for j in range(1, a + 1): if p == 1: break if n[j] == 0: break if n[j] >= p: s += p - 1 p = p - 1 if p == 1: break elif n[j] < p: s += n[j] p = n[j] if p == 1: break print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout ii = lambda: int(stdin.readline()) mi = lambda: map(int, stdin.readline().strip().split()) li = lambda: list(mi()) q = ii() while q > 0: n = ii() cnt = [0] * (n + 1) a = li() for x in a: cnt[x] += 1 cnt.sort() ans = 0 take = n i = n while i > 0 and take > 0: if cnt[i] >= take: ans += take i -= 1 take -= 1 stdout.write(str(ans) + "\n") q -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) list = [] for i in range(n + 1): list.append(0) out = [] out = [int(x) for x in input().split()] for i in range(n): list[out[i]] += 1 list.sort(reverse=True) ans = list[0] lst = list[0] for i in range(1, n + 1): if lst == 0: break if list[i] >= lst: ans += lst - 1 lst = lst - 1 else: ans += list[i] lst = list[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys q = int(sys.stdin.readline()) for _ in range(q): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) table = dict() for i in range(n): table[a[i]] = 0 for i in range(n): table[a[i]] += 1 table = list(table.items()) table = sorted(table, key=lambda ele: -ele[1]) res = 0 table2 = dict() for i in range(len(table)): cnt = table[i][1] if cnt not in table2: res += cnt table2[cnt] = 1 else: while cnt in table2: cnt -= 1 if cnt >= 1: res += cnt table2[cnt] = 1 else: break print(res)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) def f(n): s = str(n) c = 0 for i in s: c += int(i) if c % 4 == 0: return 1 else: return 0 t = ii() while t: t -= 1 n = ii() m = {} a = li() for i in range(n): if a[i] not in m: m[a[i]] = 1 else: m[a[i]] += 1 s = [] for i in m.values(): s.append(i) del m m = {} for i in s: if i not in m: m[i] = 1 else: m[i] += 1 b = [] c = [] for i in s: if m[i] == 1: b.append(i) else: c.append(i) c.sort() m = 1 for i in c: for j in range(i, 0, -1): if j not in b: b.append(j) break print(sum(b))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin t = int(stdin.readline().rstrip()) while t > 0: n = int(stdin.readline().rstrip()) l = list(map(int, stdin.readline().split())) d = {} for i in l: if i in d: d[i] += 1 else: d[i] = 1 k = [] for i in d.values(): k.append(i) k.sort(reverse=True) p = k[0] s = k[0] q = 0 if n == 1: print(s) else: for i in range(1, len(k)): if k[i] < p: s += k[i] p = k[i] else: s += p - 1 p -= 1 if p == 0: break print(s) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} for i in a: try: d[i] += 1 except: d[i] = 1 d = sorted(list(d.values()), reverse=True) ld = len(d) s = d[0] m = s - 1 for i in d[1:]: if m != 0: if i >= m: s += m m -= 1 elif m > 0: s += i m = i - 1 else: break print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin input = stdin.readline q = int(input()) d = [0] * 200005 for query in range(q): n = int(input()) for i in range(n + 1): d[i] = 0 for i in input().split(): d[int(i)] += 1 ziom = d[: n + 1] ziom.sort() thres = 10000000 wyn = 0 bla = False for i in reversed(range(len(ziom))): if thres <= 0: print(wyn) bla = True break wyn += min(thres - 1, ziom[i]) thres = min(thres - 1, ziom[i]) if not bla: print(wyn)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
T = int(input()) for _ in range(0, T): n = int(input()) s = [int(x) for x in input().split()] kk = [0] * (n + 1) for i in range(0, len(s)): kk[s[i]] += 1 pre = [] c = 0 for i in range(0, len(kk)): if kk[i] > 0: pre.append(kk[i]) pre.sort() pre = pre[::-1] ptr = n pos = [0] * (n + 1) for i in range(0, len(pre)): tt = min(pre[i], ptr) pos[tt] = 1 ptr = max(0, tt - 1) for i in range(1, len(pos)): if pos[i] > 0: c += i print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout input = stdin.readline for _ in range(int(input())): n = int(input()) d = {} r = [[0, 0] for i in range(n + 1)] for i in range(n): a, b = map(int, input().split()) d[a] = d.get(a, 0) + 1 r[a][b] += 1 b = [] for i in d.keys(): b.append([r[i][1], i]) b.sort(reverse=True) ans = 0 s = set() x = 0 for i in range(len(b)): while d[b[i][1]] > 0 and d[b[i][1]] in s: d[b[i][1]] -= 1 x += min(r[b[i][1]][1], d[b[i][1]]) ans += d[b[i][1]] s.add(d[b[i][1]]) stdout.write(f"{ans} {x}\n")
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) L = lambda: list(map(int, input().split())) for quer in range(q): n = int(input()) a = L() c = [0] * (n + 1) d = [0] * (n + 1) for i in a: c[i] += 1 for i in c: d[i] += 1 mx = n + 1 ans = 0 for i in range(n, 0, -1): if d[i] > 0: if i < mx: ans += i mx = i else: ans += mx - 1 mx -= 1 d[i - 1] += d[i] - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) while q: n = int(input()) data = list(map(int, input().split())) temp = set() cnt = {} for i in range(n): if data[i] in temp: cnt[data[i]] += 1 else: cnt[data[i]] = 1 temp.add(data[i]) data2 = list() for el in temp: data2.append(cnt[el]) data2.sort() data2.reverse() temp2 = set() ans = 0 temp_count = 200000000 for i in range(len(data2)): if data2[i] >= temp_count: ans += temp_count temp_count -= 1 if temp_count <= 0: break else: ans += data2[i] temp_count = data2[i] - 1 if temp_count <= 0: break print(ans) q -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) a = list(map(int, input().split())) u = [0] * n for i in range(n): u[a[i] - 1] += 1 u.sort(reverse=1) k = u[0] ans = k for i in range(1, n): if u[i] >= u[i - 1]: u[i] = u[i - 1] - 1 if u[i] <= 0: break ans += u[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in " " * int(input()): d = {} input() for i in map(int, input().split()): d[i] = d.get(i, 0) + 1 s = 0 k = sorted(list(d.values()), reverse=True) m = 10**9 for i in k: m = max(min(m - 1, i), 0) s += m print(s)
FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
n = int(input()) while n > 0: n = n - 1 nn = int(input()) a = [int(i) for i in input().split()] a.sort() count = [[a[0], 1]] num = 0 for i in range(nn - 1): if count[num][0] != a[i + 1]: count.append([a[i + 1], 1]) num += 1 else: count[num][1] += 1 count.sort(key=lambda i: -i[1]) output = last = count[0][1] for i in range(1, len(count)): if last <= count[i][1]: if last != 0: output = output + last - 1 last = last - 1 else: output = output + count[i][1] last = count[i][1] print(output)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import time q = int(input()) ans = [] start = time.time() for i in range(q): n = int(input()) a = [int(j) for j in input().split()] d = [(0) for j in range(n)] for j in range(n): d[a[j] - 1] += 1 d.sort(reverse=True) s = d[0] last = d[0] for j in range(1, n): if last > d[j]: s += d[j] last = d[j] else: last -= 1 if last <= 0: break s += last ans.append(s) for i in range(q): print(ans[i]) finish = time.time()
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys input = sys.stdin.readline q = int(input()) for _ in range(q): n = int(input()) a = [int(i) for i in input().split()] c = {} for i in a: if i in c: c[i] += 1 else: c[i] = 1 bib = [] for i in c: bib.append(c[i]) bib = sorted(bib)[::-1] prev = bib[0] + 1 su = 0 for i in range(len(bib)): ne = min(prev - 1, bib[i]) if ne <= 0: break su += ne prev = ne print(su)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import * q = int(stdin.readline()) while q: q = q - 1 n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) Hash = [0] * (n + 1) for i in a: Hash[i] += 1 Hash.sort(reverse=True) res = 0 now = int(1000000000.0) for i in Hash: now = max(min(now - 1, i), 0) res += now stdout.write(str(res) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
mod = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) t = ii() for _ in range(t): n = ii() l = il() d = dict() for i in l: if i in d: d[i] += 1 else: d[i] = 1 l2 = sorted([d[i] for i in d], reverse=True) mx = l2[0] sm = l2[0] for i in l2[1:]: if i < mx: mx = i else: mx -= 1 if mx < 0: mx = 0 sm += mx print(sm)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def tointarray(array): for i in range(0, len(array)): array[i] = int(array[i]) return array def result(numInBox, candies): count = [] for i in range(0, numInBox): count.append(0) for candy in candies: count[candy - 1] += 1 spots = [] for i in range(0, numInBox + 1): spots.append(0) for i in count: while i > 0: if spots[i] == 0: spots[i] = -1 break i -= 1 ans = 0 for i in range(0, len(spots)): if spots[i] == -1: ans += i return ans numBox = int(input("")) numInBox = [] candies = [] for i in range(0, numBox): numInBox.append(int(input(""))) candies.append(input("")) answers = [] for i in range(0, numBox): answers.append(result(numInBox[i], tointarray(candies[i].split(" ")))) for answer in answers: print(answer)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR WHILE VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) lis = list(map(int, input().split())) x = {} for i in lis: if i not in x: x[i] = 1 else: x[i] += 1 res = list(x.values()) visited = [] res = sorted(res, reverse=True) ans = 0 for i in res: while i in visited and i > 0: i -= 1 visited.append(i) ans += i print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) konfs = {} for k in input().split(): if k not in konfs: konfs[k] = 0 konfs[k] += 1 konfs = sorted(konfs.items(), key=lambda x: x[1]) cnt = 0 latest_count = None used = [0] for type, count in konfs: if count not in used: used.append(count) cnt += count else: latest = used[-1] for j in range(len(used) - 2, -1, -1): if used[j] != latest - 1: used.insert(j + 1, latest - 1) cnt += latest - 1 break latest = used[j] print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR LIST NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) dic = {} for i in range(n): try: if dic[a[i]]: dic[a[i]] += 1 except KeyError: dic[a[i]] = 1 temp = [] for i in dic: temp.append(dic[i]) temp.sort(reverse=True) last = 10000000 res = 0 for i in temp: if i >= last: if last - 1 <= 0: break res += last - 1 last = last - 1 else: res += i last = i print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
T = int(input()) test = 0 while test < T: N = int(input()) arr = input() arr = [int(num) for num in arr.split(" ")] freq = {} for num in arr: if num not in freq: freq[num] = 1 else: freq[num] += 1 res = 0 k = list(freq.values()) k.sort(reverse=True) m = max(k) idx = 0 while m > 0 and idx < len(k): m = min(m, k[idx]) res += m m -= 1 idx += 1 print(res) test += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = [(0) for _ in range(int(input()))] for i in list(map(int, input().split())): n[i - 1] += 1 n = [x for x in n if x] n.sort(reverse=True) S = i = n.pop(0) while i > 0 and len(n) > 0: i -= 1 i = min(i, n.pop(0)) S += i print(S)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) l = [0] * (n + 1) for j in a: l[j] += 1 l.sort(reverse=True) last = -1 ans = 0 vis = [] for j in range(len(l)): if l[j] == 0: break if l[j] in vis: k = l[j] while k in vis and k > 0: k -= 1 ans += k vis.append(k) else: ans += l[j] vis.append(l[j]) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys q = int(input()) lines = sys.stdin.readlines() res = [] for i in range(q): n = int(lines[2 * i]) bucket = {} for i in lines[2 * i + 1].split(): if not bucket.get(i): bucket[i] = 0 bucket[i] += 1 values = sorted(list(bucket.values()), reverse=True) total = 0 num = None curr = None for i in values: if i == num: curr -= 1 else: num = i if curr is not None and i >= curr: curr -= 1 else: curr = i total += curr if curr == 0: break res.append(str(total)) print("\n".join(res))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NONE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) a = [int(x) for x in input().split()] arr = [0] * n for i in a: arr[i - 1] += 1 arr.sort(reverse=True) s = arr[0] prev = arr[0] for i in range(1, len(arr)): if prev == 1: break if arr[i] < prev: s = s + arr[i] prev = arr[i] else: prev = prev - 1 s = s + prev if arr[i] == 0: break print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys sys.setrecursionlimit(10**5 + 1) inf = int(10**20) max_val = inf min_val = -inf RW = lambda: sys.stdin.readline().strip() RI = lambda: int(RW()) RMI = lambda: [int(x) for x in sys.stdin.readline().strip().split()] RWI = lambda: [x for x in sys.stdin.readline().strip().split()] nb_test = RI() for _ in range(nb_test): lens = RI() vals = RMI() cnts = [0] * lens for i in vals: cnts[i - 1] += 1 cnts.sort() maxv = cnts[-1] + 1 answer = 0 for i in range(lens - 1, -1, -1): if cnts[i] >= maxv: cnts[i] = maxv - 1 maxv = cnts[i] answer += max(0, cnts[i]) print(answer)
IMPORT EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) while q > 0: q = q - 1 n = int(input()) a = list(map(int, input().split())) a.sort(reverse=True) c = [1] l = 0 curVal = a[0] i = 1 while i < n: if a[i] == curVal: c[l] = c[l] + 1 else: c.append(1) l = l + 1 curVal = a[i] i = i + 1 c.sort(reverse=True) l = l + 1 i = 1 r = c[0] while i < l: if c[i] >= c[i - 1]: c[i] = c[i - 1] - 1 if c[i] > 0: r = r + c[i] else: break i = i + 1 print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) min1 = 0 max1 = 0 def cnt(candies): temp = candies[0] j = 1 ans = [] for i in range(1, len(candies)): if candies[i] == candies[i - 1]: j += 1 else: ans.append(j) j = 1 ans.append(j) return ans for i in range(q): n = int(input()) candies = input() candies = candies.split(" ") candies = [int(c) for c in candies] candies.sort() counted = cnt(candies) counted.sort(reverse=True) ans = 0 temp = -1 for item in counted: if temp == 0: break if temp > 0 and item >= temp: temp = temp - 1 ans += temp else: ans += item temp = item print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout q = int(stdin.readline()) for _ in range(q): n = int(stdin.readline()) lst = list(map(int, stdin.readline().split())) count = [0] * max(lst) for j in range(n): count[lst[j] - 1] += 1 count.sort(reverse=True) maxi = count[0] z = 0 ans = 0 while z < len(count) and count[z] != 0 and maxi > 0: if maxi <= count[z]: ans += maxi z += 1 maxi -= 1 stdout.write(str(ans) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) ma = max(a) lis = [(0) for i in range(ma)] for i in a: lis[i - 1] = lis[i - 1] + 1 k = set() count = 0 lis.sort() first = lis[ma - 1] count = count + lis[ma - 1] for i in range(ma - 2, -1, -1): if lis[i] >= first: first = first - 1 count = count + first else: first = lis[i] count = count + first if first == 0: break print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin q = int(stdin.readline().strip()) for _ in range(q): n = int(stdin.readline().strip()) a = [int(i) for i in stdin.readline().strip().split()] d = {} for i in a: d[i] = d.get(i, 0) + 1 counts = sorted(d.values(), reverse=True) got = set() max_ = counts[0] min_ = counts[0] res = 0 for c in counts: if c not in got: got.add(c) res += c min_ = min(min_, c) elif min_ > 0: res += min_ - 1 got.add(min_ - 1) min_ -= 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) P = [0] * (n + 1) A = [int(x) for x in input().split()] for i in A: P[i] += 1 P.sort(reverse=True) Ans = P[0] Expected = Ans - 1 for i in P[1:]: if i and Expected: if i >= Expected: Ans += Expected Expected -= 1 else: Ans += i Expected = i - 1 else: break print(Ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys input = sys.stdin.readline n = int(input()) for _ in range(n): input() a = list(map(int, input().split())) l = [0] * (max(a) + 1) for i in a: l[i - 1] += 1 l.sort(reverse=True) mi = float("inf") r = 0 for x in l: if mi > 1: if x < mi: mi = x r += mi else: mi -= 1 r += mi else: break print(r)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for ii in range(int(input())): n = int(input()) a = list(map(int, input().split())) cnt = {} for i in a: if i not in cnt.keys(): cnt[i] = 1 else: cnt[i] += 1 l = sorted([i for i in cnt.values()]) min_last = l[-1] + 1 ans = 0 for i in reversed(range(len(l))): min_last = max(min(l[i], min_last - 1), 0) ans += min_last print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) a = list(map(int, input().split())) if n == 1: print(1) continue a.sort() j = 0 b = list() count = 1 while j < len(a) - 1: while j < len(a) - 1 and a[j] == a[j + 1]: j += 1 count += 1 b.append(count) j += 1 count = 1 if j == len(a) - 1: b.append(1) b.sort() b.reverse() del j cur = b[0] res = cur for j in range(1, len(b)): if b[j] == cur: res += b[j] - 1 cur = b[j] - 1 elif b[j] < cur: res += b[j] cur = b[j] else: res += cur - 1 cur = cur - 1 if cur <= 0: break print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys def f(tup): return tup[1] q = int(sys.stdin.readline()) for i in range(q): n = int(input()) ls = list(map(int, sys.stdin.readline().split())) ls.sort() l = [[ls[0], 1]] for z in range(1, len(ls)): k = ls[z] if l[-1][0] == k: l[-1][1] += 1 else: l.append([k, 1]) l.sort(key=f, reverse=True) ans = l[0][1] small = ans for z in range(1, len(l)): s = l[z][1] if s < small: ans += s small = s else: to = small - 1 if to <= s and to > 0: ans += to small = to if small == 1: break sys.stdout.write(str(ans) + "\n")
IMPORT FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for i in range(t): n = int(input()) b = [0] * n for i in map(int, input().split()): b[i - 1] += 1 b = sorted(b, reverse=True) c = 0 for i in range(1, len(b)): if b[i] - b[i - 1] >= 0: b[i] = b[i - 1] - 1 for i in b: if i > 0: c += i else: break print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) R = lambda: map(int, input().split()) answers = [] def solve(a): groupsNumber = dict() for groupName in a: if groupName in groupsNumber: groupsNumber[groupName] += 1 else: groupsNumber[groupName] = 1 values = list(groupsNumber.values()) values.sort(reverse=True) sumResult = 0 lastAddedValue = values[0] for i in range(0, len(values)): if i == 0: sumResult += lastAddedValue continue if values[i] >= lastAddedValue: lastAddedValue -= 1 if lastAddedValue == 0: return sumResult sumResult += lastAddedValue else: lastAddedValue = values[i] sumResult += lastAddedValue return sumResult for i in range(0, q): n = R() confets = list(R()) result = solve(confets) answers.append(result) [print(x) for x in answers]
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def main(): all_ans = [] q = int(input()) for i in range(q): n = int(input()) arr = list(map(int, input().split())) dist_nums = {} for j in arr: if j not in dist_nums.keys(): dist_nums[j] = 1 else: dist_nums[j] += 1 counts = [] for j in dist_nums.keys(): counts.append(dist_nums[j]) counts.sort(reverse=True) ans = counts[0] curr = counts[0] for j in range(1, len(counts)): if counts[j] >= curr: curr -= 1 else: curr = counts[j] ans += max(curr, 0) all_ans.append(ans) for i in all_ans: print(i) main()
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
def readInts(): return [int(x) for x in input().split()] Q = int(input()) results = [] for q in range(Q): N = int(input()) A = readInts() freqs = {} for a in A: freqs[a] = freqs.get(a, 0) + 1 counts = list(freqs.values()) counts.sort(reverse=True) result = 0 ceil = counts[0] for c in counts: result += min(ceil, c) ceil = min(ceil, c) - 1 if ceil == 0: break results.append(result) print(*results, sep="\n")
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin input = stdin.readline q = int(input()) for query in range(q): n = int(input()) l = list(map(int, input().split())) l.sort() roz = 1 for i in range(1, n): if l[i - 1] != l[i]: roz += 1 d = l + [-1] ziom = [0] * roz count = 1 j = 0 for i in range(1, n + 1): if d[i] == d[i - 1]: count += 1 else: ziom[j] = count j += 1 count = 1 ziom.sort() thres = 1000000000000000000000 wyn = 0 i = len(ziom) - 1 while True: if thres <= 0 or i < 0: print(wyn) break wyn += min(thres - 1, ziom[i]) thres = min(thres - 1, ziom[i]) i -= 1
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for i in range(t): n = int(input()) b = [0] * n for i in map(int, input().split()): b[i - 1] += 1 b.sort(reverse=True) m = int(1000000000.0) ans = 0 for i in b: m = max(min(m - 1, i), 0) ans = ans + m print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) candies = list(map(int, input().split())) count = dict() for i in candies: if i in count.keys(): count[i] += 1 else: count[i] = 1 vals = [] for i in count.keys(): vals.append(count[i]) vals.sort(reverse=True) curr = vals[0] ans = 0 for i in vals: if curr <= 0: break else: if i < curr: curr = i ans += curr curr -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin for f in range(int(input())): junk = int(stdin.readline()) data = [int(i) for i in input().split()] dic = [0] * (junk + 1) for d in data: dic[d] += 1 dic.sort(reverse=True) ret = 0 curr = dic[0] + 1 for a in dic: if a < curr: curr = a else: curr -= 1 ret += curr if curr == 1 or a == 0: break print(ret)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for i in range(q): n = int(input()) a = [int(i) for i in input().split()] A = set() numbers = {} for i in a: if i not in A: numbers[i] = 1 A.add(i) else: numbers[i] += 1 new = [] for i in A: new.append(numbers[i]) new.sort(key=lambda x: -x) now = new[0] s = new[0] for i in range(1, len(new)): if now == 0: break if new[i] >= now: now = now - 1 s += now else: now = new[i] s += new[i] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for j in range(q): n = int(input()) a = list(map(int, input().split())) b = [0] * n for i in a: b[i - 1] += 1 b.sort() k = b[n - 1] + 1 ans = 0 for i in range(n - 1, -1, -1): if b[i] >= k: b[i] = k - 1 k = b[i] ans += max(0, b[i]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) a = sorted(list(map(int, input().split()))) s = [] last = a[0] count = 1 for i in range(1, n): if a[i] != last: s.append(count) last = a[i] count = 0 count += 1 s.append(count) s.sort() answer = s[-1] for i in range(len(s) - 2, -1, -1): if s[i] >= s[i + 1]: s[i] = s[i + 1] - 1 if not s[i]: break answer += s[i] print(answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for q in range(int(input())): n = int(input()) A = list(map(int, input().split())) G = {} for i in A: if i not in G: G[i] = 0 G[i] += 1 B = [] for i in G: B.append(G[i]) B.sort(reverse=True) cnt = 0 op = B[0] + 1 for i in B: if i >= op: op -= 1 cnt += max(0, op) else: op = i cnt += i if op == 0: break print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for q in range(int(input())): n = int(input()) a = list(map(int, input().split(" "))) freq = {} for i in a: if i in freq: freq[i] += 1 else: freq[i] = 1 l = [] for key, value in freq.items(): l.append(value) l.sort() mval = l[-1] su = l[-1] for i in range(len(l) - 2, -1, -1): if mval > 0: if l[i] == mval: mval -= 1 su += mval elif l[i] < mval: su += l[i] mval = l[i] else: mval -= 1 su += mval print(su)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout M = lambda: list(map(int, stdin.readline().split())) q = int(stdin.readline()) for qur in range(q): n = int(stdin.readline()) C = [0] * (n + 1) F = [0] * (n + 1) for i in range(n): a, f = M() F[a] += f C[a] += 1 C = [(C[i], F[i]) for i in range(1, n + 1)] C.sort(reverse=True) s = set() ans = 0 nm = 0 i = 0 for mx in range(n, 0, -1): while i < len(C) and C[i][0] == mx: s.add((C[i][1], i)) i += 1 if len(s): e = max(s) ans += mx nm += min(e[0], mx) s.remove(e) stdout.write(str(ans) + " " + str(nm) + "\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) b = [0] * (n + 1) c = [] for i in range(n): b[a[i]] += 1 b.sort(reverse=True) last = n + 1 for j in range(n + 1): if last == 0 or b[j] == 0: break if b[j] >= last and last != 0: c.append(last - 1) last -= 1 else: c.append(b[j]) last = b[j] print(sum(set(c)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) r = [] for _ in range(t): cnt, rng, res = {}, {}, set() n = int(input()) arr = list(map(int, input().split())) for i in arr: if i in cnt: cnt[i] += 1 else: cnt[i] = 1 for i in cnt.values(): if i in rng: rng[i] += 1 else: rng[i] = 1 for it in sorted(rng.keys(), reverse=True): i, j = it, 0 while i > 0 and j < rng[it]: if len(res) > 0 and min(res) <= i: i -= 1 continue res.add(i) i -= 1 j += 1 r.append(sum(res)) print("\n".join(map(str, r)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR DICT DICT FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for yo in range(t): n = int(input()) l = input() l = l.split(" ") d1 = {} d2 = {} for i in l: k = int(i) p = d1.get(k, -1) if p == -1: d1[k] = 1 else: d1[k] += 1 ans = 0 for u, v in d1.items(): if d2.get(v, -1) == -1: d2[v] = 1 else: d2[v] += 1 d3 = {} for u, v in d2.items(): k = 0 i = 0 while i < v: if k >= u: break if d3.get(u - k, -1) == -1: d3[u - k] = 1 ans += u - k i += 1 k += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for case in range(int(input())): n = int(input()) a = list(map(int, input().split())) ha = {} p = 0 cnt = [] sta = [0] mx = 0 for x in a: if not x in ha.keys(): ha[x] = p p += 1 cnt.append(1) continue cnt[ha[x]] += 1 cnt.sort() s = 0 for x in cnt: if x > mx: for y in range(mx + 1, x + 1): sta.append(y) l, r = 0, len(sta) - 1 while l <= r: mid = l + r >> 1 if sta[mid] <= x: l = mid + 1 else: r = mid - 1 s += sta[r] if r: sta.pop(r) mx = max(x, mx) print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys q = int(sys.stdin.readline()) ans_arr = [] for x in range(q): n = int(sys.stdin.readline()) a = [int(i) for i in sys.stdin.readline().split()] a.sort() arr = [] prev = -2 ind = -1 for x in range(n): if a[x] != prev: arr.append(1) ind += 1 else: arr[ind] += 1 prev = a[x] arr.sort() answer = 0 last = arr[-1] + 1 for g in range(len(arr) - 1, -1, -1): val = max(0, min(last - 1, arr[g])) answer += val last = val ans_arr.append(str(answer)) print("\n".join(ans_arr))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for w in range(q): n = int(input()) l = [int(i) for i in input().split()] l1 = [0] * n for i in range(n): l1[l[i] - 1] += 1 l1 = sorted(l1, reverse=True) s = 0 k = l1[0] for i in range(n - 1): s += k k = max(0, min(k - 1, l1[i + 1])) if k == 0: break s += k print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in range(int(input())): n = int(input()) x = list(map(int, input().split())) a = n * [0] for j in range(n): a[x[j] - 1] += 1 a.sort(reverse=True) z = a[0] - 1 ans = a[0] for k in range(1, n): if a[k] <= z: z = a[k] ans += z z -= 1 if z < 0: break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) res = [] for x in range(q): n = int(input()) a = {} for i in input().split(): i = int(i) if i in a: a[i] += 1 else: a[i] = 1 M = n + 1 r = 0 while M != 0 and a != {}: maxId = max(a, key=a.get) if a[maxId] >= M: M -= 1 else: M = a[maxId] a.pop(maxId) r += M res.append(r) for i in res: print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout q = int(input()) for i in range(q): n = int(input()) l = [] a = [(int(x) - 1) for x in stdin.readline().split()] for _ in [0] * n: l.append(0) for j in a: l[j] += 1 l.sort(reverse=True) naj = l[0] ans = l[0] for j in range(1, n): if l[j] >= naj: naj -= 1 ans += naj else: naj = l[j] ans += l[j] if l[j] == 0 or naj <= 0: break stdout.write(f"{ans}\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR FOR VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
T = int(input()) for _ in range(T): n = int(input()) xx = 0 ma = [0] * (n + 1) vis = [0] * (n + 1) v = [] pre = [int(i) for i in input().split()] for i in range(n): if ma[pre[i]] == 0: v.append(pre[i]) ma[pre[i]] += 1 xx = max(xx, ma[pre[i]]) for i in v: vis[ma[i]] += 1 ans = 0 cnt = 0 for i in range(xx, 0, -1): if vis[i] > 0: ans += i cnt += vis[i] - 1 elif cnt > 0: ans += i cnt -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout T = int(stdin.readline()) for i in range(T): n = int(stdin.readline()) s = list(map(int, stdin.readline().split())) p = [0] * (n + 1) q = [0] * (n + 1) for j in range(n): q[s[j]] += 1 for j in range(1, n + 1): p[q[j]] += 1 x = n ans = 0 cnt = 0 while x > 0: cnt += p[x] if cnt > 0: ans += x cnt -= 1 x -= 1 print(ans) s.clear()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout input = stdin.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} for i in a: d[i] = d.get(i, 0) + 1 ans = 0 b = list(d.values()) b.sort() ans = 0 s = set() for i in range(len(b)): while b[i] > 0 and b[i] in s: b[i] -= 1 ans += b[i] s.add(b[i]) stdout.write(str(ans) + "\n")
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) count = {} for num in arr: if num not in count: count[num] = 1 else: count[num] += 1 ans = sorted(count.values())[::-1] current = ans[0] final = ans[0] for i in range(1, len(ans)): if ans[i] >= current: current -= 1 else: current = ans[i] final += current if current == 0: break print(final)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys input = sys.stdin.readline m = int(input()) for ii in range(m): n = int(input()) l = list(map(int, input().split())) s = [(0) for _ in range(n)] for x in l: s[x - 1] += 1 s = sorted(s) s.reverse() for i in range(1, n): if s[i] >= s[i - 1]: s[i] = max(s[i - 1] - 1, 0) print(sum(s))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for j in range(q): n = int(input()) d = list(map(int, input().split())) d.sort() a = [(0) for i in range(d[-1] + 1)] for i in range(len(d)): a[d[i]] += 1 a.sort() a = a[::-1] t = a[0] for i in range(1, len(a)): if a[i] < a[i - 1]: t += a[i] else: t += max(a[i - 1] - 1, 0) a[i] = a[i - 1] - 1 print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin input = stdin.readline def sum(b, a): return a * (a + 1) // 2 - b * (b + 1) // 2 for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} for i in a: if i in d: d[i] += 1 else: d[i] = 1 c = 0 s = {} k = sorted(list(d.values()), reverse=True) now = k[0] + 1 for i in range(len(k)): now = min(now - 1, k[i]) if now > 0: c += now print(c)
ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys input = sys.stdin.readline q = int(input()) for _ in range(q): n = int(input()) a = list(map(int, input().split())) memo = {} for num in a: if num not in memo: memo[num] = 1 else: memo[num] += 1 tmp = [0] * len(memo) for i, num in enumerate(memo): tmp[i] = memo[num] tmp = sorted(tmp, reverse=True) ans = 0 max_num = 10**9 + 7 for num in tmp: if max_num <= num: max_num = max(0, max_num - 1) ans += max_num else: ans += num max_num = num print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys input = sys.stdin.readline def ecount(X): c, ans = 1, [] for i in range(len(X) - 1): if X[i] == X[i + 1]: c += 1 else: ans.append(c) c = 1 ans.append(c) return ans for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) a.sort() b = ecount(a) b.sort(reverse=1) ans = b[0] m = b[0] for i in b[1:]: m = min(max(0, m - 1), i) ans += m print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) l1 = [] cnt = 0 l.sort() for i in range(n - 1): cnt += 1 if l[i] != l[i + 1]: l1.append(cnt) cnt = 0 l1.append(l.count(l[n - 1])) l1.sort() l1.reverse() cnt = l1[0] sum = l1[0] for i in range(1, len(l1)): cnt1 = l1[i] if cnt == 0: break if cnt <= cnt1: cnt -= 1 else: cnt = cnt1 sum += cnt print(sum)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) L = lambda: list(map(int, input().split())) for quer in range(q): n = int(input()) a = L() c = [0] * (n + 1) for i in a: c[i] += 1 c.sort(reverse=True) mx = n + 1 ans = 0 for i in c: if i > 0 and mx > 1: if i < mx: ans += i mx = i else: ans += mx - 1 mx -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in range(int(input())): n = int(input()) d = [0] * n for i in map(int, input().split()): d[i - 1] += 1 s = sorted(d) s.reverse() for i in range(1, n): if s[i] >= s[i - 1]: s[i] = max(s[i - 1] - 1, 0) print(sum(s))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): a = int(input()) b = list(map(int, input().split())) e = {} for i in b: e[i] = 0 for i in b: e[i] += 1 d = list(e.values()) d.sort(reverse=True) k = 0 l = 100000000000 for i in d: if i < l: k += i l = i else: k += max(l - 1, 0) l = max(l - 1, 0) if l == 0: break print(k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for q in range(int(input())): n = int(input()) l = list(map(int, input().split())) l.sort() summ = 0 i = 0 count = 0 c = [] prev = l[0] while i < len(l): if prev == l[i]: count += 1 else: c.append(count) count = 1 prev = l[i] i += 1 c.append(count) cn = sorted(c, reverse=True) i = 0 maxx = cn[0] while i < len(cn) and maxx > 0: if maxx > cn[i]: maxx -= 1 else: summ += maxx maxx -= 1 i += 1 print(summ)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for _ in range(q): n = int(input()) P = [0] * (n + 1) A = [int(x) for x in input().split()] for i in A: P[i] += 1 P.sort(reverse=True) C = [P[0]] for i in P[1:]: if i == 0 or C[-1] == 1: break if i >= C[-1]: C.append(C[-1] - 1) else: C.append(i) print(sum(C))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
q = int(input()) for w in range(q): n = int(input()) arr = list(map(int, input().split())) dic = [(0) for i in range(n + 1)] kya = [] for i in range(n): dic[arr[i]] += 1 ss = dic ss.sort(reverse=True) nn = n ans = ss[0] prev = ss[0] for i in range(1, nn): if prev <= 0: break if prev <= ss[i]: prev = prev - 1 ans += prev else: prev = ss[i] ans += prev print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
import sys for _ in range(int(input())): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) if n == 1: print(1) continue if n == 2: if a[0] == a[1]: print(2) else: print(1) continue d = {} for i in a: if i not in d: d[i] = 0 d[i] += 1 vals = list(d.values()) vals.sort(reverse=True) s = 0 l = max(vals) + 1 for i in vals: if i < l: s += i l = i elif l - 1 > 0: s += l - 1 l = l - 1 else: break print(s)
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in range(int(input())): mmm = {} n = int(input()) for a in map(int, input().split()): if a not in mmm: mmm[a] = 1 else: mmm[a] += 1 s = 0 while len(mmm) != 0: items = sorted(mmm.items(), key=lambda x: -x[1]) s += items[0][1] for i in items: if i[1] == items[0][1]: if i[1] == 1: mmm.pop(i[0]) else: mmm[i[0]] -= 1 if items[0][0] in mmm: mmm.pop(items[0][0]) print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) arr = list(map(int, stdin.readline().split())) d = {} for i in arr: if i in d: d[i] += 1 else: d[i] = 1 cnt = list(d[i] for i in d) cnt.sort(reverse=True) ans = [] ans.append(cnt[0]) for i in range(1, len(cnt)): if cnt[i - 1] != 0: while cnt[i] > 0: if cnt[i] not in ans: ans.append(cnt[i]) break else: cnt[i] -= 1 else: break stdout.write("{}\n".format(sum(ans)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER WHILE VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
T = int(input()) for i in range(T): n = int(input()) s = [list(map(int, input().split()))] p = {} q = {} for it in s: for j in range(n): if it[j] in q: q[it[j]] += 1 else: q[it[j]] = 1 for j in range(1, n + 1): if j in q: if q[j] in p: p[q[j]] += 1 else: p[q[j]] = 1 x = n ans = 0 cnt = 0 while x > 0: if x in p: cnt += p[x] if cnt > 0: ans += x cnt -= 1 x -= 1 print(ans) p.clear() q.clear() s.clear()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): a = int(input()) b = list(map(int, input().split())) c = [0] * (a + 1) d = [0] * (a + 1) for i in b: c[i] += 1 c.sort(reverse=True) for i in c: d[i] += 1 k = 0 l = 0 for j in range(a, 0, -1): if d[j] > 0: k += j l += d[j] - 1 elif l > 0: k += j l -= 1 print(k)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split(" "))) a.sort() c = [] count = 0 m = a[0] for i in range(n): if a[i] == m: count += 1 else: c.append(count) m = a[i] count = 1 c.append(count) c.sort(reverse=True) sum = c[0] max = c[0] - 1 for i in range(1, len(c)): if c[i] >= max: sum += max max -= 1 else: sum += c[i] max = c[i] - 1 if max <= 0: break print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for i in range(int(input())): n = int(input()) line = list(map(int, input().split())) d = {} for i in line: d[i] = d.get(i, 0) + 1 values = sorted(d.values(), reverse=True) m = values[0] - 1 size = values[0] for i in range(1, len(values)): if m <= 0: break if values[i] > m: size += m m -= 1 else: m = values[i] - 1 size += m + 1 print(size)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
This problem is actually a subproblem of problem G from the same contest. There are $n$ candies in a candy box. The type of the $i$-th candy is $a_i$ ($1 \le a_i \le 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 $1$ and two candies of type $2$ 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 $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$ ($1 \le q \le 2 \cdot 10^5$) β€” the number of queries. Each query is represented by two lines. The first line of each query contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of candies. The second line of each query contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the type of the $i$-th candy in the box. It is guaranteed that the sum of $n$ over all queries does not exceed $2 \cdot 10^5$. -----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 -----Note----- In the first query, you can prepare a gift with two candies of type $8$ and one candy of type $5$, totalling to $3$ candies. Note that this is not the only possible solution β€” taking two candies of type $4$ and one candy of type $6$ is also valid.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) vic = {} for i in arr: if i in vic: vic[i] += 1 else: vic[i] = 1 ans = [] for i in vic: x = vic[i] while x in ans and x > 0: x -= 1 ans.append(x) print(sum(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR