Program for the contest present here : http://kskills.com/Tests/PublicProblem/1986