Can anyone please have a look at my solution for problem D of AtCoder ABC 216 and tell me where did I go wrong? I have compared my solution to a correct solution by comparing multiple test cases but every one of them gives the same answer on both the solutions. The logic was also a lot similar to the successful submission but I'm not able to spot the error in my code. I have commented the code to explain my logic as well as I can.
Problem link: https://atcoder.jp/contests/abc216/tasks/abc216_d
My solution link: https://atcoder.jp/contests/abc216/submissions/25492717