Please help me out. I'm getting "Memory Limit exceeded in test 3" in question D of codeforces https://codeforces.me/problemset/problem/1675/D. I can't figure out where I'm doing wrong. Kindly help me out.
I'm attaching my code here. https://codeforces.me/contest/1675/submission/156122315.