<p>You are given an array <code>heights</code> of <code>n</code> integers representing the number of bricks in <code>n</code> consecutive towers. Your task is to remove some bricks to form a <strong>mountain-shaped</strong> tower arrangement. In this arrangement, the tower heights are non-decreasing, reaching a maximum peak value with one or multiple consecutive towers and then non-increasing.</p>

<p>Return the <strong>maximum possible sum</strong> of heights of a mountain-shaped tower arrangement.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">heights = [5,3,4,1,1]</span></p>

<p><strong>Output:</strong> <span class="example-io">13</span></p>

<p><strong>Explanation:</strong></p>

<p>We remove some bricks to make <code>heights =&nbsp;[5,3,3,1,1]</code>, the peak is at index 0.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">heights = [6,5,3,9,2,7]</span></p>

<p><strong>Output:</strong> <span class="example-io">22</span></p>

<p><strong>Explanation:</strong></p>

<p>We remove some bricks to make <code>heights =&nbsp;[3,3,3,9,2,2]</code>, the peak is at index 3.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">heights = [3,2,5,5,2,3]</span></p>

<p><strong>Output:</strong> <span class="example-io">18</span></p>

<p><strong>Explanation:</strong></p>

<p>We remove some bricks to make <code>heights = [2,2,5,5,2,2]</code>, the peak is at index 2 or 3.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n == heights.length &lt;= 10<sup>3</sup></code></li>
	<li><code>1 &lt;= heights[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def maximumSumOfHeights(self, heights: List[int]) -> int:
        #brute force: select each height as a candidate. for each candidate:
            # find the number that need to be removed to get it to mountain shape
        # find the smallest number that could be removed to make one of the candidates the middle
        # answer = sum(heights) - removal number

        heightsSum = sum(heights)
        
        leftRemovals = [0 for _ in heights]
        for i in range(len(heights)):
            toRemove = 0
            
            #stack = [heights[i]]
            lastHeight = heights[i]

            for j in range(i - 1, -1, -1):
                if heights[j] <= lastHeight:
                    toRemove += leftRemovals[j]
                    break
                else:
                    toRemove += heights[j] - lastHeight
            
            leftRemovals[i] = toRemove
        
        rightRemovals = [0 for _ in heights]
        for i in range(len(heights) - 1, -1, -1):
            toRemove = 0

            stack = [heights[i]]

            for j in range(i + 1, len(heights)):
                if heights[j] <= stack[-1]:
                    toRemove += rightRemovals[j]
                    break
                else:
                    toRemove += heights[j] - stack[-1]
                    stack.append(stack[-1])
            
            rightRemovals[i] = toRemove
        
        minRemovals = min([leftRemovals[i] + rightRemovals[i] for i in range(len(heights))])

        # idea
            #definition: leftwards peak: element and everything left are non-decreasing
            #definition: rightwards peak: element and everything right are non-increasing
            # array that keeps track of removals in left and

        return heightsSum - minRemovals


392