B-7 (slow version, yields "Time limit exceeded (test 5)").