Planets (Codeforces 1730A)
Tags: Greedy, sortings

One day, Vogons wanted to build a new hyperspace highway through a distant system with $$$n$$$ planets. The $$$i$$$-th planet is on the orbit $$$a_i$$$, there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed.Vogons have two machines to do that.  The first machine in one operation can destroy any planet at cost of $$$1$$$ Triganic Pu.  The second machine in one operation can destroy all planets on a single orbit in this system at the cost of $$$c$$$ Triganic Pus. Vogons can use each machine as many times as they want.Vogons are very greedy, so they want to destroy all planets with minimum amount of money spent. Can you help them to know the minimum cost of this project?

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then the test cases follow.Each test case consists of two lines.The first line contains two integers $$$n$$$ and $$$c$$$ ($$$1 \le n, c \le 100$$$) — the number of planets and the cost of the second machine usage.The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$), where $$$a_i$$$ is the orbit of the $$$i$$$-th planet.

Output: For each test case print a single integer — the minimum cost of destroying all planets.

Note: In the first test case, the cost of using both machines is the same, so you can always use the second one and destroy all planets in orbit $$$1$$$, all planets in orbit $$$2$$$, all planets in orbit $$$4$$$, all planets in orbit $$$5$$$.In the second test case, it is advantageous to use the second machine for $$$2$$$ Triganic Pus to destroy all the planets in orbit $$$2$$$, then destroy the remaining two planets using the first machine.In the third test case, you can use the first machine twice or the second machine once.In the fourth test case, it is advantageous to use the first machine twice.

Sample Inputs:
4
10 1
2 1 4 5 2 4 5 5 1 2
5 2
3 2 1 2 2
2 2
1 1
2 2
1 2

1
1 100
1

Sample Outputs:
4
4
2
2

1

Solution (Python):
t = int(input())
for _ in range(t):
    (n, c) = map(int, input().split())
    ps = list(map(int, input().split()))
    dic = {}
    for p in ps:
        if p not in dic:
            dic[p] = 1
        else:
            dic[p] += 1
    res = 0
    for (k, v) in dic.items():
        res += v if v <= c else c
    print(res)

Solution (Python):
for _ in range(int(input())):
    (n, c) = map(int, input().split())
    a = [int(i) for i in input().split()]
    b = [0 for k in range(100)]
    count = 0
    for k in range(len(a)):
        b[a[k] - 1] += 1
    for j in range(len(b)):
        if b[j] >= c:
            count += c
        else:
            count += b[j]
    print(count)

Solution (Python):
t = int(input())
while t:
    l_str = input()
    l_str = l_str.split()
    l_int = [int(i) for i in l_str]
    (n, cost) = (l_int[0], l_int[1])
    planets_str = input()
    planets_str = planets_str.split()
    planets_int = [int(i) for i in planets_str]
    my_dict = {}
    planets_int.sort()
    for i in planets_int:
        if i in my_dict:
            my_dict[i] = my_dict[i] + 1
        else:
            my_dict[i] = 1
    ans = 0
    for i in my_dict:
        ans = ans + min(cost, my_dict[i] * 1)
    print(ans)
    t = t - 1