LuoTianyi and the Table (Codeforces 1825B)
Tags: Math, greedy

LuoTianyi gave an array $$$b$$$ of $$$n \cdot m$$$ integers. She asks you to construct a table $$$a$$$ of size $$$n \times m$$$, filled with these $$$n \cdot m$$$ numbers, and each element of the array must be used exactly once. Also she asked you to maximize the following value: $$$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\left(\max\limits_{1 \le x \le i, 1 \le y \le j}a_{x,y}-\min\limits_{1 \le x \le i, 1 \le y \le j}a_{x,y}\right)$$$ This means that we consider $$$n \cdot m$$$ subtables with the upper left corner in $$$(1,1)$$$ and the bottom right corner in $$$(i, j)$$$ ($$$1 \le i \le n$$$, $$$1 \le j \le m$$$), for each such subtable calculate the difference of the maximum and minimum elements in it, then sum up all these differences. You should maximize the resulting sum.Help her find the maximal possible value, you don't need to reconstruct the table itself.

Input: Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 200$$$) — the number of test cases. The description of test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 100$$$) — the number of rows and columns of the table.The second line of each test case contains $$$n \cdot m$$$ integers $$$b_1, b_2, \ldots, b_{n\cdot m}$$$ ($$$-10^5 \le b_{i} \le 10^5$$$) — the numbers you can put in the table.Note, that integers in the array $$$b$$$ can be negative.It is guaranteed that the sum of $$$n \cdot m$$$ over all test cases doesn't exceed $$$2\cdot 10^5$$$.

Output: For each test case, output a single integer — the maximal value, that can be obtained.

Note: In the first test case, the table is follows:4113 In the subtable with the bottom right corner in $$$(1, 1)$$$, the difference of the maximal and minimal elements is $$$4 - 4 = 0$$$.In the subtable with the bottom right corner in $$$(1, 2)$$$, the difference of the maximal and minimal elements is $$$4 - 1 = 3$$$.In the subtable with the bottom right corner in $$$(2, 1)$$$, the difference of the maximal and minimal elements is $$$4 - 1 = 3$$$.In the subtable with the bottom right corner in $$$(2, 2)$$$, the difference of the maximal and minimal elements is $$$4 - 1 = 3$$$.Then the maximum possible value is $$$0+3+3+3=9$$$.In the second test case, all elements are equal, so all differences are $$$0$$$, and the answer is $$$0$$$.

Sample Inputs:
5
2 2
1 3 1 4
2 2
-1 -1 -1 -1
2 3
7 8 9 -3 10 8
3 2
4 8 -3 0 -7 1
4 3
-32030 59554 16854 -85927 68060 -64460 -79547 90932 85063 82703 -12001 38762

Sample Outputs:
9
0
64
71
1933711

Solution (Python):
import heapq

def find_extremes(lst):
    largest = heapq.nlargest(2, lst)
    smallest = heapq.nsmallest(2, lst)
    return (largest, smallest)
test_num = int(input())
for i in range(test_num):
    input_parts = input().split()
    n = int(input_parts[0])
    m = int(input_parts[1])
    input_parts = input().split()
    arr = [int(i) for i in input_parts]
    ([max1, max2], [min1, min2]) = find_extremes(arr)
    big_nm = max(n, m)
    small_nm = min(n, m)
    use = max(max1 - min2, max2 - min1)
    result = (small_nm - 1) * use + small_nm * (big_nm - 1) * (max1 - min1)
    print(result)

Solution (Python):
I = lambda : map(int, input().split())
for _ in range(int(input())):
    (n, m) = I()
    b = list(I())
    b.sort()
    print(max(b[-2] - b[0], b[-1] - b[1]) * (min(n, m) - 1) + (b[-1] - b[0]) * (n * m - min(n, m)))

Solution (Python):
def read1():
    return int(input())

def read2():
    return list(map(int, input().split()))

def solve(n, m, nums):
    if n < m:
        (n, m) = (m, n)
    max_ = sec_max = float('-inf')
    min_ = sec_min = float('inf')
    for x in nums:
        if x > max_:
            sec_max = max_
            max_ = x
        elif x > sec_max:
            sec_max = x
        if x < min_:
            sec_min = min_
            min_ = x
        elif x < sec_min:
            sec_min = x
    diff1 = max_ - min_
    diff2 = max_ - sec_min
    diff3 = sec_max - min_
    if diff2 > diff3:
        return diff1 * (n - 1) * m + diff2 * (m - 1)
    return diff1 * (n - 1) * m + diff3 * (m - 1)
for _ in range(read1()):
    (n, m) = read2()
    nums = read2()
    print(solve(n, m, nums))