Prerequisites: Constructive Algorithm, Implementation
At first, as you cannot make or the highest pillar after the arrangment, it's a nice observation that you don't need to increase the height of of in any time. So, needs to be to satisfy the property. Similarly, needs to be . So, you can calculate as the cost needed to make strictly increasing and same goes for to make strictly decreasing.
So, we check for each index to be the highest pillar after the arrangement if possible. The cost for an index to satisfy the properties is if , and . You just check all the possibilities and take the minimum.
Time Complexity: O(n)