You are given an array containing n elements, where n is odd. In one operation, you can select an index i, such that 1 ≤ i < n and remove ai and ai+1 from the array and concatenate the remaining array. You have to keep doing the operation until there are only 3 elements remaining in the array, say x, y and z. The objective is to do the operation in such a way that the sum x+y+z of the remaining 3 elements is maximized. Report the maximum sum you can obtain after doing the operations in any order.
I tried brute force but I am looking for a optimal solution