You are implementing binary search. There is a sorted array of n objects and one new object that you want to insert into it. To find the insertion position you compare the new object with objects of the array. A comparison answers either greater, meaning the new object belongs to the right of the compared object, or less, meaning it belongs to the left. In this problem a comparison never answers equal. When the new object is greater than some object of the array, it is also greater than every object to the left of that object, and when it is less than some object, it is also less than every object to the right of that object. So an array of n elements has n+1 possible insertion positions.
Comparisons do not all cost the same. Comparing the new object with the i-th object of the array costs ai, an integer between 1 and 9 inclusive.
Assume you follow a strategy that minimizes the total cost in the worst case. Find that worst case total cost.