problem 1048 (time limit exceeded)