Codeforces question help

Hello guys , i’m a beginner in competitive . i want your help in solving this question on codeforces . my code is not able to pass the 7th and 8th testcase .

Thanks in advance.

https://codeforces.com/contest/1203/problem/D1