DWITE 2011-01 P3: Fixed algorithm, even though it passed the test cases by luck.
DWITE 2010-12 P4: Tweaked code for slight simplification.