I was solving a problem on codeforces.com, my code\'s time limit exceeded on test 7, how can I optimize this code to reduce the execution time? Is it possible using python?