Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Maximum Sum

Limits: 3s, 32 MB

Little Jimmy loves to play with numbers. One day he was playing with some number sequence. In his game, he picked an array of integers and computed the sum of all the numbers in that array.

Then after playing for couple of hours, he got bored and wanted more challenge. Then he came up with the following game.

The rules for the game is as follows ->

  1. First he is given an array of N integers(Now the game starts).

  2. In each step, he picked a non-empty contiguous subarray and multiply the sum of elements of the sub-array with the size of the subarray. This product is added to his score. After that he removed the whole subarray from the array. The subarray must be picked sequentially. That is, xth element can’t be picked before picking (x-1)th element.

  3. He continued doing step 2 until the array became empty.

Can you determine the maximum score he can obtain?



-10000 <= Array[i] <=10000


First take a number T as input, which indicates the total number of testcases. Now for each case, take a number N which indicates total number of elements in the array. Now take N numbers as input and compute the maximum score that can be obtained for the new game.


For each case, print the maximum score in a new line.


1 2 3
1 2