How to solve the problem Table Tennis Game 24 in CodeForces 765C?