I tried making a nested dictionary storing all pairs. But got stuck after trying to recursively iterate through the dictionary. Is it to be done by DP somehow ?
Please guide me towards any simillar problems or the theory needed to solve this. I am familiar with only memoization in DP.