Maximize Array Function

3

4 votes
2D dynamic programming, Mathamatics, Algorithms, Data Structures, Dynamic Programming
Problem

For array A[] of N integers we call a function fA=Ni=1iAi
Given array of N integers and in one move you can select a position and delete the array element. Then all elements to the right are shifted to the left by 1 position. For example, given array is [5,9,15,25,15,9], if you delete A2 then the array will be [5,15,25,15,9] and then if you delete A2 the array will be [5,25,15,9]
Find the maximum value of fA of modified array by performing the above move any number of times (possibly 0).

 Input format

  • The first line contains T denoting the number of test cases. The description of each test case is as follows.
    • The first line contains an integer N denoting the number of array elements.
    • The next line contains N space-separated integers.

Output format

For each test case, print the answer in a new line.

Constraints

1T10

1N103

109Ai109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • For the first testcase we'll delete A2 and the array will be [5,6,2,9]. So fA=15+26+32+49=47
  • For the second testcase we'll delete all array elements.
Editor Image

?