A genealogist has recorded a family's ancestry as a binary tree rooted at person
For any level (generation) of the tree, its width is defined as the distance between the leftmost and rightmost recorded person in that generation, counting the gaps where a person's sibling position could have existed but no person was actually recorded there (as if the tree were laid out as a complete binary tree, with every position indexed accordingly, whether or not a person actually occupies it).
Find the maximum width across all generations of the family tree.
The first line contains a single integer KaTeX can only parse string typed expression — the number of people recorded. If KaTeX can only parse string typed expression, the tree is empty.
The second line contains KaTeX can only parse string typed expression integers, the value associated with each person (person KaTeX can only parse string typed expression to person KaTeX can only parse string typed expression).
Each of the next KaTeX can only parse string typed expression lines contains two integers, the left and right child of that person (using KaTeX can only parse string typed expression if a child was not recorded). The tree is rooted at person KaTeX can only parse string typed expression.
Print a single integer — the maximum generation width.
Input
6
1 3 2 5 3 9
2 3
4 5
-1 6
-1 -1
-1 -1
-1 -1Output4ExplanationThe bottom generation contains person 4 and person 6. Laid out as a complete binary tree, person 4 sits at position 0 and person 6 sits at position 3 of that generation (accounting for the missing sibling positions in between), giving a width of KaTeX can only parse string typed expression.
Example 2:
Input
1
42
-1 -1Output1ExplanationA single person, with no children, has a generation width of 1.
Example 3:
Input
0Output0ExplanationAn empty family tree has no generations at all, so the width is 0.