TopCoder SRM 146 Div 1, 300 pt problem