Heap
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
Special Judge, 64bit IO Format: %lld

题目描述

You are given a tree with n nodes rooted at node 1, and the i-th node has a value a_i. You need to assign a new value b_i to each node, such that b_{p_i} \leq b_i for each node i and its parent p_i. Output the minimum value of \sum_{i=1}^n (a_i-b_i)^2.

输入描述:

The first line contains an integer n\ (1\leq n\leq 2\cdot 10^5) indicating the number of nodes in the tree.
The second line contains n integers, and the i-th integer a_i\ (0 \leq a_i \leq 10^9) indicates the original value of the i-th node.
Each of the next (n-1) lines contains two integers u and v, indicating an edge between u and v.

输出描述:

Output a real number indicating the minimum value of \sum_{i=1}^n (a_i-b_i)^2. Your answer will be considered correct if and only if the absolute or relative error of your answer to the correct answer is less than or equal to 10^{-6}.
示例1

输入

复制
3
1 0 2
1 2
1 3

输出

复制
0.500000000
示例2

输入

复制
5
9 5 2 7 3
1 2
1 3
2 4
2 5

输出

复制
28.750000000
示例3

输入

复制
6
1 2 3 4 5 6
1 2
2 3
3 6
1 4
1 5

输出

复制
0.000000000