There is an array [-1,0,0,0,0,1,1,1,1,-1,-1,1,-1] .
With the help of such a cycle, each next element becomes the sum of the previous ones:
for i in range(BESTRESULT.shape[0] - 1): BESTRESULT[i + 1] = BESTRESULT[i + 1] + BESTRESULT[i] The total code execution time due to this cycle is exactly doubled. There is an urgent need to replace this code with something faster. Preferably using NumPy .