Finish 11512, works on sample test cases.