#K94792. Taco Tree Equalization

    ID: 38720 Type: Default 1000ms 256MiB

Taco Tree Equalization

Taco Tree Equalization

Given a tree with n nodes, each having an integer value, you are allowed to perform the following operation: in one day, you can choose any connected subgraph (i.e. any set of nodes that form a connected component in the tree) and increment the value of every node in that subgraph by 1.

The goal is to make all node values equal in the minimum number of days. Since the tree is connected, one optimal strategy is to increment all nodes until they all reach the maximum initial value. Hence, the minimum number of days required is given by $$\text{days} = \max(v) - \min(v),$$ where \( v \) is the list of initial values.

Your task is to implement a program that reads the tree information from the standard input and outputs the minimum number of days required to equalize all node values to standard output.

inputFormat

The input is given via stdin and consists of:

  1. A single integer n representing the number of nodes in the tree.
  2. A line with n space-separated integers, where the i-th integer denotes the initial value of the i-th node.
  3. n - 1 lines each containing two space-separated integers u and v indicating there is an edge between nodes u and v.

outputFormat

The output should be a single integer printed to stdout representing the minimum number of days required to equalize the node values.

## sample
4
1 2 3 4
1 2
1 3
1 4
3