#P2986. [USACO10MAR] Great Cow Gathering G

[USACO10MAR] Great Cow Gathering G

Description

Bessie is planning the annual grand cow gathering, with cows coming from all over the country. Of course, she will choose the most convenient place to hold it.

Each cow lives on one of the NN farms, which are connected by N1N-1 roads so that any farm is reachable from any other. Road ii connects farms AiA_i and BiB_i with length LiL_i. The gathering may be held at any of the NN farms. In addition, farm ii contains CiC_i cows.

When choosing the location, Bessie wants to maximize convenience (that is, minimize inconvenience). If farm XX is chosen as the location, its inconvenience is the sum, over all other farms, of the distance each cow must travel to attend. For example, if the distance from farm ii to farm XX is 2020, then the total distance contributed by farm ii is Ci×20C_i \times 20. Help Bessie find the most convenient farm for the gathering.

Input Format

  • Line 1: an integer NN.
  • Lines 2 to N+1N+1: line i+1i+1 contains a single integer CiC_i.
  • Lines N+2N+2 to 2N2N: line i+N+1i+N+1 contains 33 integers AiA_i, BiB_i, and LiL_i.

Output Format

A single line containing one integer, the minimum inconvenience.

5 
1 
1 
0 
0 
2 
1 3 1 
2 3 2 
3 4 3 
4 5 3 

15 

Hint

Constraints

1N1051 \leq N \leq 10^5, 1AiBiN1 \leq A_i \leq B_i \leq N, 0Ci,Li1030 \leq C_i, L_i \leq 10^3.

Translated by ChatGPT 5