Problem title
Difficulty
Avg time to solve

Find power of a number
Easy
--
Merge K Sorted Arrays
Moderate
15 mins
Topological Sort
Moderate
30 mins
Next Smaller Element
Moderate
25 mins
Cycle Detection in a Singly Linked List
Moderate
15 mins
Valid String
Moderate
18 mins
Max Game
Easy
25 mins
Matrix Median
Moderate
15 mins
Redundant Brackets
Easy
15 mins
Tree Traversals
Easy
15 mins
29

Max Game

Difficulty: EASY
Contributed By
Avg. time to solve
25 min
Success Rate
70%

Problem Statement

You are given 'N' numbers and you have to play a game using them. In one move you have to pick any two numbers 'A' and 'B' and replace them by their sum 'A+B'. Doing this gives you a penalty of 'A+B'. Note that the count of elements reduces by 1 every time you take 2 numbers and replace them by their sum. The game ends when there is only one element left. Your task is to minimise the penalty during the game.

You need to return the minimum possible penalty you can have when the game ends.

Input format:
The first line of input contains an integer 'T' denoting the number of queries or test cases. 

The first line of every test case contains an integer 'N' denoting the count of numbers.

The second line of every test case contains 'N' single space-separated integers representing the numbers given to you initially. 
Output format:
For each test case, return the minimum possible penalty in a separate line. 
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 10^5 
1 <= X[i] <= 10^6 

Time limit: 1 second
Sample input 1:
1
4  
2 3 1 4 
Sample output 1:
19
Explanation
To achieve the minimum penalty,
First, we pick 1 and 2, replace it by 3. We get { 3, 3, 4}
Second, we pick 3 and 3, replace it by 6. We get {4, 6}
Third, we pick 4 and 6, replace it by 10. We get {10}

The minimum penalty will be 3 + 6 + 10 = 19.   
Sample input 2:
1
5
7 6 5 5 3
Sample output 2:
60
Reset Code
Full screen
copy-code
Console