Under construction

Still being built. Things may change

Warehouses

Time limit2sMemory limit1024 MB

Summary
Given a weighted road graph and each warehouse's company, find for each company the sum of max-bottleneck path values over all its warehouse pairs.
Level

Medium7 of 10

Topics
Union-find, Sorting, Hash map, Tree
Solved
No attempts yet

Problem

UCPC City has NN warehouses, and KK companies each own some of them. The warehouses are numbered from 11 to NN, and the companies are numbered from 11 to KK. MM bidirectional roads connect pairs of warehouses, and each road has a transfer limit. Two warehouses may be connected by several roads, and there is always a path between any two warehouses.

Suppose goods are shipped between two warehouses. The shipping limit between them is the smallest transfer limit among the roads on a path connecting them. If several paths connect the two warehouses, the path with the largest shipping limit is chosen.

For each company, compute the sum of the shipping limits over all pairs of warehouses owned by that company.

Each company is guaranteed to own at least 2 warehouses.

Input

The first line contains three integers NN, KK, and MM, separated by spaces. (2≤N≤100 000; 1≤K≤min⁡(N2,50 000); N−1≤M≤300 000)(2\leq N\leq 100\ 000;\ 1\leq K\leq\min(\frac{N}{2} ,50\ 000) ;\ N-1\leq M\leq 300\ 000)

The second line contains NN integers C1,C2,...,CNC_1, C_2, ..., C_N, separated by spaces. Warehouse ii is owned by company CiC_i. (1≤Ci≤K)(1\leq C_i\leq K)

The next MM lines each contain three integers XX, YY, and WW, separated by spaces. Each line describes a bidirectional road between warehouses XX and YY with transfer limit WW. (1≤X,Y≤N; 1≤W≤109)(1\leq X,Y\leq N;\ 1\leq W\leq 10^9)

Output

Print KK lines. The ii-th line contains the sum of shipping limits over all pairs of warehouses owned by company ii, for ii from 11 to KK.

Examples1

  1. Example 1

    Input
    5 2 7
    1 2 1 1 2
    1 2 3
    1 3 4
    1 5 6
    2 3 2
    2 4 7
    3 5 1
    4 5 3
    
    Expected output
    10
    3