#K82007. Calculate Function Loads in a Module

    ID: 35879 Type: Default 1000ms 256MiB

Calculate Function Loads in a Module

Calculate Function Loads in a Module

Given a module with ( n ) functions organized in a tree structure (with function 1 as the root), each function has an associated complexity. The load of a function is defined as the sum of its own complexity and the complexities of all functions in its subtree (i.e. all functions that are called directly or indirectly).

You are given an integer ( n ), a list of ( n ) integers representing the complexities of each function, and ( n-1 ) pairs of integers representing the edges of the tree (call relationships). Compute and output the load for each function in order from 1 to ( n ).

inputFormat

The input is provided via standard input (stdin) with the following format:

(n): An integer representing the number of functions.
Next line: (n) space-separated integers representing the complexity of each function.
Next (n-1) lines: Each contains two integers ( u ) and ( v ) representing an edge between function ( u ) and function ( v ).
Note: If ( n = 1 ), there are no edge lines.

outputFormat

Print a single line to standard output (stdout) containing ( n ) integers. The ( i^{th} ) integer represents the load of function ( i ). The outputs should be separated by a single space.## sample

5
3 2 1 4 5
1 2
1 3
2 4
4 5
15 11 1 9 5