TopCoder SRM 601, task 500