YATP

Given a node-weighted, edge-weighted tree, for each node find the minimum of dist(u,v) + p_u*p_v over all v, and sum these minima over all nodes.

Hard9TreeDivide and conquerDynamic programmingShortest pathNo attempts yetTime limit5sMemory limit512 MB

Problem

Here is yet another tree problem. You are given a tree in which every node has a penalty and every edge has a weight. The cost of a simple path between two nodes is the sum of the weights of the edges on that path plus the product of the penalties of the two endpoints. For nodes uu and vv the cost is dist(u,v)+pu×pv\mathrm{dist}(u, v) + p_u \times p_v, where dist(u,v)\mathrm{dist}(u, v) is the sum of the edge weights on the path and pup_u is the penalty of node uu.

A path may contain no edges at all. The cost of such a path is the square of that node's penalty, that is pu2p_u^2.

For each node, find the smallest cost among the paths that start at that node. The final answer is the sum of those minimum costs over all nodes.

Input

The input consists of a single test case.

The first line contains one integer nn, the number of nodes. (1n200,0001 \le n \le 200{,}000)

The second line contains nn space separated integers pp, the penalty of each node in order of node number. (1p1,000,0001 \le p \le 1{,}000{,}000)

Each of the next n1n - 1 lines contains three space separated integers ii, jj and ww, describing an edge between node ii and node jj with weight ww. (1in1 \le i \le n, 1jn1 \le j \le n, iji \ne j, 1w1,000,0001 \le w \le 1{,}000{,}000)

Output

Print a single integer, the sum of the lowest path cost of every node.