Something very much wrong in this code

TLE’s, wrong answers and runtime errors. need a hint