Did problem 909. Works for sample input. Wrong answer on judge.