NQNF Error

Hi guys i am new to code chef, compiler says time exceeded every time i submit but i have checked my code even with larger inputs on other compilers where it works like a charm so i guess i am consuming my input incorrectly

Please help me guys i am stuck with this since yesterday.

Also the sample link https://ibb.co/iVuGce