#C2285. Maximum Subtree Sum

    ID: 45584 Type: Default 1000ms 256MiB

Maximum Subtree Sum

Maximum Subtree Sum

You are given a tree with ( n ) nodes, where each node is assigned an integer value. The tree is described by an array of parent indices, where the root node is indicated by ( -1 ). For each node ( i ), define the subtree sum ( S_{subtree}(i) ) as: [ S_{subtree}(i) = \sum_{j \in \text{subtree}(i)} v_j ] Your task is to compute the maximum subtree sum among all nodes in the tree.

Input and output are provided via standard input and output respectively.

inputFormat

The first line contains an integer ( n ), the number of nodes in the tree.\nThe second line contains ( n ) space-separated integers representing the parent of each node (with ( -1 ) indicating the root).\nThe third line contains ( n ) space-separated integers representing the value of each node.

outputFormat

Output a single integer which is the maximum subtree sum in the tree.## sample

5
-1 0 0 1 1
1 2 3 4 5
15