Wizards and Costly Wands

Can anyone help me to solve below problem .
https://www.codechef.com/DCOD2017/problems/WIZCW