Given a tree T of **N** nodes rooted at **1**, where each node **i** has **B _{i}** coins attached with it. You have to choose a subset of nodes such that no two adjacent nodes(i.e. nodes connected directly by an edge) are chosen and sum of coins attached with nodes in chosen subset is maximum.

**Input Format:**

```
First argument of input contains an integer aray A denoting parent of each node.
Second argument of input contains an integer array B denoting coins at each node.
```

**Output Format:**

```
return a single integer denoting maximum coins possible.
```

**Constraints:**

```
1 <= N <= 50000
-40000 <= B[i] <= 40000
A[0] = 0
```

**For Example:**

```
Input 1:
A = [0, 1, 1] B = [10, 2, 3]
Output 1:
10
Explanation 1:
we choose node 10
10
/ \
2 3
Input 2:
A = [0, 1, 1] B = [5, 3, 4]
Output 2:
7
```

NOTE:You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.

Sign Up

to access hints and editorial solutions for**Maximum coin in a tree**

to access hints and editorial solutions for

Loading...