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:
2nd Solution:

Thanks in advance.