please, someone help me in this problem I am getting a time error
and also please tell me the name of concepts required to learn in c++ for this qn.

Hello @jiten0004, Welcome to Codechef Community. I already answered this question in previous post. Here’s the link .

Probably C++ STL, specifically map.
Array may also satisfy your requirement.

thanks so much i will learn this topic and then try it .
btw very nice explanation for dp.