Can you please tell why my code is giving TLE


please give the corrected code as i think this is the best solution available

your solution approach is correct, actually the test case have n=10000, , so a little bit of optimization will work!
see this