TLE although solution is short

Hi, I was wondering whether anyone could help me with TLE. The problem link is pasted and my solution is attached(photo)

My solution is only 18 lines while the other successful solutions are 30+. I ran my program through multiple testcases and the answers are all correct. Thank you so much.

if you write shorter program then it’s not mean that your program is fast , and larger program is not fast

Thanks for the reply! Could you give me some advice on how to make that specific code faster?

your program giving tle because your code running time is n*n

for fast input output

Thanks a bunch:)