TLE for Scalar Product Tree

Can someone please help me understand as to why the second code is giving a TLE on two test cases of the last subtask while the first one is passing all test cases? The two codes are very similar except the variable names.

1st Solution: https://www.codechef.com/viewsolution/39656432
2nd Solution: https://www.codechef.com/viewsolution/39668053

Thanks in advance.