-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathcodeforces.py
61 lines (52 loc) · 1.47 KB
/
codeforces.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return...
The orangutan has gifted you an array 𝑎
of length 𝑛
. Using 𝑎
, you will construct two arrays 𝑏
and 𝑐
, both containing 𝑛
elements, in the following manner:
𝑏𝑖=min(𝑎1,𝑎2,…,𝑎𝑖)
for each 1≤𝑖≤𝑛
.
𝑐𝑖=max(𝑎1,𝑎2,…,𝑎𝑖)
for each 1≤𝑖≤𝑛
.
Define the score of 𝑎
as ∑𝑛𝑖=1𝑐𝑖−𝑏𝑖
(i.e. the sum of 𝑐𝑖−𝑏𝑖
over all 1≤𝑖≤𝑛
). Before you calculate the score, you can shuffle the elements of 𝑎
however you want.
Find the maximum score that you can get if you shuffle the elements of 𝑎
optimally.
Input
The first line contains 𝑡
(1≤𝑡≤100
) — the number of test cases.
The first line of each test case contains an integer 𝑛
(1≤𝑛≤1000
) — the number of elements in 𝑎
.
The following line contains 𝑛
integers 𝑎1,𝑎2,…,𝑎𝑛
(1≤𝑎𝑖≤1000
) — the elements of the array 𝑎
.
It is guaranteed that the sum of 𝑛
over all test cases does not exceed 1000
.
Output
For each test case, output the maximum score that you can get.
solution as follows
'''
test = int(input())
for _ in range(test):
n = int(input())
arr = list(map(int,input().split()))
maxa = max(arr)
mina = min(arr)
diff=maxa-mina
ans = int(diff*(n-1))
print(ans)