Rotate Columns (easy version) (Codeforces 1209E1)
Tags: Greedy, bitmasks

This is an easier version of the next problem. The difference is only in constraints.You are given a rectangular $$$n \times m$$$ matrix $$$a$$$. In one move you can choose any column and cyclically shift elements in this column. You can perform this operation as many times as you want (possibly zero). You can perform this operation to a column multiple times.After you are done with cyclical shifts, you compute for every row the maximal value in it. Suppose that for $$$i$$$-th row it is equal $$$r_i$$$. What is the maximal possible value of $$$r_1+r_2+\ldots+r_n$$$?

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 40$$$), the number of test cases in the input.The first line of each test case contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 4$$$, $$$1 \le m \le 100$$$) — the number of rows and the number of columns in the given matrix $$$a$$$. Each of the following $$$n$$$ lines contains $$$m$$$ integers, the elements of $$$a$$$ ($$$1 \le a_{i, j} \le 10^5$$$).

Output: Print $$$t$$$ integers: answers for all test cases in the order they are given in the input.

Note: In the first test case, you can shift the third column down by one, this way there will be $$$r_1 = 5$$$ and $$$r_2 = 7$$$.In the second case you can don't rotate anything at all, this way there will be $$$r_1 = r_2 = 10$$$ and $$$r_3 = 9$$$.

Sample Inputs:
2
2 3
2 5 7
4 2 4
3 6
4 1 5 2 10 4
8 6 6 4 9 10
5 4 9 5 8 7

Sample Outputs:
12
29

Solution (Python):
t = int(input())

def maxsa(A):
    ans = 0
    for i in range(n):
        cur_maxx = 0
        for j in range(4):
            cur_maxx = max(cur_maxx, A[j][i])
        ans += cur_maxx
    return ans

def fu(A):
    answer = 0
    for j in range(n):
        A[0] = A[0][1:] + A[0][:1]
        for i in range(n):
            A[1] = A[1][1:] + A[1][:1]
            for k in range(n):
                A[2] = A[2][1:] + A[2][:1]
                for l in range(n):
                    A[3] = A[3][1:] + A[3][:1]
                    cur_ans = maxsa(A)
                    answer = max(answer, cur_ans)
    return answer
for j in range(t):
    (n, m) = map(int, input().split())
    A = [0] * n
    inds = [-1, -1, -1, -1]
    maxs = [0, 0, 0, 0]
    for j in range(n):
        A[j] = list(map(int, input().split()))
    for j in range(m):
        cur_maxs = 0
        for i in range(n):
            cur_maxs = max(cur_maxs, A[i][j])
        maxs.append(cur_maxs)
        inds.append(j)
        ind = 4
        while ind != 0 and maxs[ind] > maxs[ind - 1]:
            (inds[ind], inds[ind - 1]) = (inds[ind - 1], inds[ind])
            (maxs[ind], maxs[ind - 1]) = (maxs[ind - 1], maxs[ind])
            ind -= 1
        maxs.pop()
        inds.pop()
    S = [0] * 4
    for j in range(4):
        if inds[j] != -1:
            S[j] = [s[inds[j]] for s in A]
        else:
            S[j] = [0] * n
    print(fu(S))

Solution (Python):
from sys import stdin

def f(lst, num):
    new = lst[num:] + lst[:num]
    return new
t = int(stdin.readline())
for i in range(t):
    (row, col) = tuple((int(x) for x in stdin.readline().split()))
    lst = list(([int(x)] for x in stdin.readline().split()))
    for j in range(row - 1):
        line = tuple((int(x) for x in stdin.readline().split()))
        for k in range(len(line)):
            lst[k].append(line[k])
    lst.sort(key=lambda x: max(x), reverse=True)
    ans = float('-inf')
    for a in range(4):
        for b in range(4):
            for c in range(4):
                for d in range(4):
                    if col >= 1:
                        aa = f(lst[0], a)
                    else:
                        aa = (0,) * row
                    if col >= 2:
                        bb = f(lst[1], b)
                    else:
                        bb = (0,) * row
                    if col >= 3:
                        cc = f(lst[2], c)
                    else:
                        cc = (0,) * row
                    if col >= 4:
                        dd = f(lst[3], d)
                    else:
                        dd = (0,) * row
                    ans = max(ans, sum((max((x[j] for x in (aa, bb, cc, dd))) for j in range(row))))
    print(ans)

Solution (Python):
import random
for _ in range(int(input())):
    (N, M) = map(int, input().split())
    X = [[int(a) for a in input().split()] for _ in range(N)]
    Y = [[X[i][j] for i in range(N)] for j in range(M)]
    ma = 0
    for t in range(99):
        for i in range(M):
            a = random.randrange(N)
            Y[i] = [Y[i][j - a] for j in range(N)]
        ma = max(ma, sum([max([Y[i][j] for i in range(M)]) for j in range(N)]))
    print(ma)