Comments

Actually I was not using fast i/o. After I added os_base::sync_with_stdio(false); cin.tie(NULL); it got accepted.

Can you try using fast i/o? I was also getting TLE. Try adding this

ios_base::sync_with_stdio(false); cin.tie(NULL);

Getting Time Limit Exceeded for D. Can someone help find the mistake? https://codeforces.me/contest/1709/submission/165440694