Weak test case for the problem


#1

Initially i done with some kind of BFS. I got correct result. But after comparing with solution i debug my code. I find out it does not work with below test case.
14 1 2 2 4 -1 3 4 3 -1 -1 -1 -1 -1 -1