#MAGIC4. Magic Program IV

Magic Program IV

This is the solution (TLE).