Can someone help in optimizing the code

Here is the question and Here is the code. I am getting NZEC while submitting the code. No such issue was there while running the code on VS or at codechef ide.
I am new to CP. please help me in figuring out the mistake.

Hint: check you code for K=0

Thanks manish !!