Wizards and Costly Wands

Can anyone help me to solve below problem .
CodeChef: Practical coding for everyone