You are given an array A of size (Nâ‰¥2).

We define

ï¿½

(

ï¿½

# )

âˆ‘

# ï¿½

1

ï¿½

âˆ’

1

(

ï¿½

ï¿½

+

ï¿½

ï¿½

+

1

)

f(A)=âˆ‘

i=1

Nâˆ’1

â€‹

(A

i

â€‹

+A

i+1

â€‹

).

Find the maximum value of

ï¿½

(

ï¿½

)

f(A) you can obtain by rearranging the elements of

ï¿½

A in any arbitrary order.

Input Format

The first line of input will contain a single integer

ï¿½

T, denoting the number of test cases.

Each test case consists of multiple lines of input.

The first line of each test case contains one integer,

ï¿½

N, the size of the array.

The next line contains

ï¿½

N space-separated integers,

ï¿½

1

,

ï¿½

2

,

â€¦

,

ï¿½

ï¿½

A

1

â€‹

,A

2

â€‹

,â€¦,A

N

â€‹

.

Output Format

For each test case, output on a new line, the maximum value of

ï¿½

(

ï¿½

)

f(A) you can obtain by rearranging the elements of

ï¿½

A in any arbitrary order.

Constraints

1

â‰¤

ï¿½

â‰¤

2

â‹…

1

0

4

1â‰¤Tâ‰¤2â‹…10

4

2

â‰¤

ï¿½

â‰¤

1

0

5

2â‰¤Nâ‰¤10

5

1

â‰¤

ï¿½

ï¿½

â‰¤

1

0

4

1â‰¤A

i

â€‹

â‰¤10

4

The sum of

ï¿½

N over all test cases does not exceed

1

0

5

10

5

.

Sample 1:

Input

Output

2

2

3 6

5

2 2 1 2 2

9

15

Explanation:

The first sample has

2

2 valid arrays :

[

3

,

6

]

[3,6] and

[

6

,

3

]

[6,3], both have

ï¿½

(

ï¿½

# )

9

f(A)=9.

For the second sample, one optimal array is

[

1

,

2

,

2

,

2

,

2

]

[1,2,2,2,2], which has

ï¿½

(

ï¿½

# )

f(A)=

(

1

+

2

)

+

(

2

+

2

)

+

(

2

+

2

)

+

(

2

+

2

# )

15

(1+2)+(2+2)+(2+2)+(2+2)=15.

```
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort(arr.begin(), arr.end(), greater<int>());
long long sum = 0;
for (int i = 0; i < n - 1; i++) {
sum += arr[i] + arr[i + 1];
}
cout << sum << endl;
}
return 0;
}
```

## Leave a Reply