problem 1019 (time limit exceeded)