works but 'Time Limit Exceeded'