Slight optimization to problem 14