<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:
        '''
        6,5,3,9,2,7

        [6] 5   3   3  2  2 = 21
         5 [5]  3   3  2  2 = 20
         3  3  [3]  3  2  2 = 16
         3  3   3  [9] 2  2 = 22    <-  
         2  2.  2   2  2  2 = 12
         2  2   2   2  2  7 = 17  
        
        6. 10. 9.  18. 10. 17 <- prefix_sum_left
        21.15. 10.  13. 4. 7. <- prefix_sum_right
        21 20. 16   22. 12 17 -> 21
        prefix_sum_left[i] + prefix_sum_right[i] - heights[i]


        0.  1. 2.  3.   4  5
        6.  5. 3.  9.   2. 7
        6. 10. 9.  18. 10. 17

        6  10  9   18. 10 17
        
               i.      st[-1]
        0.  1. 2.  3.   4  5
        6.  5. 3.  9.   2. 7
        21  15   10  13    4  7
        [4,2]

        0,1,2,3,4,5 
        6,5,3,9,2,7

        1,2,4,4,6,6

        -1,-1,-1,2,-1,4   
        
        '''
        n = len(heights)
        prefix_sum_left = self.cal_prefix_sum_left(heights, n)
        # print(prefix_sum_left)
        prefix_sum_right = self.cal_prefix_sum_right(heights,n)
        # print(prefix_sum_right)
        ans = -1
        for i in range(n):
            ans = max(ans,prefix_sum_left[i] + prefix_sum_right[i] - heights[i])

        return ans

    def cal_prefix_sum_left(self, heights, n):
        arr = [0] * n
        st = []
        for i in range(n):
            h = heights[i]
            while st and heights[st[-1]] >= h:
                st.pop()
            if not st:
                arr[i] = h * (i+1)
            else:
                arr[i] = arr[st[-1]] + h * (i - st[-1])
            st.append(i)
        return arr
    
    def cal_prefix_sum_right(self, heights, n):
        arr = [0] * n
        st = []
        for i in range(n-1,-1,-1):
            h = heights[i]
            while st and heights[st[-1]] >= h:
                st.pop()

            if not st:
                arr[i] = h * (n-i)
            else:
                arr[i] = arr[st[-1]] + h * (st[-1] - i)
            st.append(i)
        return arr




73