TopCoder SRM 144 Div 1, 300 pt problem