SRM 159 DIV 2
passed all test cases
might redo it again.