Array ( [0] => [1] => questions [2] => Basic [3] => Collecting-Candies ) Codevita Previous Questions | Collecting Candies | THE INQUISITIVE





Collecting Candies

LEVEL:Beginner

Description

Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat them later whenever he wants to.
He has recently received N boxes of candies each containing Ci candies where Ci represents the total number of candies in the ith box. Krishna wants to store them in a single box. The only constraint is that he can choose any two boxes and store their joint contents in an empty box only. Assume that there are an infinite number of empty boxes available.
At a time he can pick up any two boxes for transferring and if both the boxes contain X and Y number of candies respectively, then it takes him exactly X+Y seconds of time. As he is too eager to collect all of them he has approached you to tell him the minimum time in which all the candies can be collected.

Input Format

The first line of input is the number of test case T
Each test case is comprised of two inputs
The first input of a test case is the number of boxes N
The second input is N integers delimited by whitespace denoting the number of candies in each box
Constraints:
1 < T < 10
1 < N< 10000
1 < [Candies in each box] < 100009

Output Format

Print minimum time required, in seconds, for each of the test cases. Print each output on a new line.


Example 1:

Input
1
4
1 2 3 4
Output
19
Example 2:

Input
1
5
1 2 3 4 5
Output
34
Example 3:

Input
1
9
4 1 7 6 2 3 9 5 8
Output
164

oops

Login to see Discussion




Approach

Until box count equal to 1, Sort the given array.
After that calculate sum of first and second elements in given array and store in first index.
Now remove second index element from array.
print the total sum value.


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution