Beautiful Towers I

You are given an array heights of n integers representing the number of bricks in n consecutive towers. Your task is to remove some bricks to form a mountain-shaped 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.

Return the maximum possible sum of heights of a mountain-shaped tower arrangement.

Example 1
#
#   #
# # #
# # #
# # # # #
5 3 4 1 1
Inputheights = [5,3,4,1,1]
Output13
We remove some bricks to make heights = [5, 3, 3, 1, 1], the peak is at index 0.
Example 2
      #
      #
      #   #
#     #   #
# #   #   #
# #   #   #
# # # #   #
# # # # # #
# # # # # #
6 5 3 9 2 7
Inputheights = [6,5,3,9,2,7]
Output22
We remove some bricks to make heights = [3, 3, 3, 9, 2, 2], the peak is at index 3.

Constraints

  • 1 <= n == heights.length <= 10^3
  • 1 <= heights[i] <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate