[SOLUTION] Berland Regional solution codeforces – Polycarp is an organizer of a Berland ICPC regional event – 2021

Berland Regional solution codeforces

 Berland Regional solution codeforces

Polycarp is an organizer of a Berland ICPC regional event. There are nn universities in Berland numbered from 11 to nn. Polycarp knows all competitive programmers in the region. There are nn students: the ii-th student is enrolled at a university uiui and has a programming skill sisi.

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

Help Polycarp to find the strength of the region for each choice of kk from 11 to nn. Berland Regional solution codeforces

 Berland Regional solution codeforces
Input : Berland Regional solution codeforces

The first line contains a single integer tt (1t10001≤t≤1000) — the number of testcases.

The first line of each testcase contains a single integer nn (1n21051≤n≤2⋅105) — the number of universities and the number of students.

The second line of each testcase contains nn integers u1,u2,,unu1,u2,…,un (1uin1≤ui≤n) — the university the ii-th                                                student is enrolled at.

The third line of each testcase contains nn integers s1,s2,,sns1,s2,…,sn (1si1091≤si≤109) — the programming skill of the ii-th student.

The sum of nn over all testcases doesn’t exceed 21052⋅105. Berland Regional solution codeforces

Output : Berland Regional solution codeforces

For each testcase print nn integers: the strength of the region — the total skill of the members of the present teams — for each choice of team size kk. Berland Regional solution codeforces

Example: Berland Regional solution codeforces

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 2893 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083

output : Berland Regional solution codeforces

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 : Berland Regional solution codeforces

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

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

 Berland Regional solution codeforces

Program Code :

C++

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define M1 1000000007
#define M2 998244353
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define REPI(i,a,b) for(ll i=b-1;i>=a;i--)
#define F first
#define S second
#define PB push_back
#define DB pop_back
#define MP make_pair
#define MT make_tuple
#define G(a,b) get<a>(b)
#define V(a) vector<a>

static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T>
#define o_set(T) tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set

pll Egcd(ll,ll);
pll Egcd(ll x,ll y)
{
if(x==0) return MP(0,1);
pll t=Egcd(y%x,x);
return MP(t.S-t.F*(y/x),t.F);
}

ll powM(ll x,ll y,ll m)
{
ll ans=1,r=1;
x%=m;
while(r>0&&r<=y)
{
if(r&y)
{
ans*=x;
ans%=m;
}
r<<=1;
x*=x;
x%=m;
}
return ans;
}

ll modI(ll a, ll m)
{
ll m0=m,y=0,x=1;
if(m==1) return 0;
while(a>1)
{
ll q=a/m;
ll t=m;
m=a%m;
a=t;
t=y;
y=x-q*y;
x=t;
}
if(x<0) x+=m0;
return x;
}

void Miden(ll **p1,ll n)
{
ll (*x)[n]=(ll(*)[n]) p1;
REP(i,0,n)
{
REP(j,0,n)
{
x[i][j]=0;
}
x[i][i]=1;
}
return;
}

void Mmult(ll **p1,ll **p2,ll **ans,ll x,ll y,ll z,ll m)
{
ll (*a)[y]=(ll (*)[y])p1;
ll (*b)[z]=(ll (*)[z])p2;
ll (*c)[z]=(ll (*)[z])ans;
REP(i,0,x)
{
REP(j,0,z)
{
c[i][j]=0;
REP(k,0,y)
{
c[i][j]+=a[i][k]*b[k][j];
c[i][j]%=m;
}
}
}
return;
}

void Mpow(ll **p1,ll **ans,ll n,ll y,ll m)
{
if(y==0)
{
Miden(ans,n);
return;
}
ll t[n][n];
Mpow(p1,(ll **)t,n,y/2,m);
ll z[n][n];
Mmult((ll **)t,(ll **)t,(ll **)z,n,n,n,m);
if(y%2)
{
Mmult((ll **)z,p1,ans,n,n,n,m);
}
else
{
Miden((ll **)t,n);
Mmult((ll **)z,(ll **)t,ans,n,n,n,m);
}
return;
}

bool isprime(ll n)
{
if(n<2)
return false;
for(ll x:{2,3,5,7,11,13,17,19,23,29,31,37})
{
if(n==x)
return true;
bool flag=true;
ll r=1;
ll t=1;
while(r<=((n-1)>>__builtin_ctzll(n-1)))
{
if(r&((n-1)>>__builtin_ctzll(n-1)))
t=((__int128)t*x)%n;
x=((__int128)x*x)%n;
r<<=1;
}
if(t==1||t==n-1)
flag=false;
for(r=0;r<__builtin_ctzll(n-1);r++)
{
t=((__int128)t*t)%n;
if(t==n-1)
flag=false;
}
if(flag)
return false;
}
return true;
}

ll PrimRoot(ll p,ll x)
{
//finds primitive root of prime p greater than x(If it doesnt exist, returns 0)
V(ll) v;
ll t=p-1;
REP(i,2,t+1)
{
if(i*i>t) break;
if(t%i==0)
{
v.PB((p-1)/i);
while(t%i==0)
{
t/=i;
}
}
}
if(t>1) v.PB((p-1)/t);
REP(i,x+1,p)
{
ll flag=0;
REP(j,0,((ll)v.size()))
{
if(powM(i,v[j],p)==1)
{
flag=1;
break;
}
}
if(flag==0)
{
return i;
}
}

return 0;
}

void fft(V(ll) &a,ll n,bool invert,ll m,ll x)
{
REP(i,0,n)
{
ll y=0;
REP(j,0,__builtin_ctzll(n))
{
if((1LL<<j)&i)
{
y|=(1LL<<(__builtin_ctzll(n)-j-1));
}
}
if(y>i)
{
swap(a[i],a[y]);
}
}
if(invert) x=modI(x,m);
REP(s,1,__builtin_ctzll(n)+1)
{
ll y=powM(x,(n/(1LL<<s)),m);
REP(j,0,(n/(1LL<<s)))
{
ll r=1;
REP(i,0,(1LL<<(s-1)))
{
ll u=a[i+j*(1LL<<s)];
ll v=(r*a[i+j*(1LL<<s)+(1LL<<(s-1))])%m;
a[i+j*(1LL<<s)]=u+v;
if(a[i+j*(1LL<<s)]>m) a[i+j*(1LL<<s)]-=m;
a[i+j*(1LL<<s)+(1LL<<(s-1))]=u-v;
if(a[i+j*(1LL<<s)+(1LL<<(s-1))]<0) a[i+j*(1LL<<s)+(1LL<<(s-1))]+=m;
r*=y;
r%=m;
}
}
}
if(invert)
{
ll invn=modI(n,m);
REP(i,0,n)
{
a[i]=(a[i]*invn)%m;
}
}
return;
}

void PolyMult(V(ll) &a,V(ll) &b,V(ll) &v,ll m,ll x)
{
ll n=1;
while(n<((ll)a.size())+((ll)b.size()))
{ 
n<<=1;
}
V(ll) fa(a.begin(),a.end());
fa.resize(n,0);
V(ll) fb(b.begin(),b.end());
fb.resize(n,0);
ll y=powM(x,(m-1)/n,m);
fft(fa,n,false,m,y);
fft(fb,n,false,m,y);
v.resize(n,0);
REP(i,0,n)
{
v[i]=((fa[i]*fb[i])%m);
}
fft(v,n,true,m,y);
v.resize(((ll)a.size())+((ll)b.size())-1,0LL);
return;
}

void PolyInverse(V(ll) &a,V(ll) &v,ll n,ll m,ll x)
{
v.clear();
v.PB(modI(a[0],m));
while(((ll)v.size())<n)
{
ll tmpsz=(((ll)v.size())<<1);
V(ll) tmpa(tmpsz,0LL);
REP(i,0,min(((ll)a.size()),tmpsz))
{
tmpa[i]=a[i];
}
V(ll) tmppr;
PolyMult(tmpa,v,tmppr,m,x);
tmppr.resize(tmpsz,0LL);
REP(i,0,tmpsz)
{
tmppr[i]=((m-tmppr[i])%m);
}
tmppr[0]=((tmppr[0]+2)%m);
V(ll) tmpv(v.begin(),v.end());
PolyMult(tmppr,tmpv,v,m,x);
v.resize(tmpsz,0LL);
}
v.resize(n,0LL);
return;
}

void PolyDiv(V(ll) &a,V(ll) &b,V(ll) &q,V(ll) &r,ll m,ll x)
{
if(((ll)a.size())<((ll)b.size()))
{
r=a;
r.resize(((ll)b.size())-1,0LL);
q.clear();
q.PB(0LL);
return;
}
V(ll) ra(((ll)a.size())-((ll)b.size())+1,0LL);
REP(i,0,((ll)a.size())-((ll)b.size())+1)
{
ra[i]=a[((ll)a.size())-1-i];
}
V(ll) rb(((ll)b.size()),0LL);
REP(i,0,((ll)b.size()))
{
rb[i]=b[((ll)b.size())-1-i];
}
V(ll) irb;
PolyInverse(rb,irb,((ll)a.size())-((ll)b.size())+1,m,x);
V(ll) rq;
PolyMult(ra,irb,rq,m,x);
rq.resize(((ll)a.size())-((ll)b.size())+1,0LL);
q.resize(((ll)a.size())-((ll)b.size())+1,0LL);
REP(i,0,((ll)rq.size()))
{
q[i]=rq[((ll)rq.size())-1-i];
}
V(ll) tmppr;
PolyMult(b,q,tmppr,m,x);
r.resize(((ll)b.size())-1,0LL);
REP(i,0,((ll)r.size()))
{
r[i]=((a[i]+m-tmppr[i])%m);
}
return;
}

ll fn(ll x,ll rn[])
{
if(x==rn[x])
return x;
else
return rn[x]=fn(rn[x],rn);
}

bool un(ll x,ll y,ll rn[],ll sz[])
{
x=fn(x,rn);
y=fn(y,rn);
if(x==y)
return false;
if(sz[x]<sz[y])
swap(x,y);
sz[x]+=sz[y];
rn[y]=x;
return true;
}

void build(ll v,ll tl,ll tr,ll st[],ll lz[],bool f[],ll a[])
{
if(tl==tr)
{
st[v]=a[tl];
lz[v]=0LL;
f[v]=false;
return;
}
build((v<<1),tl,((tl+tr)>>1),st,lz,f,a);
build((v<<1)|1,((tl+tr)>>1)+1,tr,st,lz,f,a);
//operation
st[v]=st[(v<<1)]+st[(v<<1)|1];
lz[v]=0LL;
f[v]=false;
return;
}

void push(ll v,ll tl,ll tr,ll st[],ll lz[],bool f[])
{
if(f[v])
{
//operation
st[(v<<1)]=lz[(v<<1)]=st[(v<<1)|1]=lz[(v<<1)|1]=0LL;
f[(v<<1)]=f[(v<<1)|1]=true;
f[v]=false;
}
//operation
st[(v<<1)]+=lz[v]*(((tl+tr)>>1)-tl+1);
//operation
lz[(v<<1)]+=lz[v];
//operation
st[(v<<1)|1]+=lz[v]*(tr-((tl+tr)>>1));
//operation
lz[(v<<1)|1]+=lz[v];
lz[v]=0LL;
return;
}

void update(ll v,ll tl,ll tr,ll l,ll r,ll val,bool set,ll st[],ll lz[],bool f[])
{
if(l>r)
{
return;
}
if(l==tl&&tr==r)
{
if(set)
{
//operation
st[v]=lz[v]=0LL;
f[v]=true;
}
//operation
st[v]+=val*(tr-tl+1);
//operation
lz[v]+=val;
return;
}
push(v,tl,tr,st,lz,f);
update((v<<1),tl,((tl+tr)>>1),l,min(r,((tl+tr)>>1)),val,set,st,lz,f);
update((v<<1)|1,((tl+tr)>>1)+1,tr,max(l,((tl+tr)>>1)+1),r,val,set,st,lz,f);
//operation
st[v]=st[(v<<1)]+st[(v<<1)|1];
return;
}

ll query(ll v,ll tl,ll tr,ll l,ll r,ll st[],ll lz[],bool f[])
{
if(l>r)
{
return 0LL;
}
if(l==tl&&tr==r)
{
return st[v];
}
push(v,tl,tr,st,lz,f);
//operation
return query((v<<1),tl,((tl+tr)>>1),l,min(r,((tl+tr)>>1)),st,lz,f)+query((v<<1)|1,((tl+tr)>>1)+1,tr,max(l,((tl+tr)>>1)+1),r,st,lz,f);
}

ll tonelli_shanks(ll x,ll p)
{
// Outputs a y such that (y^2)%p=x, or -1 if it doesn't exist

if(powM(x,(p-1)/2,p)==p-1) return -1;

ll q=((p-1)>>__builtin_ctzll(p-1)),s=__builtin_ctzll(p-1)-1;
ll r=powM(x,(q+1)/2,p),t=powM(x,q,p);

ll z;
while(true)
{
z=rng()%(p-1)+1;
if(powM(z,(p-1)/2,p)==p-1) break;
}
z=powM(z,q,p);

while(t!=1)
{
if(powM(t,1LL<<(s-1),p)==p-1)
{
t=(t*((z*z)%p))%p;
r=(r*z)%p;
}
z=(z*z)%p;
s--;
}

return r;
}

int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);

//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);

ll ntc=1;
cin>>ntc;
REP(tc,1,ntc+1)
{
//cout<<"Case #"<<tc<<": ";

ll n;
cin>>n;
V(ll) ad[n];
ll xt[n],yt[n];
REP(i,0,n) cin>>xt[i];
REP(i,0,n) cin>>yt[i];
REP(i,0,n)
{
xt[i]--;
ad[xt[i]].PB(yt[i]);
}
ll sm=0;
V(ll) ps[n];
ll ans[n+1]={};
REP(i,0,n)
{
sort(ad[i].begin(), ad[i].end());
ps[i].resize(((ll)ad[i].size())+1, 0);
REP(j,0,((ll)ad[i].size()))
{
ps[i][j+1]=ps[i][j]+ad[i][j];
}
sm+=ps[i][((ll)ad[i].size())];
REP(j,1,((ll)ad[i].size())+1)
{
ans[j]+=ps[i][((ll)ad[i].size())]-ps[i][((ll)ad[i].size())%j];
}
}
REP(i,1,n+1)
{
cout<<ans[i]<<' ';
}

cout<<'\n';
}

return 0;
}

Java

/* Author : DEBOJYOTI MANDAL
Attribute : Some of the methods are copied from GeeksforGeeks Website */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{ 
static Reader sc=new Reader();
// static FastReader sc=new FastReader(System.in);

public static void main (String[] args) throws java.lang.Exception
{
// try{
/*
int n=sc.nextInt();
ArrayList<Integer> al=new ArrayList<>();
ArrayList<Long> al=new ArrayList<>();
Set<Integer> set=new HashSet<>();
Collections.sort(al,Collections.reverseOrder());
out.println("Case #"+tt+": "+ans);
long n=sc.nextLong();
for(int i=0;i<n;i++) 
String s=sc.next();
*/
int t = sc.nextInt();
for(int tt=1;tt<=t;tt++)
{ 
int n=sc.nextInt();
ArrayList<Integer> al[]=new ArrayList[n+1];
for(int i=0;i<n+1;i++) al[i]=new ArrayList< Integer>();
int u[]=new int[n];
int s[]=new int[n];
for(int i=0;i<n;i++)
{
u[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{ s[i]=sc.nextInt();
al[u[i]].add(s[i]);
}
ArrayList<Long> all[]=new ArrayList[n+1];
for(int i=0;i<n+1;i++) all[i]=new ArrayList<Long>();
int max=0;
long res[]=new long[n+1];
for(int i=0;i<n+1;i++)
{
ArrayList<Integer> now=al[i];
if(now.size()==0) continue;
Collections.sort(now);
ArrayList<Long> pre=new ArrayList<>();
pre.add(0L);
long cur=0;
for(int j=0;j<now.size();j++)
{
cur+=now.get(j);
pre.add(cur);
}
all[i]=pre;
int si=pre.size();
if(si==0) continue;
long high=pre.get(si-1);
for(int j=1;j<pre.size();j++)
{
int mod=(si-1)%j;
if(mod<si)
res[j]+=high-pre.get(mod);
}
}
print(res);
}
out.flush();
out.close();
// } 
// catch(Exception e)
// {}
}

/*
...SOLUTION ENDS HERE...........SOLUTION ENDS HERE...
*/

static void flag(boolean flag)
{
out.println(flag ? "YES" : "NO");
out.flush();
}

/* 
Map<Long,Long> map=new HashMap<>();
for(int i=0;i<n;i++)
{
if(!map.containsKey(a[i]))
map.put(a[i],1);
else
map.replace(a[i],map.get(a[i])+1);
}

Set<Map.Entry<Long,Long>> hmap=map.entrySet();
for(Map.Entry<Long,Long> data : hmap)
{

}

Iterator<Integer> it = set.iterator();
while(it.hasNext()) 
{ 
int x=it.next();
}
*/

static void print(int a[])
{
int n=a.length;
for(int i=0;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
}
static void print(long a[])
{
int n=a.length;
for(int i=1;i<n;i++)
{
out.print(a[i]+" ");
}
out.println();
out.flush();
} 
static void print_int(ArrayList<Integer> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}
static void print_long(ArrayList<Long> al)
{
int si=al.size();
for(int i=0;i<si;i++)
{
out.print(al.get(i)+" ");
}
out.println();
out.flush();
}

static class Graph
{
int v;
ArrayList<Integer> list[];
Graph(int v)
{
this.v=v;
list=new ArrayList[v+1];
for(int i=1;i<=v;i++)
list[i]=new ArrayList<Integer>();
}
void addEdge(int a, int b)
{
this.list[a].add(b);
}
}
static void DFS(Graph g, boolean[] visited, int u)
{
visited[u]=true;
int v=0;
for(int i=0;i<g.list[u].size();i++)
{
v=g.list[u].get(i);
if(!visited[v])
DFS(g,visited,v);
}
} 

// static class Pair
// {
// int x,y;
// Pair(int x,int y)
// {
// this.x=x;
// this.y=y;
// }
// }

static long sum_array(int a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}
static long sum_array(long a[])
{
int n=a.length;
long sum=0;
for(int i=0;i<n;i++)
sum+=a[i];
return sum;
}

static void sort(int[] a) 
{
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a) 
{
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}

static void reverse_array(int a[])
{
int n=a.length;
int i,t; 
for (i = 0; i < n / 2; i++) { 
t = a[i]; 
a[i] = a[n - i - 1]; 
a[n - i - 1] = t; 
} 
}
static void reverse_array(long a[])
{
int n=a.length;
int i; long t; 
for (i = 0; i < n / 2; i++) { 
t = a[i]; 
a[i] = a[n - i - 1]; 
a[n - i - 1] = t; 
} 
}

static long gcd(long a, long b) 
{ 
if (a == 0) 
return b; 

return gcd(b%a, a); 
} 
static int gcd(int a, int b) 
{ 
if (a == 0) 
return b; 

return gcd(b%a, a); 
}

static class FastReader{

byte[] buf = new byte[2048];
int index, total;
InputStream in;

FastReader(InputStream is) {
in = is;
}

int scan() throws IOException {
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0) {
return -1;
}
}
return buf[index++];
}

String next() throws IOException {
int c;
for (c = scan(); c <= 32; c = scan());
StringBuilder sb = new StringBuilder();
for (; c > 32; c = scan()) {
sb.append((char) c);
}
return sb.toString();
}

int nextInt() throws IOException {
int c, val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}

long nextLong() throws IOException {
int c;
long val = 0;
for (c = scan(); c <= 32; c = scan());
boolean neg = c == '-';
if (c == '-' || c == '+') {
c = scan();
}
for (; c >= '0' && c <= '9'; c = scan()) {
val = (val << 3) + (val << 1) + (c & 15);
}
return neg ? -val : val;
}
}

static class Reader 
{ 
final private int BUFFER_SIZE = 1 << 16; 
private DataInputStream din; 
private byte[] buffer; 
private int bufferPointer, bytesRead; 

public Reader() 
{ 
din = new DataInputStream(System.in); 
buffer = new byte[BUFFER_SIZE]; 
bufferPointer = bytesRead = 0; 
} 

public Reader(String file_name) throws IOException 
{ 
din = new DataInputStream(new FileInputStream(file_name)); 
buffer = new byte[BUFFER_SIZE]; 
bufferPointer = bytesRead = 0; 
} 

public String readLine() throws IOException 
{ 
byte[] buf = new byte[64]; // line length 
int cnt = 0, c; 
while ((c = read()) != -1) 
{ 
if (c == '\n') 
break; 
buf[cnt++] = (byte) c; 
} 
return new String(buf, 0, cnt); 
} 

public int nextInt() throws IOException 
{ 
int ret = 0; 
byte c = read(); 
while (c <= ' ') 
c = read(); 
boolean neg = (c == '-'); 
if (neg) 
c = read(); 
do
{ 
ret = ret * 10 + c - '0'; 
} while ((c = read()) >= '0' && c <= '9'); 

if (neg) 
return -ret; 
return ret; 
} 

public long nextLong() throws IOException 
{ 
long ret = 0; 
byte c = read(); 
while (c <= ' ') 
c = read(); 
boolean neg = (c == '-'); 
if (neg) 
c = read(); 
do { 
ret = ret * 10 + c - '0'; 
} 
while ((c = read()) >= '0' && c <= '9'); 
if (neg) 
return -ret; 
return ret; 
} 

public double nextDouble() throws IOException 
{ 
double ret = 0, div = 1; 
byte c = read(); 
while (c <= ' ') 
c = read(); 
boolean neg = (c == '-'); 
if (neg) 
c = read(); 

do { 
ret = ret * 10 + c - '0'; 
} 
while ((c = read()) >= '0' && c <= '9'); 

if (c == '.') 
{ 
while ((c = read()) >= '0' && c <= '9') 
{ 
ret += (c - '0') / (div *= 10); 
} 
} 

if (neg) 
return -ret; 
return ret; 
} 

private void fillBuffer() throws IOException 
{ 
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
if (bytesRead == -1) 
buffer[0] = -1; 
} 

private byte read() throws IOException 
{ 
if (bufferPointer == bytesRead) 
fillBuffer(); 
return buffer[bufferPointer++]; 
} 

public void close() throws IOException 
{ 
if (din == null) 
return; 
din.close(); 
} 
}
static PrintWriter out=new PrintWriter(System.out);
static int int_max=Integer.MAX_VALUE;
static int int_min=Integer.MIN_VALUE;
static long long_max=Long.MAX_VALUE;
static long long_min=Long.MIN_VALUE;
}
// Thank You !

Python

I=lambda:[*map(int,input().split())];r=range
for t in r(*I()):
N,=I();U=I();S=I();A=[0]*N;X=[[0]for i in r(N)]
for i in r(N):X[U[i]-1]+=S[i],
for i in r(N):
X[i].sort();l=len(X[i]);R=r(1,l)
for j in R:X[i][j]+=X[i][j-1]
for j in R:A[j-1]+=X[i][-1]-X[i][(l-1)%j]
print(*A)

 Berland Regional solution codeforces

Berland Regional solution codeforces, Berland Regional solution codeforces , Berland Regional solution codeforces , Berland Regional solution codeforces , Berland Regional solution codeforces , Berland Regional solution codeforces, Berland Regional solution codeforces , Berland Regional solution codeforces , Berland Regional solution codeforces , Berland Regional solution codeforces ,

Also read : Flower Sequence solution codechef 2021 

Leave a Comment