Problem:


Score : 700 points

Problem Statement

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.

Constraints

1 \leq N \leq 300

1 \leq M \leq 300

A_{i1} , A_{i2} , ... , A_{iM} is a permutation of the integers from 1 to M.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
A_{21} A_{22} ... A_{2M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print the minimum possible number of the participants in the sport with the largest number of participants.

Sample Input 1

4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1

Sample Output 1

2

Assume that Sports 1, 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1, Person 2 in Sport 3, Person 3 in Sport 3 and Person 4 in Sport 4.
Here, the sport with the largest number of participants is Sport 3, with two participants.
There is no way to reduce the number of participants in the sport with the largest number of participants to 1. Therefore, the answer is 2.

Sample Input 2

3 3
2 1 3
2 1 3
2 1 3

Sample Output 2

3

Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected.
Therefore, the answer is 3.



Sample input:
4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1


Sample output:
2

Submission status: Accepted

Solution code:
people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) - 1 for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
for _ in range(sports):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
    answer = min(answer, max(rank))
    use_sports -= {rank.index(max(rank))}
print(answer)

Submission status: Accepted

Solution code:
def dfs(people, sports, use_sports, answer):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
            will_delete = []
    max_p = max(rank)
    exclude = rank.index(max_p)
    if len(use_sports) > 1:
        answer = min(answer, dfs(people, sports, use_sports - {exclude}, answer))
    answer = min(answer, max_p)
    return answer

people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) - 1 for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
print(dfs(people, sports, use_sports, answer))

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
A = [list(map(lambda x:int(x)-1,input().split()))[::-1] for i in range(N)]

L = [0]*M
for p in A:
  L[p[-1]] += 1
best,i = max((l,i) for i,l in enumerate(L))

for r in range(M-1):
  L[i] = -1
  for p in A:
    L[p[-1]] -= 1
    while L[p[-1]] < 0:
      p.pop()
    L[p[-1]] += 1

  b,i = max((l,i) for i,l in enumerate(L))
  best = min(best,b)

print(best)

Submission status: Accepted

Solution code:
N,M=list(map(int,input().strip().split(' ')))
A=[]
for _ in range(N):
    A+=[list(map(int,input().strip().split(' ')))[::-1]]
    
current=[0]*(M+1)
deleted=[0]*(M+1)

for j in range(N):
    current[A[j][-1]]+=1
    
#print(current)    
MAX=max(current)
ANS=MAX
index=-1
for j in range(1,len(current)):
    if current[j]==MAX:
        index=j
good=0       
if MAX==1:
    good=1
    print(1)
if good==0:
    count=0
    while(count<M-1):
        current=[0]*(M+1)
        deleted[index]=1
        for x in range(N):
            while(deleted[A[x][-1]]==1):
                A[x].pop()
                if A[x]==[]:
                    break
        for x in range(N):
            if A[x]!=[]:
                current[A[x][-1]]+=1
        MAX=max(current)
        if MAX<ANS:
            ANS=MAX
        for x in range(M+1):
            if current[x]==MAX:
                index=x
                
        count+=1  
    print(ANS)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A=[]
for n in range(N):
    A.append(list(map(int,input( ).split( ))))

candi=[ ]
for m in range(M):
    tmp =[0 for _ in range(M)]
    for n in range(N):
        tmp[A[n][0]-1] +=1
    max_player=max(tmp)
    candi.append(max_player)
    max_sports=tmp.index(max_player)
    for n in range(N):
         A[n].remove(max_sports+1)
 
print(min(candi))

Submission status: Accepted

Solution code:
from collections import Counter

def read():
    return [int(x) for x in input().split()]

N, M = read()

A = []
for _ in range(N):
    A.append(read())

res = N
for _ in range(M - 1):
    count_dict = Counter([a[0] for a in A])
    sport, count = count_dict.most_common(1)[0]
    res = min(res, count)
    for a in A:
        a.remove(sport)

print(res)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, M = list(map(int, input().split()))
    A = []
    for i in range(N):
        A.append(list(map(int, input().split())))

    ans = float("inf")
    while len(A[0]) != 0:
        count_list = [0 for i in range(M + 1)]
        for i in range(0, N):
            count_list[A[i][0]] += 1

        max_value = max(count_list)
        for i in range(0, N):
            A[i].remove(count_list.index(max_value))

        if ans > max_value:
            ans = max_value

    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N, M=map(int, input().split())
m=[]
ans = 300

for i in range(N):
    m.append(list(map(int,input().split())))

def search(ln):
    #l = []
    cnt = Counter()
    for r in m:
        cnt[r[0]] += 1
    s,c = cnt.most_common(1)[0]
    global ans
    ans = min(ans, c)
    ln.remove(s)
    for r in m:
        r.remove(s)
    if len(ln) == 0:
        return 
    search(ln)
search(list(range(1, M+1)))
print (ans)

Submission status: Accepted

Solution code:
N, M = map(int, raw_input().split(' '))
A = [map(int, raw_input().split(' ')) for _ in xrange(N)]

answer = N
for _ in xrange(M):
    participants = [0] * M
    for n in xrange(N):
        participants[A[n][0]-1] += 1
    sport_with_max_participants = max(xrange(M), key=lambda x: participants[x])
    answer = min(answer, participants[sport_with_max_participants])
    for n in xrange(N):
        A[n].remove(sport_with_max_participants+1)

print answer

Submission status: Accepted

Solution code:
# encoding:utf-8

def main():
	N, M = list(map(int, input().split()))
	prefs = [list(map(int, input().split())) for _ in range(N)]

	game_list = [i for i in range(1, M + 1)]

	tmp = 10 ** 10
	for i in range(M):
		max_game, idx = maxGame(prefs, game_list, M)
		tmp = min(tmp, max_game)
		prefs = removeGame(prefs, idx + 1)
	print(tmp)

def removeGame(prefs, idx):
	for i in range(len(prefs)):
		prefs[i].remove(idx)
	return(prefs)

def maxGame(prefs, game_list, M):
	counter = [0 for _ in range(M)]
	for i in range(len(prefs)):
		counter[prefs[i][0] - 1] += 1
	return([max(counter), counter.index(max(counter))])

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
from collections import Counter
import random

random.seed(1)

# sys.stdin = open('b1.in')


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


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def gen():
    n = 301
    m = 300
    a = [[-1] * m for i in range(n)]
    for i in range(n):
        for j in range(m):
            a[i][j] = 1 + (i + j) % m
    return n, m, a


def gen2(n, m):
    a = [list(range(1, m + 1)) for i in range(n)]
    for i in range(n):
        random.shuffle(a[i])
    return a


def is_possible(l, n, m, a):
    s = set(range(1, m + 1))
    choice = [0] * n
    c = Counter()
    for i in range(n):
        c[a[i][0]] += 1
    while len(s) > 0:
        update = False
        for i in range(n):
            j = choice[i]
            x = a[i][j]
            if x not in s:
                c[x] -= 1
                while a[i][j] not in s:
                    j += 1
                choice[i] = j
                x = a[i][j]
                c[x] += 1
            if c[x] > l:
                s.remove(x)
                update = True
                break
        if not update:
            return True
    return False


def solve(n, m, a):
    left, right = 1, n
    if is_possible(left, n, m, a):
        return left
    while right - left > 1:
        mid = (left + right) // 2
        if is_possible(mid, n, m, a):
            right = mid
        else:
            left = mid
    return right


def solve2(n, m, a):
    pass

    pinf = float("inf")
    best = pinf
    roop = 0
    while True:
        if roop == m:
            break

        firstlist = []
        for r in a:
            tmp = r[0]
            firstlist.append(tmp)
        mode = -1
        count = -1
        for i in range(1, m + 1):
            tmp_c = firstlist.count(i)
            if tmp_c > count:
                mode = i
                count = tmp_c
        if mode == -1:
            break
        elif count <= best:
            best = count
        for r in a:
            r.remove(mode)
        roop += 1
    return best


def check():
    n_steps = 10000
    M = 10
    n_max = 10
    m_max = 10
    for i in range(n_steps):
        n = random.randint(1, n_max)
        m = random.randint(1, m_max)

        a = gen2(n, m)
        aa = [row[:] for row in a]
        r = solve(n, m, a)
        r2 = solve2(n, m, aa)
        if r != r2:
            print('expected:', r2, 'got:', r, )
            print(n, m)
            for row in a:
                print(*row)
            print()
            # return n, m, a
            n_max -= 1
            m_max -= 1


def main():
    # check()
    solve_it()


def solve_it():
    debug = False
    if debug:
        n, m, a = gen()
    else:
        n, m = read_int_list()
        a = [read_int_list() for _ in range(n)]
    res = solve(n, m, a)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m = map(int, raw_input().split())
a = [map(int, raw_input().split())[::-1] for _ in xrange(n)]
ans = n
d = [0] * (m + 1)
for i in xrange(m):
    s = [0] * (m + 1)
    for j in xrange(n):
        while d[a[j][-1]]:
            a[j].pop()
            continue
        s[a[j][-1]] += 1
    l = 1
    for i in xrange(m + 1):
        if s[l] < s[i]:
            l = i
    d[l] = 1
    ans = min(ans, s[l])
print ans

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
A = [[0 for i in range(M)] for j in range(N)]

for i in range(N):
    A[i] = list(map(int, input().split()))

ans = float('inf')
for i in range(M):
    rank = [0] * M
    for j in range(N):
        rank[A[j][0] - 1] += 1
    ans = min(ans, max(rank))
    remove_idx = rank.index(max(rank)) + 1
    for j in range(N):
        A[j].remove(remove_idx)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, m = [int(x) for x in raw_input().split()]
    a = [[int(x) - 1 for x in raw_input().split()] for i in xrange(n)]
    ans = n
    for _ in xrange(m):
        cnt = [0] * m
        for ai in a:
            cnt[ai[0]] += 1
        c = max(xrange(m), key=lambda k: cnt[k])
        ans = min(ans, cnt[c])
        for ai in a:
            ai.remove(c)
    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import numpy as np

N, M = list(map(int, input().split(" ")))

A = []
for i in range(N):
    A.append(list(map(int, input().split(" "))))
    

ans = 9999999999999999

A_np = np.asarray(A)
for i in range(M):
    tmp = np.bincount(A_np[:, 0]).max()
    a = np.bincount(A_np[:, 0]).argmax()
    if tmp < ans:
        ans = tmp
    
    A_flatten = A_np.flatten()
    A_flatten = np.delete(A_flatten, np.where(A_flatten == a)[0])
    A_np = A_flatten.reshape((N, M-1-i))
    
print (ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split())
arys = []
for i in range(N):
    tmp = input()
    ary = [int(a) - 1 for a in tmp.split()]
    arys.append(ary)


s = set([i for i in range(M)])
i_s = [0 for i in range(len(arys))]
min_num = None
while len(s) > 0:
    select_num = [0 for i in range(M)]
    max_v = None
    max_k = None
    for j, ary in enumerate(arys):
        while ary[i_s[j]] not in s:
            i_s[j] += 1
        select_num[ary[i_s[j]]] += 1
        tmp = select_num[ary[i_s[j]]]
        if max_v is None or max_v < tmp:
            max_v = tmp
            max_k = ary[i_s[j]]
    if min_num is None or min_num > max_v:
        min_num = max_v
    s.remove(max_k)
print(min_num)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
a = []

for i in range(N):
    a_i = list(map(int, input().split()))
    a.append(a_i)

max_min_num = N
for _ in range(M):
    selected = {}
    for i in range(N):
        if a[i][0] in selected:
            selected[a[i][0]] += 1
        else:
            selected[a[i][0]] = 1
    max_value = 0
    target_key = 0
    for key, value in selected.items():
        if value > max_value:
            max_value = value
            target_key = key
    if max_min_num > max_value:
        max_min_num = max_value

    for i in range(N):
        a[i].remove(target_key)

print(max_min_num)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
rank = []
ind = [0 for i in range(n)]
for i in range(n):
    rank.append(list(map(int, input().split())))
sports = set(range(1, m + 1))
best = n
while len(sports) > 0:
    kol = [0 for i in range(m + 1)]
    for i in range(n):
        while rank[i][ind[i]] not in sports:
            ind[i] += 1
        kol[rank[i][ind[i]]] += 1
    best = min(best, max(kol))
    new_sports = []
    for s in sports:
        if kol[s] < best:
            new_sports.append(s)
    sports = set(new_sports)
print(best)

Submission status: Accepted

Solution code:
# B(700)
import collections

N, M = map(int, input().split())

A = []
for _ in range(N):
    A.append(list(map(int, input().split())))

res = N
for _ in range(M-1):
    count_dict = collections.Counter([a[0] for a in A])
    sport, count = count_dict.most_common(1)[0]
    res = min(res, count)
    for a in A:
        a.remove(sport)

print(res)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def gcd(a,b):
    while b>0:
        a,b = b,a
        b %= a
    return a

def main():
    n,m = LI()
    a = [LI() for _ in range(n)]
    ai = [0] * n
    d = collections.defaultdict(int)
    for i in range(n):
        d[a[i][0]] += 1
    dr = r = max(d.values())
    s = set(list(range(1,m+1)))
    while s:
        for k,v in d.items():
            if v == dr:
                s.remove(k)
        if not s:
            break
        d = collections.defaultdict(int)
        for i in range(n):
            while a[i][ai[i]] not in s:
                ai[i] += 1
        for i in range(n):
            d[a[i][ai[i]]] += 1
        dr = max(d.values())
        if r > dr:
            r = dr
    return r


print(main())

Submission status: Accepted

Solution code:
N,M=map(int, input().split())
A=[]
for i in range(N):
  A.append(list(map(int,input().split())))

ans=N
# i は外した種目の個数
for i in range(M-1):
  sanka=[0]*M
  for j in range(N):
    sanka[A[j][0]-1]+=1
#  print(sanka)
  saidai=max(enumerate(sanka), key=lambda x: x[1])[0]+1
#  print(saidai)
  ans=min(ans,max(sanka))
  for j in range(N):
    A[j].remove(saidai)

print(ans)

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline

n,m=map(int,input().split())

g=[list(map(lambda x:int(x)-1,input().split())) for _ in range(n)]
picking=[0]*n

# all sports available
hist=[0]*m
sport=[1]*m
for i in range(n):
    hist[g[i][0]]+=1
ans=max(hist)

# keep getting rid of the busiest sport
for _ in range(m-1):
    # pick busiest sport
    b=0
    while not sport[b]: b+=1
    for i in range(b+1,m):
        if hist[b]<hist[i]:
            b=i
    sport[b]=0
    # move people
    for i in range(n):
        while not sport[g[i][picking[i]]]:
            hist[g[i][picking[i]]]-=1
            picking[i]+=1
            hist[g[i][picking[i]]]+=1
    # check answer
    ans=min(ans,max(hist))

print(ans)

Submission status: Runtime Error

Solution code:
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    N,M = LI()
    A = [LI() for _ in range(N)]

    ctr = [0] * N

    sp = [0] * (M+1)
    for i in range(N):
        sp[A[i][ctr[i]]] += 1
    ans = max(sp)

    for _ in range(N-1):
        m = 0
        mi = -1
        for i in range(1,M+1):
            if sp[i] > m:
                m = sp[i]
                mi = i
        sp[mi] = -1
        for i in range(N):
            if A[i][ctr[i]] == mi:
                while sp[A[i][ctr[i]]] < 0:
                    ctr[i] += 1
                sp[A[i][ctr[i]]] += 1
        ans = min(ans,max(sp))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = [list(map(int,input().split())) for i in range(n)]
count = [0]*(m+1)
for i in a:
    count[i[0]] += 1
l = [0]*n
ans = max(count)
s = set()
for i in range(n-1):
    M = max(count)
    ind = count.index(M)
    s.add(ind)

    for j in range(n):
        if a[j][l[j]] == ind:

            count[ind] -= 1
            while a[j][l[j]] in s:
                l[j] += 1
            count[a[j][l[j]]] += 1
    ans = min(ans,max(count))

print(ans)

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, m = map(int, readline().split())
    a = []
    for i in range(n):
        a.append(list(map(int, readline().split())))

    ans = INF
    for _ in range(n - 1):
        cnt = [0] * (m + 1)
        for i in range(n):
            cnt[a[i][0]] += 1
        max_cnt = max(cnt)
        ans = min(ans, max_cnt)
        max_num = cnt.index(max_cnt)
        for i in range(n):
            a[i].remove(max_num)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
    a[i].reverse()

ans = n
used = set()
for i in range(m):
    count = [0]*(m+1)
    for j in range(n):
        count[a[j][-1]] += 1

    ans = min(max(count), ans)
    num = count.index(max(count))
    used.add(num)
    for j in range(n):
        while True:
            if a[j][-1] in used:
                a[j].pop()
            else:
                break

print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
a = [list(map(int, input().split())) for i in range(n)]
for i in range(n):
    a[i].reverse()

ans = n
used = set()
for i in range(m):
    count = [0]*(m+1)
    for j in range(n):
        count[a[j][-1]] += 1

    ans = min(max(count), ans)
    num = count.index(max(count))
    used.add(num)
    for j in range(n):
        if a[j][-1] in num:
            a[j].pop()

print(ans)

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *A = map(int, read().split())
A = list(map(list, map(reversed, zip(*[iter(A)] * M))))

# 各スポーツに参加している人数
p = [0] * (M + 1)
canceled = set()
answer = []


def solve(s, n):
    # nは開催しているスポーツの数
    # sは新たに開催をやめるスポーツ
    p[s] = 0
    canceled.add(s)
    if n == M:
        for i in A:
            p[i[-1]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1)
    elif n == 1:
        answer.append(N)
    else:
        for i in range(N):
            if A[i][-1] in canceled:
                while A[i][-1] in canceled:
                    A[i].pop()
                p[A[i][-1]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1)


solve(0, M)
print(min(answer))

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *A = map(int, read().split())
A = tuple(zip(*[iter(A)] * M))

# 各スポーツに参加している人数
p = [0] * (M + 1)
# 人iはn番目に好きなスポーツに参加している
sports = [0] * N
answer = []


def solve(s, n, p, sports):
    # nは開催しているスポーツの数
    # sは新たに開催をやめるスポーツ
    p[s] = 0
    if n == M:
        for i in A:
            p[i[0]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1, p, sports)
    elif n == 1:
        answer.append(N)
    else:
        for idx, i in enumerate(A):
            if i[sports[idx]] == s:
                sports[idx] += 1
                p[i[sports[idx]]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1, p, sports)


solve(0, M, p, sports)
print(min(answer))

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *A = map(int, read().split())
A = tuple(zip(*[iter(A)] * M))

# 各スポーツに参加している人数
p = [0] * (M + 1)
# 人iはn番目に好きなスポーツに参加している
sports = [0] * (N + 1)
answer = []


def solve(s, n, p, sports):
    # nは開催しているスポーツの数
    # sは新たに開催をやめるスポーツ
    p[s] = 0
    if n == M:
        for i in A:
            p[i[0]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1, p, sports)
    elif n == 1:
        answer.append(N)
    else:
        for idx, i in enumerate(A, 1):
            if i[sports[idx]] == s:
                sports[idx] += 1
                p[i[sports[idx]]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1, p, sports)


solve(0, M, p, sports)
print(min(answer))

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *A = map(int, read().split())
A = tuple(zip(*[iter(A)] * M))

# 各スポーツに参加している人数
p = [0] * (M + 1)
# 人iはn番目に好きなスポーツに参加している
sports = [0] * (N + 1)
answer = []


def solve(s, n, p, sports):
    # nは開催しているスポーツの数
    # sは新たに開催をやめるスポーツ
    p[s] = 0
    if n == M:
        for i in A:
            p[i[0]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1, p, sports)
    elif n == 1:
        pass
    else:
        for idx, i in enumerate(A, 1):
            if i[sports[idx]] == s:
                sports[idx] += 1
                p[i[sports[idx]]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = p.index(p_max)
        solve(cancel, n - 1, p, sports)


solve(0, M, p, sports)
print(min(answer))

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *A = map(int, read().split())
A = tuple(zip(*[iter(A)] * M))

# 各スポーツに参加している人数
p = [0] * (M + 1)
# 人iはn番目に好きなスポーツに参加している
sports = [0] * (N + 1)
answer = []


def solve(s, n, p, sports):
    # nは開催しているスポーツの数
    # sは新たに開催をやめるスポーツ
    p[s] = 0
    if n == M:
        for i in A:
            p[i[0]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = [idx for idx, i in enumerate(p) if i == p_max]
        for i in cancel:
            solve(i, n - 1, p, sports)
    elif n == 1:
        pass
    else:
        for idx, i in enumerate(A, 1):
            if i[sports[idx]] == s:
                sports[idx] += 1
                p[i[sports[idx]]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = [idx for idx, i in enumerate(p) if i == p_max]
        for i in cancel:
            solve(i, n - 1, p, sports)


solve(0, M, p, sports)
print(min(answer))

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, M, *A = map(int, read().split())
A = tuple(zip(*[iter(A)] * M))

# 各スポーツに参加している人数
p = [0] * (M + 1)
# 人iはn番目に好きなスポーツに参加している
sports = [0] * (N + 1)
answer = []


def solve(s, n, p, sports):
    # nは開催しているスポーツの数
    # sは新たに開催をやめるスポーツ
    p[s] = 0
    if n == M:
        for i in A:
            p[i[0]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = [idx for idx, i in enumerate(p) if i == p_max]
        for i in cancel:
            solve(i, n - 1, p, sports)
    elif n == 1:
        pass
    else:
        for i in range(1, N + 1):
            if A[i - 1][sports[i]] == s:
                sports[i] += 1
                p[A[i - 1][sports[i]]] += 1
        p_max = max(p)
        answer.append(p_max)
        cancel = [idx for idx, i in enumerate(p) if i == p_max]
        for i in cancel:
            solve(i, n - 1, p, sports)


solve(0, M, p, sports)
print(min(answer))

Submission status: Runtime Error

Solution code:
N, M, *X = map(int, open('0').read().split())
A = []
for a in zip(*[iter(X)]*M):
  A.append(a)
log = set(range(1,M+1))
ans = N
while log:
  dic = {}
  for i in range(N):
    for j in range(M):
      if A[i][j] in log:
        dic[A[i][j]] = dic.get(A[i][j],0)+1
        break
  ans = min(ans,max(dic.values()))
  c = max(dic.keys(),key=lambda x:dic[x])
  log.remove(c)
print(ans)

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(N)]

L = [0]*(M+1)
I = [0]*N
ans = N
flag = True
while flag:
    cnt = [0]*(M+1)
    for i in range(N):
        while I[i] < N and L[A[i][I[i]]] == 1:
            I[i] += 1
        if I[i] == N:
            flag = False
            break
        cnt[A[i][I[i]]] += 1
        I[i] += 1
    num = max(cnt)
    ans = min(ans, num)
    for i in range(1, M+1):
        if cnt[i] == num:
            L[i] = 1
    if sum(L) == M:
        flag = False

print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(x):
    b = []
    for i in range(n):
        b.append([x for x in a[i]])
    #ddprint(b)
    cnt = 0
    rem = [False]*m
    while cnt<n:
        top = [0]*m
        ok = True
        for i in range(n):
            top[b[i][-1]] += 1
        for j in range(m):
            if top[j]>x:
                ok = False
                rem[j] = True
                cnt += 1
                if cnt==n:
                    return False
        if ok:
            return True
        for i in range(n):
            while len(b[i])>0 and rem[b[i][-1]]:
                b[i].pop()
    return True

n,m = inm()
a = []
for i in range(n):
    aa = inl()
    aa = [x-1 for x in aa]
    aa.reverse()
    a.append(aa)

ddprint(a)
mx = n
mn = 0
while mx>mn+1:
    mid = (mx+mn)//2
    if foo(mid):
        mx = mid
    else:
        mn = mid
print(mx)

Submission status: Runtime Error

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

def foo(x):
    b = []
    for i in range(n):
        b.append([x for x in a[i]])
    cnt = 0
    rem = [False]*m
    while cnt<n:
        top = [0]*m
        ok = True
        for i in range(n):
            top[b[i][-1]] += 1
        for j in range(m):
            if top[j]>x:
                ok = False
                rem[j] = True
                cnt += 1
                if cnt==n:
                    return False
        if ok:
            return True
        for i in range(n):
            while len(b[i])>0 and rem[b[i][-1]]:
                b[i].pop()
    return True

n,m = inm()
a = []
for i in range(m):
    aa = inl()
    aa = [x-1 for x in aa]
    aa.reverse()
    a.append(aa)
mx = n
mn = 0
while mx>mn+1:
    mid = (mx+mn)//2
    if foo(mid):
        mx = mid
    else:
        mn = mid
print(mx)

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict
n,m=map(int,input().split())
A=[[int(i) for i in input().split()] for _ in range(n)]
l=[int(i)+1 for i in range(m)]
min_=n
while True:
    d=defaultdict(int)
    for j in range(h):
        c=A[j-1]
        for i in range(m):
            if c[i] in l:
                d[c[i]] += 1
                break
    s=sorted(list(zip(d.keys(),d.values())),key = lambda x: x[1])
    if s[-1][1] <= min_:
        min_=s[-1][1]
        l.remove(s[-1][0])
    else:
        print(min_)
        sys.exit()

Submission status: Runtime Error

Solution code:
from collections import Counter

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

check = [0] * (M + 1)

answer = []
for n in range(N):
  tmp = []
  for ai in A:
    for aij in ai:
      if check[aij] == 0:
        tmp.append(aij)
        break
  #print(tmp)
  counter = list(Counter(tmp).items())
  counter.sort(key = lambda x: -x[1])  
  answer.append(counter[0][1])
  check[counter[0][0]] = 1
  
print(min(answer))

Submission status: Runtime Error

Solution code:
from collections import Counter

N, M = map(int, input().split())
A_list = []
for _ in range(N):
    a = list(map(int, input().split()))
    A_list.append(a)

ans = N
for _ in range(N-1):
    most_like_list = []
    for i in range(N):
        most_like_list.append(A_list[i][0])
    most_like, ans_new = Counter(most_like_list).most_common()[0]
    ans = min(ans, ans_new)
    for i in range(N):
        A_list[i].remove(most_like)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

N, M = map(int, input().split())
A_list = []
for _ in range(N):
    a = list(map(int, input().split()))
    A_list.append(a)

ans = 10 ** 10
for _ in range(N-1):
    most_like_list = []
    for i in range(N):
        most_like_list.append(A_list[i][0])
    most_like, ans_new = Counter(most_like_list).most_common()[0]
    ans = min(ans, ans_new)
    for i in range(N):
        A_list[i].remove(most_like)

print(ans)