Code giving TLE

Code - https://ide.codingblocks.com/s/412807

Can’t figure out where could I optimize this code.

Can you check if the code is correct and how can I optimize it?