Given an array A of N numbers, and an initially empty array P, you can do operations of 4 types on it:
Note that after each operation, array A becomes smaller and maybe reversed. You need to continue adding elements to array P until the array A has 0 elements. You need to maximise the sum of all the values in array P.
Input:
First line contains a single integer N. Next line contains N space separated integers denoting the array A.
Output:
Output the maximum sum of all elements of array P that can be obtained by doing the 4 operations as described above on the array A.
Constraints:
1≤N≤103
1≤Ai≤106
We can get 24 by doing the operations in this order: [2,3,2].
After first operation (2nd) : A=[1,4,2],P=[15]
After second operation (3rd) : A=[2,4],P=[15,1]
After third operation (2nd) : A=[],P=[15,1,8]
Total sum at the end is 15+1+8=24.