2014 lge code jam contest 1 : problem 2, 4