<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:     
        if len(heights) == 1:
            return heights[0]


        def try_top(heights: List[int], top: int) -> int:
            result = 0
            v = heights[top]
            result += heights[top]
            i = top + 1
            while i < len(heights):
                if heights[i] > v:
                    result += v
                else:
                    result += heights[i]
                    v = heights[i]
                i += 1
            if top == 0:
                return result
            v = heights[top]
            i = top - 1
            while i >= 0:
                if heights[i] > v:
                    result += v
                else:
                    result += heights[i]
                    v = heights[i]
                i -= 1
            return result


        m = 0
        for i in range(len(heights)):
            r = try_top(heights, i)
            if r > m:
                m = r
        return m



        
        # packed = list()
        # c = 1
        # mv = heights[0]
        # for i in range(1, len(heights)):
        #     if heights[i] > mv:
        #         mv = heights[i]
        #     if heights[i-1] != heights[i]:
        #         packed.append(
        #             (heights[i-1], c)
        #         )
        #         c = 1
        #     else:
        #         c += 1
        # packed.append(
        #     (heights[i], c)
        # )
        # print(mv, packed)

        # pmv = 0
        # for i in range(len(packed)):
        #     if packed[i][0] * packed[i][1] > pmv:
        #         pmv = packed[i][0] * packed[i][1]

        # # print(pmv)

        # tops = list()
        # for i in range(len(packed)):
        #     if packed[i][0] * packed[i][1] == pmv:
        #         tops.append(i)
        
        # # print(tops)

        # result = -1
        # for t in tops:
        #     t_result = 0
        #     i = t
        #     v = packed[i][0]
        #     while i < len(packed)-1:
        #         if v < packed[i+1][0]:
        #             t_result += v * packed[i+1][1]
        #         else:
        #             t_result += packed[i+1][0] * packed[i+1][1]
        #             v = packed[i+1][0]
        #         i += 1
        #     i = t
        #     v = packed[i][0]
        #     t_result += packed[i][0] * packed[i][1]
        #     while i > 0:
        #         if packed[i-1][0] > v:
        #             t_result += v * packed[i-1][1]
        #         else:
        #             t_result += packed[i-1][0] * packed[i-1][1]
        #             v = packed[i-1][0]
        #         i -= 1
        #     # print('t_result', t_result)
        #     if t_result > result:
        #         result = t_result
        # return result
        


        # mv, mi = 0, 0
        # for i in range(len(heights)):
        #     if heights[i] > mv:
        #        mv = heights[i]
        #        mi = i
        
        # middle = int(len(heights) / 2)
        # #bestd = len(heights) + 1
        # besti = -1
        # for i in range(len(heights)):
        #     if heights[i] == mv:
        #         if abs(middle - i) < abs(middle - besti):
        #             besti = i

        # print(besti)
        # mi = besti
        # for i in range(mi, len(heights)-1):
        #     if heights[i] < heights[i+1]:
        #         heights[i+1] = heights[i]
        
        # while mi > 0:
        #     if heights[mi-1] > heights[mi]:
        #         heights[mi-1] = heights[mi]
        #     mi -= 1
        
        # print(heights)
        # return sum(heights)
        
817