Bugfix of musashibg's solution

Hello, the competition deadline is long past, but nevertheless I have fixed a couple of bugs in my implementation.
One of them was caused by an incorrect assumption about the algorithm (article 26); the other was due to me not identifying the special case presented in Test 7.
I'm submitting a pull request to ensure that the final solution will be correct for future reference.