您好,欢迎来到百家汽车网。
搜索
您的当前位置:首页Berland Regional

Berland Regional

来源:百家汽车网

H. Berland Regional

Problem

Polycarp is an organizer of a Berland ICPC regional event. There are n n n universities in Berland numbered from 1 1 1 to n n n. Polycarp knows all competitive programmers in the region. There are n n n students: the i i i-th student is enrolled at a university u i u_i ui and has a programming skill s i s_i si.

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 k k, each university will send their k k k strongest (with the highest programming skill s s s) students in the first team, the next k k k strongest students in the second team and so on. If there are fewer than k k 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 0 0.

Help Polycarp to find the strength of the region for each choice of k k k from 1 1 1 to n n n.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of testcases.

The first line of each testcase contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105) — the number of universities and the number of students.

The second line of each testcase contains n n n integers u 1 , u 2 , … , u n u_1, u_2, \dots, u_n u1,u2,,un ( 1 ≤ u i ≤ n 1 \le u_i \le n 1uin) — the university the i i i-th student is enrolled at.

The third line of each testcase contains n n n integers s 1 , s 2 , … , s n s_1, s_2, \dots, s_n s1,s2,,sn ( 1 ≤ s i ≤ 1 0 9 1 \le s_i \le 10^9 1si109) — the programming skill of the i i i-th student.

The sum of n n n over all testcases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

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

Example

Input
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 23 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083
Output
29 28 26 19 0 0 0 
24907 20705 22805 9514 0 0 0 0 0 0 
43 43 43 32 38 43 
3083 

Note

In the first testcase the teams from each university for each k k k are:

  • k = 1 k=1 k=1:
    • university 1 1 1: [ 6 ] , [ 5 ] , [ 5 ] , [ 3 ] [6], [5], [5], [3] [6],[5],[5],[3];
    • university 2 2 2: [ 8 ] , [ 1 ] , [ 1 ] [8], [1], [1] [8],[1],[1];
  • k = 2 k=2 k=2:
    • university 1 1 1: [ 6 , 5 ] , [ 5 , 3 ] [6, 5], [5, 3] [6,5],[5,3];
    • university 2 2 2: [ 8 , 1 ] [8, 1] [8,1];
  • k = 3 k=3 k=3:
    • university 1 1 1: [ 6 , 5 , 5 ] [6, 5, 5] [6,5,5];
    • university 2 2 2: [ 8 , 1 , 1 ] [8, 1, 1] [8,1,1];
  • k = 4 k=4 k=4:
    • university 1 1 1: [ 6 , 5 , 5 , 3 ] [6, 5, 5, 3] [6,5,5,3];

Code

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll N=2e5+10;
vector<ll> v[N];
ll a[N],b[N];
ll s[N];

void solve()
{
    ll n;
    cin>>n;

    for(ll i=1;i<=n;i++)
    {
        v[i].clear();
        s[i]=0;
    }

    for(ll i=1;i<=n;i++) cin>>a[i];
    for(ll i=1;i<=n;i++) cin>>b[i];
    for(ll i=1;i<=n;i++) v[a[i]].push_back(b[i]);

    for(ll i=1;i<=n;i++)
    {
        if(v[i].size())
        {
            sort(v[i].begin(),v[i].end(),greater<ll>());
            for(ll j=1;j<v[i].size();j++) v[i][j]+=v[i][j-1];
        }
    }

    for(ll i=1;i<=n;i++)
    {
        if(v[i].size())
        {
            for(ll j=1;j<=v[i].size();j++) s[j]+=v[i][v[i].size()/j*j-1];
        }
    }

    for(ll i=1;i<=n;i++) cout<<s[i]<<" ";
    cout<<endl;
}

int main()
{
    ll t;
    cin>>t;
    while(t--) solve();
    
    return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baijiahaobaidu.com 版权所有 湘ICP备2023023988号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务