The mechanizm for saving currently best result without stopping the program is not working when many test cases are given. It only saves the current test case one.
The solution might be to keep test case number and keep best result separate for each test case number.