#P6615. 草莓 Strawberry

草莓 Strawberry

Description

Given a tree TT, both vertices and edges have weights.

A simple path is a path that does not pass through any vertex more than once. It can be proved that the simple path between two nodes uu and vv is unique; we denote it by P(u,v)P(u,v). The length of a path is the sum of the weights of all edges on it.

You have two sets SS and GG.

At the beginning, you must choose one node as the current node and add it to the set SS. Then you may perform any number of operations. One operation is as follows:

Suppose your current node is uu. You may choose a node vv such that vuv \neq u, then update your current node to vv, add node vv to the set SS, and add the path P(u,v)P(u,v) to the set GG.

After you finish all operations, the profit you get is: the minimum of (the sum of vertex weights of all nodes in SS) ×\times (the sum of lengths of all paths in GG). If GG is empty, we consider the value of the second term to be 00.

Note that both SS and GG are sets, not multisets. This means that even if you add the same node to SS multiple times, it is counted only once when computing the vertex-weight sum.

Find the maximum possible profit.

Input Format

The first line contains an integer nn.

The next line contains nn integers a1,a2ana_1,a_2 \ldots a_n, representing the vertex weights of these nn nodes.

The next n1n - 1 lines each contain three integers u,v,wu,v,w, indicating that there is an edge of weight ww between uu and vv.

Output Format

Output one integer, the maximum profit.

6
1 4 2 8 5 7
1 2 3
4 1 2
1 5 1
2 3 4
3 6 1
198

Hint

This problem uses bundled testdata.

Subtask ID nn Special Property Score
1 10\leq 10 5
2 100\leq 100 13
3 900\leq 900 8
4 214748\leq 214748 The tree is a chain. 13
5 ai=0a_i=0 1
6 60

For all data, it is guaranteed that 1n2147481 \leq n \leq 214748, 0ai100000 \leq a_i \leq 10000, and 1w100001 \leq w \leq 10000.


Sample #1 Explanation

The graph given in the first sample is as follows.

EWyWjg.png

Translated by ChatGPT 5