Build n towers from 1 to n.
Height of the i-th tower, a_i, cannot be greater than m_i meters. (1 <= a_i <= m_i)
There cannot be integers j and k such that there exist j < i < k and a_j > a_i and a_i < a_k.
Given a list of M, return a list A so that the sum of all a_i is the maximum possible one.
(Thus, the sequence of building heights can have at most a single peak, and no valley)
Example 1:
Input: [1, 2, 3, 2, 1]
Output: [1, 2, 3, 2, 1]
Explanation: Sum=9 with 3 as the peakExample 2:
Input: [10, 6, 8]
Output: [10, 6, 6]
Explanation: Sum=22 with 10 as the peak, other valid sequences with a lower sum can be [6, 6, 6] or [6, 6, 8]Example 3:
Input: [7, 7, 1, 9, 1, 7, 7]
Output: [7, 7, 1, 1, 1, 1, 1] or [1, 1, 1, 1, 1, 7, 7]
Explanation: Sum=19 with 7 as the peak, [1, 1, 1, 9, 1, 1, 1] is also valid but has sum of 15I couldn't find a solution better than O(n^2). Thoughts?