Problem: Takahashi is hosting an sports meet. There are N people who will participate. These people are conveniently numbered 1 through N. Also, there are M options of sports for this event. These sports are numbered 1 through M. Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.  Takahashi knows that Person i's j-th favorite sport is Sport A_{ij}. Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.  Takahashi is worried that one of the sports will attract too many people. Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized. Find the minimum possible number of the participants in the sport with the largest number of participants.

Submission attempts (in order):
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


N, M = map(int, input().split())
mat = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N)]

ans = N
Q = [set([v for v in range(M)])]
while Q:
    C = Counter()
    sports_cand = Q.pop(0)
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                C[mat[n][m]] += 1
                break
    tmp = 0
    rem_key = {}
    for key, val in C.most_common():
        if val >= tmp:
            tmp = val
            ans = min(ans, val)
            rem_key.add(key)
        else:
            break
    if len(sports_cand) > 2:
        Q.append(sports_cand-rem_key)
print(ans)


from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


N, M = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(N)]

ans = float('inf')
sports_cand = set([v for v in range(M)])


def rec(sports_cand, ans):
    if not sports_cand:
        return ans
    else:
        attend_i = [0]*N  
        attend_sports = [-1]*N  
        for s in sports_cand:
            for p in range(N):
                if attend_i[p] < mat[p][s]:  
                    attend_i[p] = mat[p][s]
                    attend_sports[p] = s
        C = Counter(attend_sports)
        print("c->", C)
        num = 0
        for id, tmp_val in C.most_common():
            if tmp_val >= num:
                num = tmp_val
                ans = min(ans, tmp_val)
                if len(sports_cand) <= 2:
                    break
                ans = min(ans, rec(sports_cand-{id}, ans))
            else:
                break
    return ans


print(rec(sports_cand, ans))


from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


N, M = map(int, input().split())
mat = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N)]

ans = N
Q = [set([v for v in range(M)])]
while Q:
    C = Counter()
    sports_cand = Q.pop(0)
    for n in range(N):
        for m in range(M):
            if mat[n][m] in sports_cand:
                C[mat[n][m]] += 1
                break
    tmp = 0
    rem_key = set()
    for key, val in C.most_common():
        if val >= tmp:
            tmp = val
            ans = min(ans, val)
            rem_key.add(key)
        else:
            break
    if len(sports_cand) > 2:
        Q.append(sports_cand-rem_key)
print(ans)


Verdicts: Runtime Error

Wrong Answer

Accepted