Given n non-negative numbers a_1, a_2, ..., a_n where each represents a point at coordinate (i, a_i), n vertical lines, representing trees height, are drawn such that the two endpoints of line i is at (i, a_i) and (i, 0).
The trees must be cutted in a way that the top of all of them are in a straigh line (not necessarily parallel to the ground). The first or the last tree can be removed, and in that case the root of the removed tree must align with the top of the other trees.
Whats the maximum sum of tree's height that can be achieved after the cut?
Example:
Input: 1.00 2.00 4.00 6.00
Output: 12.00
My best solution runs with time complexity O(N^2), where I assume that the best cut must include the top of two trees, and so for each top of tree I try to find the other one that maximizes the sum.
Any thoughts on how to solve this problem with time complexity lower than O(N^2)?