Wizards and Huge Prize

CodeForces - 167B