https://codeforces.com/contest/1401/problem/D

I am trying to solve this problem but i am getting wa in test no 5 please someone can help me through this… can someone please spot out where i am making mistake …

i have provided proper explanation in my code …link is below

Thanks in Advane