#### Topic: Almost Hanoi towers

The task. Almost Hanoi towers time Limit 1. The game field represents sequence from N columns on K i counters in everyone. For one course from a column with number N i it is possible to take away and move on m counters to an adjacent left and right column. (2 * m <= K i) from extreme left and right columns it is authorized to move counters only to one of adjacent accordingly. Define what least amount of the allowed courses it is necessary to fulfill, that in each column there was an equal amount of counters. It is guaranteed that the TOTALS (K i) / N always an integer number. The input data the First line of an input file contains number N - an amount of columns. Next line it is written down K i - amounts of counters in each of columns. (I belongs [1: N]) (2<=N<=200) (0<=Ki<=2000) the Initial data the Output file should contain one number - the minimum quantity of the allowed courses. Input.txt Output.txt 5 7 5 2 4 8 16 Remark a column 3 we take on 2, a column 2 we take on 2, a column 5 we take 16, a column 4 we take on 13, a column 3 we take on 7, a column 5 we take 12, a column 4 we take on 6. My idea of the decision: It is given: 1) a vector of counters v = {5 2 4 8 16} 2) maxFish = the TOTALS (K i); Algorithm: to "weigh" left left and right right half of vector calculating for each side suma (v [i]-maxFish) / N if left <right then For each column i (from left to right) f (i) =f (i-1) + (an amount of courses for Noi). Otherwise For each column i (from right to left) f (i) =f (i-1) + (an amount of courses for Noi). answ = f (n) the Question: how it is possible to receive the task answer faster?