While I was solving this question during virtual contest, this submission gave me Memory Limit Exceeded verdict, when I changed the code slightly (I just made "vector" ans a global vector) it was accepted, here is the accepted version. The point to observe here is that "vector ans" is of size 193 only, which is very small to cross the given memory limits. I couldn't figure out what is wrong with my code. Any help is appreciated.