AlgoUniversity
  • Go Back
Discussion
Unique Element Problem :

Author

Akash

Difficulty Level : Hard

Submissions : 405

Asked In : Walmart

Marks :10

: 6 | : 1

You are given an array of size $$$N$$$. The power of the array represents the sum of the values that you obtain after performing the described operations for N times.

Your task is to delete the array by performing the following operations:

• For any $$$turn_i$$$ where $$$(1 \le i \le N)$$$, delete either of the end (first or last) elements of the remaining array.

• Before deleting that element (let's say at index $$$K$$$), it contributes to the sum of the values as $$$Arr[K]*turn_i + SV_i$$$ the power of the array. Here, $$$turn_i$$$ represents the number of turns in which you are performing the operation.

• $$$SV_i$$$ represents the value of the maximum element that is present in the remaining array before the $$$i^{th}$$$ turn is performed.

You are required to perform this in such a way that you maximize the value of the power of the array.

Input

The input consists of multiple test cases. The first line contains a single integer t $$$(1 \le t \le 10)$$$ — the number of test cases. The description of the test cases follows.

The first line contains the integer $$$N$$$, $$$(1 \le N \le 10^3)$$$ denoting the size of the array.

The second line consists of $$$N$$$ integers, $$$(a_1,a_2,...a_N)$$$ $$$(1 \le a_i \le 10^9)$$$ denoting the elements of the array.

Output

For each test case in a new line, print the value representing the maximum power that can be obtained after the array is completely deleted.

Example

Input
1
5
5 4 3 6 2
Output
96

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.