Berland Regional (Codeforces 1519C)
Tags: Greedy, number theory

Polycarp is an organizer of a Berland ICPC regional event. There are $$$n$$$ universities in Berland numbered from $$$1$$$ to $$$n$$$. Polycarp knows all competitive programmers in the region. There are $$$n$$$ students: the $$$i$$$-th student is enrolled at a university $$$u_i$$$ and has a programming skill $$$s_i$$$.Polycarp has to decide on the rules now. In particular, the number of members in the team.Polycarp knows that if he chooses the size of the team to be some integer $$$k$$$, each university will send their $$$k$$$ strongest (with the highest programming skill $$$s$$$) students in the first team, the next $$$k$$$ strongest students in the second team and so on. If there are fewer than $$$k$$$ students left, then the team can't be formed. Note that there might be universities that send zero teams.The strength of the region is the total skill of the members of all present teams. If there are no teams present, then the strength is $$$0$$$.Help Polycarp to find the strength of the region for each choice of $$$k$$$ from $$$1$$$ to $$$n$$$.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases.The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of universities and the number of students.The second line of each testcase contains $$$n$$$ integers $$$u_1, u_2, \dots, u_n$$$ ($$$1 \le u_i \le n$$$) — the university the $$$i$$$-th student is enrolled at.The third line of each testcase contains $$$n$$$ integers $$$s_1, s_2, \dots, s_n$$$ ($$$1 \le s_i \le 10^9$$$) — the programming skill of the $$$i$$$-th student.The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each testcase print $$$n$$$ integers: the strength of the region — the total skill of the members of the present teams — for each choice of team size $$$k$$$.

Note: In the first testcase the teams from each university for each $$$k$$$ are:   $$$k=1$$$:   university $$$1$$$: $$$[6], [5], [5], [3]$$$;  university $$$2$$$: $$$[8], [1], [1]$$$;   $$$k=2$$$:   university $$$1$$$: $$$[6, 5], [5, 3]$$$;  university $$$2$$$: $$$[8, 1]$$$;   $$$k=3$$$:   university $$$1$$$: $$$[6, 5, 5]$$$;  university $$$2$$$: $$$[8, 1, 1]$$$;   $$$k=4$$$:   university $$$1$$$: $$$[6, 5, 5, 3]$$$;  

Sample Inputs:
4
7
1 2 1 2 1 2 1
6 8 3 1 5 1 5
10
1 1 1 2 2 2 2 3 3 3
3435 3014 2241 2233 2893 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083

Sample Outputs:
29 28 26 19 0 0 0 
24907 20705 22805 9514 0 0 0 0 0 0 
43 43 43 32 38 43 
3083

Solution (Python):
def solve():
    J = int(input())
    for O in range(J):
        F = int(input())
        K = map(int, input().split())
        L = map(int, input().split())
        B = {}
        for (C, M) in zip(K, L):
            if C not in B:
                B[C] = []
            B[C].append(M)
        for A in B.values():
            A.sort(reverse=True)
        for A in B.values():
            for G in range(1, len(A)):
                A[G] += A[G - 1]
        H = [0] * F
        for A in B.values():
            for D in range(1, F + 1):
                I = 0
                E = len(A) // D
                if E == 0:
                    break
                if E > 0:
                    N = A[E * D - 1]
                    I += N
                H[D - 1] += I
        print(' '.join([str(A) for A in H]))
if __name__ == '__main__':
    solve()

Solution (Python):
from sys import stdin, stdout
import itertools

def main():
    t = int(stdin.readline())
    for __ in range(t):
        n = int(stdin.readline())
        u = map(int, stdin.readline().split(), itertools.repeat(10, n))
        s = map(int, stdin.readline().split(), itertools.repeat(10, n))
        students = [list() for __ in range(n)]
        for (ui, si) in zip(u, s):
            students[ui - 1].append(si)
        ans = [0] * n
        for skills in filter(None, students):
            skills.sort()
            l = len(skills)
            partsum = [0] * (l + 1)
            for (i, si) in enumerate(skills):
                partsum[i + 1] = partsum[i] + si
            for k in range(1, l + 1):
                ans[k - 1] += partsum[-1] - partsum[l % k]
        stdout.write(' '.join(map(str, ans)))
        stdout.write('\n')
if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

def solve():
    mem = defaultdict(list)
    N = len(uni)
    for i in range(N):
        mem[uni[i]].append(stu[i])
    ans = [0 for i in range(N)]
    for un in mem:
        arr = mem[un]
        arr.sort(reverse=True)
        for i in range(1, len(arr)):
            arr[i] += arr[i - 1]
        lim = len(arr)
        for i in range(1, lim + 1):
            rem = lim % i
            ans[i - 1] += arr[lim - rem - 1]
    return ' '.join(map(str, ans))
if __name__ == '__main__':
    T = int(input())
    for t in range(1, T + 1):
        n = int(input())
        uni = list(map(int, input().split()))
        stu = list(map(int, input().split()))
        ans = solve()
        print(ans)