G - Ghost in the SecuriTree

Languages: C, C++, Java, Python, Kotlin
Time & Memory limits: (details)

Input

Output

Sample test(s)

Input
5 4 2 2 3 4 1 4 5 2 4 1 1 2 1 5 2 1 3
Output
1 2 4 4
Input
9 6 5 5 1 4 1 6 8 9 3 5 6 2 9 7 4 9 6 2 2 8 3 3 4 7 1 2 1 4 2
Output
5 8 9 1 2 4

Hints