Rotate Columns (hard version) (Codeforces 1209E2)
Tags: Greedy, bitmasks

This is a harder version of the 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 12$$$, $$$1 \le m \le 2000$$$) — 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:
3
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
3 3
9 9 9
1 1 1
1 1 1

Sample Outputs:
12
29
27

Solution (Python):
import heapq
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_root(s):
    v = []
    while not s == root[s]:
        v.append(s)
        s = root[s]
    for i in v:
        root[i] = s
    return s

def unite(s, t):
    (s, t) = (get_root(s), get_root(t))
    if s == t:
        return
    if rank[s] < rank[t]:
        (s, t) = (t, s)
    if rank[s] == rank[t]:
        rank[s] += 1
    root[t] = s
    size[s] += size[t]
    return

def same(s, t):
    return True if get_root(s) == get_root(t) else False

def get_size(s):
    return size[get_root(s)]

def f(u, v):
    return u * m + v
t = int(input())
ans = []
pow2 = [1]
for _ in range(12):
    pow2.append(2 * pow2[-1])
inf = pow(10, 9) + 1
g0 = [0]
for i in range(1, 12):
    g0 = g0 + [i] + g0
nx0 = [[]]
s00 = [[]]
for n in range(1, 13):
    l = pow2[n]
    (nx, s0) = ([], [0] * (l + 1))
    for i in range(l):
        x = []
        for j in range(n):
            if not i & pow2[j]:
                x.append(pow2[j])
        k = i
        for j in range(pow2[len(x)] - 1):
            k ^= x[g0[j]]
            nx.append(k)
        s0[i + 1] = len(nx)
    nx0.append(nx)
    s00.append(s0)
for _ in range(t):
    (n, m) = map(int, input().split())
    l = pow2[n]
    root = [i for i in range(l)]
    rank = [1 for _ in range(l)]
    size = [1 for _ in range(l)]
    visit = [0] * l
    for i in range(1, l):
        if visit[i]:
            continue
        j = i
        while not visit[j]:
            visit[j] = 1
            unite(i, j)
            j = j >> 1 ^ (j & 1) << n - 1
    a = [[] for _ in range(m)]
    for _ in range(n):
        a0 = list(map(int, input().split()))
        for i in range(m):
            a[i].append(a0[i])
    h = [-f(max(a[i]), i) for i in range(m)]
    heapq.heapify(h)
    dp = [0] * l
    (nx, s0) = (nx0[n], s00[n])
    for _ in range(min(n, m)):
        i = -heapq.heappop(h) % m
        ai = a[i]
        ma = [0] * l
        (k, s) = (0, 0)
        for j in range(l - 1):
            u = g0[j]
            s += ai[u] if not k & pow2[u] else -ai[u]
            k ^= pow2[u]
            ma[get_root(k)] = max(ma[get_root(k)], s)
        for j in range(1, l):
            ma[j] = ma[get_root(j)]
        for u in range(l - 1, -1, -1):
            for j in range(s0[u], s0[u + 1]):
                v = nx[j]
                dp[v] = max(dp[v], dp[u] + ma[u ^ v])
    ans0 = dp[-1]
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import heapq
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_root(s):
    v = []
    while not s == root[s]:
        v.append(s)
        s = root[s]
    for i in v:
        root[i] = s
    return s

def unite(s, t):
    (s, t) = (get_root(s), get_root(t))
    if s == t:
        return
    if rank[s] < rank[t]:
        (s, t) = (t, s)
    if rank[s] == rank[t]:
        rank[s] += 1
    root[t] = s
    size[s] += size[t]
    return

def same(s, t):
    return True if get_root(s) == get_root(t) else False

def get_size(s):
    return size[get_root(s)]

def f(u, v):
    return u * m + v
t = int(input())
ans = []
pow2 = [1]
for _ in range(12):
    pow2.append(2 * pow2[-1])
inf = pow(10, 9) + 1
g0 = [0]
for i in range(1, 12):
    g0 = g0 + [i] + g0
nx0 = [[]]
s00 = [[]]
for n in range(1, 13):
    l = pow2[n]
    (nx, s0) = ([], [0] * (l + 1))
    for i in range(l):
        x = []
        for j in range(n):
            if not i & pow2[j]:
                x.append(pow2[j])
        k = i
        for j in range(pow2[len(x)] - 1):
            k ^= x[g0[j]]
            nx.append(k)
        s0[i + 1] = len(nx)
    nx0.append(nx)
    s00.append(s0)
for _ in range(t):
    (n, m) = map(int, input().split())
    l = pow2[n]
    root = [i for i in range(l)]
    rank = [1 for _ in range(l)]
    size = [1 for _ in range(l)]
    visit = [0] * l
    for i in range(1, l):
        if visit[i]:
            continue
        j = i
        while not visit[j]:
            visit[j] = 1
            unite(i, j)
            j = j >> 1 ^ (j & 1) << n - 1
    a = [[] for _ in range(m)]
    for _ in range(n):
        a0 = list(map(int, input().split()))
        for i in range(m):
            a[i].append(a0[i])
    h = [-f(max(a[i]), i) for i in range(m)]
    heapq.heapify(h)
    dp = [0] * l
    (nx, s0) = (nx0[n], s00[n])
    for _ in range(min(n, m)):
        i = -heapq.heappop(h) % m
        ai = a[i]
        ma = [0] * l
        (k, s) = (0, 0)
        for j in range(l - 1):
            u = g0[j]
            s += ai[u] if not k & pow2[u] else -ai[u]
            k ^= pow2[u]
            ma[get_root(k)] = max(ma[get_root(k)], s)
        for j in range(1, l):
            ma[j] = ma[get_root(j)]
        for u in range(l - 1, -1, -1):
            for j in range(s0[u], s0[u + 1]):
                v = nx[j]
                dp[v] = max(dp[v], dp[u] + ma[u ^ v])
    ans0 = dp[-1]
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))

Solution (Python):
import heapq
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def get_root(s):
    v = []
    while not s == root[s]:
        v.append(s)
        s = root[s]
    for i in v:
        root[i] = s
    return s

def unite(s, t):
    (s, t) = (get_root(s), get_root(t))
    if s == t:
        return
    if rank[s] < rank[t]:
        (s, t) = (t, s)
    if rank[s] == rank[t]:
        rank[s] += 1
    root[t] = s
    size[s] += size[t]
    return

def same(s, t):
    return True if get_root(s) == get_root(t) else False

def get_size(s):
    return size[get_root(s)]

def f(u, v):
    return u * m + v
t = int(input())
ans = []
pow2 = [1]
for _ in range(12):
    pow2.append(2 * pow2[-1])
inf = pow(10, 9) + 1
g0 = [0]
for i in range(1, 12):
    g0 = g0 + [i] + g0
nx0 = [[]]
s00 = [[]]
for n in range(1, 13):
    l = pow2[n]
    (nx, s0) = ([], [0] * (l + 1))
    for i in range(l):
        x = []
        for j in range(n):
            if not i & pow2[j]:
                x.append(pow2[j])
        k = i
        for j in range(pow2[len(x)] - 1):
            k ^= x[g0[j]]
            nx.append(k)
        s0[i + 1] = len(nx)
    nx0.append(nx)
    s00.append(s0)
for _ in range(t):
    (n, m) = map(int, input().split())
    l = pow2[n]
    root = [i for i in range(l)]
    rank = [1 for _ in range(l)]
    size = [1 for _ in range(l)]
    visit = [0] * l
    for i in range(1, l):
        if visit[i]:
            continue
        j = i
        while not visit[j]:
            visit[j] = 1
            unite(i, j)
            j = j >> 1 ^ (j & 1) << n - 1
    a = [[] for _ in range(m)]
    for _ in range(n):
        a0 = list(map(int, input().split()))
        for i in range(m):
            a[i].append(a0[i])
    h = [-f(max(a[i]), i) for i in range(m)]
    heapq.heapify(h)
    dp = [0] * l
    (nx, s0) = (nx0[n], s00[n])
    for _ in range(min(n, m)):
        i = -heapq.heappop(h) % m
        ai = a[i]
        ma = [0] * l
        (k, s) = (0, 0)
        for j in range(l - 1):
            u = g0[j]
            s += ai[u] if not k & pow2[u] else -ai[u]
            k ^= pow2[u]
            ma[get_root(k)] = max(ma[get_root(k)], s)
        for j in range(1, l):
            ma[j] = ma[get_root(j)]
        for u in range(l - 1, -1, -1):
            for j in range(s0[u], s0[u + 1]):
                v = nx[j]
                dp[v] = max(dp[v], dp[u] + ma[u ^ v])
    ans0 = dp[-1]
    ans.append(ans0)
sys.stdout.write('\n'.join(map(str, ans)))