Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int, input().split())
P = []
for _ in range(N):
  a,b = map(int, input().split())
  P.append((a,b))
  
ans = 5 * 10 ** 18

def solve(xl,xr,yl,yr):
  xm, xM = min(xl[0], xr[0]), max(xl[0], xr[0])
  ym, yM = min(yl[1], yr[1]), max(yl[1], yr[1])
  cnt = 0
  for k in range(N):
    p,q = P[k]
    if xm <= p <= xM and ym <= q <= yM:
      cnt += 1
  #print(xm, xM, ym, yM, cnt)
  return cnt >= K
    
def calc(xl,xr,yl,yr):
  return abs(xr[0] - xl[0]) * abs(yr[1] - yl[1])
  
for i in range(N):
  for j in range(i+1,N):
    for k in range(N):
      for l in range(k+1,N):
        if solve(P[i], P[j], P[k], P[l]):
          wk = calc(P[i], P[j], P[k], P[l])
          #rint(i,j,k,l,wk)
          ans = min(ans, wk)
      
print(ans)

Fixed submission:
N,K = map(int, input().split())
P = []
for _ in range(N):
  a,b = map(int, input().split())
  P.append((a,b))
  
ans = 5 * 10 ** 18

def solve(xl,xr,yl,yr):
  xm, xM = min(xl[0], xr[0]), max(xl[0], xr[0])
  ym, yM = min(yl[1], yr[1]), max(yl[1], yr[1])
  cnt = 0
  for k in range(N):
    p,q = P[k]
    if xm <= p <= xM and ym <= q <= yM:
      cnt += 1
  #print(xm, xM, ym, yM, cnt)
  return cnt >= K
    
def calc(xl,xr,yl,yr):
  return abs(xr[0] - xl[0]) * abs(yr[1] - yl[1])
  
for i in range(N):
  for j in range(i+1,N):
    for k in range(N):
      for l in range(k+1,N):
        if solve(P[i], P[j], P[k], P[l]):
          wk = calc(P[i], P[j], P[k], P[l])
          #rint(i,j,k,l,wk)
          ans = min(ans, wk)
      
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())

# a >= b >= c を仮定できる
# a,b,c <= 3500
# 4/N <= 3/a  a <= 0.75N
# 4/N >= 3/c  c >= 0.75N

amax = int(0.75 * N) + 2
bmin = int(0.75 * N) - 1
max = 3501
for b in range(bmin,max):
  for a in range(1,amax):
    res = 4/N - 1/a - 1/b
    if res<=0:
      continue
    candc = int(1/res)
    for c in range(candc-1,candc+2):
      if 4*a*b*c == N*(a*b + b*c + c*a):
        print(a,b,c)
        exit(0)



Fixed submission:
N = int(input())

# a >= b >= c を仮定できる
# a,b,c <= 3500
# 4/N <= 3/a  a <= 0.75N
# 4/N >= 3/c  c >= 0.75N

least = int(0.75 * N) + 2
max = 3501
for b in range(1,max):
  for a in range(1,least):
    res = 4/N - 1/a - 1/b
    if res<=0:
      continue
    candc = int(1/res)
    for c in range(candc-1,candc+2):
      if 4*a*b*c == N*(a*b + b*c + c*a):
        print(a,b,c)
        exit(0)
Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
N = int(input())

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

ans = 0
for i in range(N):
  for j in range(i+1,N):
    if D[i][j] < A[i][j]:
      print(-1)
      exit(0)
      
for i in range(N):
  for j in range(i+1,N):
    flg = True
    for k in range(N):
      if i == k or j == k:
        continue
      if D[i][j] == D[i][k] + D[k][j]:
        flg = False
        break
    if flg:
      ans += D[i][j]
      
print(int(ans))

Fixed submission:
#from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
import copy
N = int(input())

A = []
for _ in range(N):
  A.append(list(map(int, input().split())))
  
D = copy.deepcopy(A)
for i in range(N):
  D[i][i] = 0
  
for k in range(N):
  for i in range(N):
    for j in range(N):
      D[i][j] = min(D[i][j], D[i][k] + D[k][j])
      
#G = csgraph_from_dense(A)
#D = floyd_warshall(G)

ans = 0
for i in range(N):
  for j in range(i+1,N):
    if D[i][j] < A[i][j]:
      print(-1)
      exit(0)
      
for i in range(N):
  for j in range(i+1,N):
    flg = True
    for k in range(N):
      if i == k or j == k:
        continue
      if D[i][j] == D[i][k] + D[k][j]:
        flg = False
    if flg:
      ans += D[i][j]
      
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
N = int(input())

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

ans = 0
for i in range(N):
  for j in range(i+1,N):
    if D[i][j] < A[i][j]:
      print(-1)
      exit(0)
      
for i in range(N):
  for j in range(i+1,N):
    flg = True
    for k in range(N):
      if i == k or j == k:
        continue
      if D[i][j] == D[i][k] + D[k][j]:
        flg = False
    if flg:
      ans += D[i][j]
      
print(int(ans))

Fixed submission:
#from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
import copy
N = int(input())

A = []
for _ in range(N):
  A.append(list(map(int, input().split())))
  
D = copy.deepcopy(A)
for i in range(N):
  D[i][i] = 0
  
for k in range(N):
  for i in range(N):
    for j in range(N):
      D[i][j] = min(D[i][j], D[i][k] + D[k][j])
      
#G = csgraph_from_dense(A)
#D = floyd_warshall(G)

ans = 0
for i in range(N):
  for j in range(i+1,N):
    if D[i][j] < A[i][j]:
      print(-1)
      exit(0)
      
for i in range(N):
  for j in range(i+1,N):
    flg = True
    for k in range(N):
      if i == k or j == k:
        continue
      if D[i][j] == D[i][k] + D[k][j]:
        flg = False
    if flg:
      ans += D[i][j]
      
print(int(ans))
Buggy python submission (verdict: Runtime Error):
N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

def GCD(a,b):
  if a < b:
    a, b = b, a
  if b == 0:
    return a
  else:
    return GCD(b, a%b)

def LCM(a,b):
  return int(a*b/GCD(a,b))

prev = A[0]
for i in range(1,N):
  lcm = LCM(prev, A[i])
  prev = lcm

print(prev)

Fixed submission:
N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

def GCD(a,b):
  if a < b:
    a, b = b, a
  while b > 0:
    a, b = b, a%b
  
  return a

def LCM(a,b):
  return a*b // GCD(a,b)

lcm = A[0]
for i in range(1,N):
  lcm = LCM(lcm, A[i])

print(lcm)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

Amax = max(A)

i = 1
while i * Amax <= 1e18:
  fl = False
  for a in A:
    if i * Amax % a != 0:
      fl = True
      break
  if fl:
    i += 1
    continue
  else:
    break

print(i*Amax)

Fixed submission:
N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

def GCD(a,b):
  if a < b:
    a, b = b, a
  while b > 0:
    a, b = b, a%b
  
  return a

def LCM(a,b):
  return a*b // GCD(a,b)

lcm = A[0]
for i in range(1,N):
  lcm = LCM(lcm, A[i])

print(lcm)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

Amax = max(A)

i = 1
while i * Amax <= 1e18:
  fl = False
  for a in A:
    if i * Amax % a != 0:
      fl = True
      continue
  if fl:
    i += 1
    continue
  else:
    break

print(i*Amax)

Fixed submission:
N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

def GCD(a,b):
  if a < b:
    a, b = b, a
  while b > 0:
    a, b = b, a%b
  
  return a

def LCM(a,b):
  return a*b // GCD(a,b)

lcm = A[0]
for i in range(1,N):
  lcm = LCM(lcm, A[i])

print(lcm)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
from operator import itemgetter

N = int(input())
P = []
for i in range(N):
  x,y = map(int, input().split())
  P.append((x,y,i))
  
P.sort() 
D = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N-1):
  a,b,p = P[i]
  c,d,q = P[i+1]
  D[p][q] = min(abs(a-c), abs(b-d))
    
P.sort(key=itemgetter(1))
for i in range(N-1):
  a,b,p = P[i]
  c,d,q = P[i+1]
  D[p][q] = min(abs(a-c), abs(b-d))

mst = minimum_spanning_tree(D)
print(int(mst.sum()))

Fixed submission:
import numpy as np
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
from operator import itemgetter

N = int(input())
P = []
for i in range(N):
  x,y = map(int, input().split())
  P.append((x,y,i))
  
data = [10, 20, 30, 40]
row = [0, 0, 1, 1]
col = [1, 2, 0, 2]

#print(csr_matrix((data, (row, col))).toarray())
  
data = []
row = []
col = []

P.sort() 
D = {}
for i in range(N-1):
  a,b,p = P[i]
  c,d,q = P[i+1]
  cost = min(abs(a-c), abs(b-d))
  data.append(cost)
  row.append(p)
  col.append(q)
  data.append(cost)
  row.append(q)
  col.append(p)
  D[(p,q)] = cost
  D[(q,p)] = cost

P.sort(key=itemgetter(1))
for i in range(N-1):
  a,b,p = P[i]
  c,d,q = P[i+1]
  if (p,q) in D:
    continue
  cost = min(abs(a-c), abs(b-d))
  data.append(cost)
  row.append(p)
  col.append(q)
  data.append(cost)
  row.append(q)
  col.append(p)
  D[(p,q)] = cost
  D[(q,p)] = cost

coo = coo_matrix((data, (row, col)), (N, N))
mst = minimum_spanning_tree(coo)
print(int(mst.sum()))
Buggy python submission (verdict: Runtime Error):
S = input()

li = set()
for i in len(S):
  li.add(S[i])

if len(S) == len(li):
  print("yes")
else:
  print("no")

Fixed submission:
S = input()

li = set()
for i in range(len(S)):
  li.add(S[i])

if len(S) == len(li):
  print("yes")
else:
  print("no")
Buggy python submission (verdict: Runtime Error):
N, W = map(int, input().split())
P = []
for i in range(N):
  P.append(list(map(int, input().split())))

#dp[i][w] : i番目までの品物で、wの重みでできる最大の価値. 1-indexed
dp = [[0] * (W+1) for i in range(N+1)]

for i in range(1,N+1):
  weight = P[i-1][0]
  value = P[i-1][1]

  for j in range(W+1):
    if j - weight >= 0:
      dp[i][j] = max(dp[i-1][j-weight] + value, dp[i-1][j])
    else:
      dp[i][j] = dp[i-1][j]

#print(dp)
print(max(dp[N][:]))

Fixed submission:
N, W = map(int, input().split())

W1,V1 = map(int, input().split())

P = {W1:[V1], W1+1:[], W1+2:[], W1+3:[]}

for i in range(1,N):
  w,v = map(int, input().split())
  P[w].append(v)

PS={}
for k,v in P.items():
  v.sort(reverse=True)
  S = [0] * (len(v)+1)
  for i in range(len(v)):
    S[i+1] = S[i] + v[i]
  PS[k] = S
  
#print(P)
#print(PS)
A,B,C,D = len(PS[W1]), len(PS[W1+1]), len(PS[W1+2]), len(PS[W1+3])
ans = 0
for a in range(A):
  for b in range(B):
    for c in range(C):
      for d in range(D):
        if a*W1 + b*(W1+1) + c*(W1+2) + d*(W1+3) > W:
          continue
        ans = max(ans, PS[W1][a] + PS[W1+1][b] + PS[W1+2][c] + PS[W1+3][d])
        
print(ans)
Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce
from math import sqrt

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

N, A, B = list(map(int, input().split()))
V = list(map(int, input().split()))

V.sort(reverse = True)

print(sum(V[:A]) / A)

# A番目と一緒の数字の数（A個より外で）
cnt_ext = 0
for i in range(A,min(A+B,N)):
  if V[A-1] == V[i]:
    cnt_ext += 1
    
# A番目と一緒の数字の数（A個の中で）
cnt_int = 0
for i in range(0,A):
  if V[A-1] == V[i]:
    cnt_int += 1
    
#print(cnt_int, cnt_ext)
cnt_all = cnt_int + cnt_ext

ans = 0
for i in range(A,B+1):
  ans += cmb(cnt_all, cnt_int)
  cnt_int += 1
  
print(ans)

Fixed submission:
from collections import Counter

from scipy.misc import comb
#a = comb(n, r, exact=True)

N, A, B = list(map(int, input().split()))
V = list(map(int, input().split()))

V.sort(reverse = True)
C = Counter(V)

print(sum(V[:A]) / A)

max_value = V[0]

# V[0], ... V[A-1] がすべて同じとき、そうでないときで異なる
# V[0], ... V[A-1] に異なるものが含まれると、Aコ選ぶときがMAX
key_value = V[A-1]
if key_value < max_value:
  # V[A-1]の個数と、その前に何個あるかを数えればよい
  forward_item_count = 0
  for k,v in C.items():
    if k > key_value:
      forward_item_count += v

  print(int(comb(C[key_value], A - forward_item_count, exact=True)))
  exit(0)
  
# すべて同じときはAコ～Bコ選んでも平均一緒。組み合わせ数を足していく必要性
ans = 0
cnts = C[key_value]
for i in range(A,B+1):
  ans += comb(cnts,i, exact=True)
  
print(int(ans))
Buggy python submission (verdict: Runtime Error):
W, A, B = map(int, input().split())

if B >= A+W:
  print(abs(A+W-b))
elif B+W <= A:
  print(abs(B+W-A))
else:
  print(0)


Fixed submission:
W, A, B = map(int, input().split())

if B >= A+W:
  print(abs(A+W-B))
elif B+W <= A:
  print(abs(B+W-A))
else:
  print(0)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

cnt = 0 
for x in A:
  if A%2 == 1:
    cnt += 1
    
if cnt % 2 == 0:
  print("YES")
else:
  print("NO")

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

cnt = 0 
for x in A:
  if x%2 == 1:
    cnt += 1
    
if cnt % 2 == 0:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
import heapq
from collections import deque
sys.setrecursionlimit(200000)

N = int(input())
X = [[] for _ in range(N)]
for i in range(N-1):
  a = int(input())
  X[a-1].append(i+1)
  
# dfsをして、Depthを計算しつつ、depth大きい順に取り出せるようにheapqを使う
h = []

seen = [False] * N
def dfs(a,depth):
  seen[a] = True
  heapq.heappush(h, (-depth, a))
  for nxt in X[a]:
    #if seen[nxt]:
      #continue
    dfs(nxt, depth+1)

dfs(0,0)
#rint(len(h))
DP = [0] * N
while h:
  depth, a = heapq.heappop(h)
  cand = []
  for child in X[a]:
    cand.append(DP[child])
  if not cand:
    DP[a] = 1
    continue
  cand.sort(reverse=True)
  for i in range(len(cand)):
    DP[a] = max(DP[a], cand[i]+i+1)
    
print(DP[0]-1)

Fixed submission:
import sys
input = sys.stdin.readline
import heapq
from collections import deque
sys.setrecursionlimit(200000)

N = int(input())
X = [[] for _ in range(N)]
for i in range(N-1):
  a = int(input())
  X[a-1].append(i+1)
  
# dfsをして、Depthを計算しつつ、depth大きい順に取り出せるようにheapqを使う
h = []

seen = [False] * N
def dfs(a,depth):
  seen[a] = True
  heapq.heappush(h, (-depth, a))
  for nxt in X[a]:
    #if seen[nxt]:
      #continue
    dfs(nxt, depth+1)

dfs(0,0)
#rint(len(h))
DP = [0] * N
while h:
  depth, a = heapq.heappop(h)
  cand = []
  for child in X[a]:
    cand.append(DP[child])
  if not cand:
    DP[a] = 1
    continue
  cand.sort(reverse=True)
  for i in range(len(cand)):
    DP[a] = max(DP[a], cand[i]+i+1)
    
print(DP[0]-1)

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
import heapq
from collections import deque

N = int(input())
X = [[] for _ in range(N)]
for i in range(N-1):
  a = int(input())
  X[a-1].append(i+1)
  
# dfsをして、Depthを計算しつつ、depth大きい順に取り出せるようにheapqを使う
h = []

seen = [False] * N
def dfs(a,depth):
  seen[a] = True
  heapq.heappush(h, (-depth, a))
  for nxt in X[a]:
    #if seen[nxt]:
      #continue
    dfs(nxt, depth+1)

dfs(0,0)
#rint(len(h))
DP = [0] * N
while h:
  depth, a = heapq.heappop(h)
  cand = []
  for child in X[a]:
    cand.append(DP[child])
  if not cand:
    DP[a] = 1
    continue
  cand.sort(reverse=True)
  for i in range(len(cand)):
    DP[a] = max(DP[a], cand[i]+i+1)
    
print(DP[0]-1)

Fixed submission:
import sys
input = sys.stdin.readline
import heapq
from collections import deque
sys.setrecursionlimit(200000)

N = int(input())
X = [[] for _ in range(N)]
for i in range(N-1):
  a = int(input())
  X[a-1].append(i+1)
  
# dfsをして、Depthを計算しつつ、depth大きい順に取り出せるようにheapqを使う
h = []

seen = [False] * N
def dfs(a,depth):
  seen[a] = True
  heapq.heappush(h, (-depth, a))
  for nxt in X[a]:
    #if seen[nxt]:
      #continue
    dfs(nxt, depth+1)

dfs(0,0)
#rint(len(h))
DP = [0] * N
while h:
  depth, a = heapq.heappop(h)
  cand = []
  for child in X[a]:
    cand.append(DP[child])
  if not cand:
    DP[a] = 1
    continue
  cand.sort(reverse=True)
  for i in range(len(cand)):
    DP[a] = max(DP[a], cand[i]+i+1)
    
print(DP[0]-1)
Buggy python submission (verdict: Time Limit Exceeded):
N,M = list(map(int,input().split()))
dist = [[1e9 for i in range(N)] for j in range(N)]
E = []

for i in range(N):
  dist[i][i] = 0

for i in range(M):
  a,b,c = list(map(int,input().split()))
  a, b = a-1, b-1 # 0-indexed
  dist[a][b] = c
  dist[b][a] = c
  E.append([a,b,c])
  
# Warshal-Floyd
for k in range(N):
  for i in range(N):
    for j in range(N):
      dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
  
ans = 0
for e in E:
  a,b,c = e
  flg = False
  for i in range(N):
    for j in range(N):
      if i == j : #or i == a or i == b or j == a or j == b:
        continue
      if dist[i][a] + c + dist[b][j] == dist[a][b]:
        #print(i,a,b,j)
       # print(dist[i][a], c, dist[b][j] , ":", dist[a][b])
        flg = True
        break
    if flg:
      break
  if not flg:
    ans += 1
    
print(ans)

Fixed submission:
N,M = map(int, input().split())

G = [[] for _ in range(N)]
INF = 10**9
D = [[ INF for _ in range(N)] for _ in range(N)]
for i in range(N):
  D[i][i] = 0

E = []
for _ in range(M):
  a,b,c = map(int, input().split())
  a,b = a-1, b-1
  G[a].append((b,c))
  G[b].append((a,c))
  D[a][b] = c
  D[b][a] = c
  E.append((a,b,c))
  

for k in range(N):
  for i in range(N):
    for j in range(N):
      if D[i][k] + D[k][j] < D[i][j]:
        D[i][j] = D[i][k] + D[k][j]
        D[j][i] = D[i][k] + D[k][j]
  
ans = 0
for a,b,c in E:
  flg = False
  for i in range(N):
    if D[i][a] + c == D[i][b]:
      flg = True
      break
   
  if not flg:
    ans += 1
    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = []
for _ in range(N):
  A.append(int(input()))
  
cnt = A[0] - 1
cant_use = 1
for a in A[1:]:
  wk = a
  if wk == cant_use+1:
    #cnt += 1
    cant_use += 1
    continue
    
  while wk > cant_use:
    if wk % (cant_use+1) == 0:
      wk -= cant_use + 2
    else:
      wk -= cant_use + 1
    cnt += 1
print(cnt)

Fixed submission:
N = int(input())
A = []
for _ in range(N):
  A.append(int(input()))
  
cnt = A[0] - 1
cant_use = 1
for a in A[1:]:
  wk = a
  if wk == cant_use+1:
    #cnt += 1
    cant_use += 1
    continue
    
  if wk % (cant_use+1) == 0:
    wk -= cant_use + 2
    cnt += 1
  cnt += wk // (cant_use+1)
print(cnt)
Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())

A = [" " for _ in range(K)]

zan = K
for i in range(len(S)):
  n = (ord('z') - ord(S[i]) + 1) % 26 # aに変換するのに必要な回数
  if zan >= n:
    zan -= n
    A[i] = 'a'
  else:
    A[i] = S[i]

if zan > 0:
  A[i] = chr((ord(A[i]) - ord('a') + zan)%26 + ord('a'))
print("".join(A))

Fixed submission:
S = input()
K = int(input())

A = [" " for _ in range(len(S))]

zan = K
for i in range(len(S)):
  n = (ord('z') - ord(S[i]) + 1) % 26 # aに変換するのに必要な回数
  if zan >= n:
    zan -= n
    A[i] = 'a'
  else:
    A[i] = S[i]

if zan > 0:
  A[-1] = chr((ord(A[-1]) - ord('a') + zan)%26 + ord('a'))
print("".join(A))
Buggy python submission (verdict: Time Limit Exceeded):
h, w, m = map(int, input().split())
target_points = {tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(m)}

row_points = [0] * h
col_points = [0] * w
for row, col in target_points:
    row_points[row] += 1
    col_points[col] += 1

row_max = max(row_points)
col_max = max(col_points)

row_max_indexes = [i for i in range(h) if row_points[i] == row_max]
col_max_indexes = [i for i in range(w) if col_points[i] == col_max]

crossing_points = {(row, col) for row in row_max_indexes for col in col_max_indexes}

print(row_max + col_max - (not (crossing_points - target_points)))


Fixed submission:
h, w, m = map(int, input().split())
target_points = {tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(m)}

row_points = [0] * h
col_points = [0] * w
for row, col in target_points:
    row_points[row] += 1
    col_points[col] += 1

row_max = max(row_points)
col_max = max(col_points)

row_max_indexes = [i for i in range(h) if row_points[i] == row_max]
col_max_indexes = [i for i in range(w) if col_points[i] == col_max]

empty_crossing_exist = False
for row in row_max_indexes:
    for col in col_max_indexes:
        if (row, col) not in target_points:
            empty_crossing_exist = True
            break
    if empty_crossing_exist:
        break

print(row_max + col_max - (not empty_crossing_exist))

Buggy python submission (verdict: Time Limit Exceeded):
h, w, m = map(int, input().split())
target_points = {tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(m)}

row_points = [0] * h
col_points = [0] * w
for row, col in target_points:
    row_points[row] += 1
    col_points[col] += 1

row_max = max(row_points)
col_max = max(col_points)

row_max_indexes = [i for i in range(h) if row_points[i] == row_max]
col_max_indexes = [i for i in range(w) if col_points[i] == col_max]

crossing_points = {(row, col) for row in row_max_indexes for col in col_max_indexes}

print(row_max + col_max - (not (crossing_points - target_points)))


Fixed submission:
h, w, m = map(int, input().split())
target_points = {tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(m)}

row_points = [0] * h
col_points = [0] * w
for row, col in target_points:
    row_points[row] += 1
    col_points[col] += 1

row_max = max(row_points)
col_max = max(col_points)

row_max_indexes = [i for i in range(h) if row_points[i] == row_max]
col_max_indexes = [i for i in range(w) if col_points[i] == col_max]

empty_crossing_exist = False
for row in row_max_indexes:
    for col in col_max_indexes:
        if (row, col) not in target_points:
            empty_crossing_exist = True
            break
    if empty_crossing_exist:
        break

print(row_max + col_max - (not empty_crossing_exist))
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
p = list(map(lambda x: int(x) - 1, input().split()))
c = list(map(int, input().split()))

max_score = min(c)
for init_mass in range(n):
    curr_mass = p[init_mass]
    scores = [c[init_mass]]
    while curr_mass != init_mass:
        scores.append(scores[-1] + c[curr_mass])
        curr_mass = p[curr_mass]
    cycle_score = scores[-1]

    if len(scores) >= k:
        score = max(scores[:k])
    elif cycle_score <= 0:
        score = max(scores)
    else:
        cycle_len = len(scores)
        max_cycle_num, cycle_rem = divmod(k, cycle_len)
        score = max(cycle_score * (max_cycle_num - 1) + max(scores),
                    cycle_score * max_cycle_num + max(scores[:cycle_rem]))

    if score > max_score:
        max_score = score

print(max_score)


Fixed submission:
n, k = map(int, input().split())
p_list = list(map(lambda x: int(x) - 1, input().split()))
c_list = list(map(int, input().split()))

max_score = -(1 << 64)
cycles = []
cycled = [False] * n
for i in range(n):
    if cycled[i]:
        continue

    cycle = [i]
    cycled[i] = True
    while p_list[cycle[-1]] != cycle[0]:
        cycle.append(p_list[cycle[-1]])
        cycled[cycle[-1]] = True

    cycles.append(cycle)

for cycle in cycles:
    cycle_len = len(cycle)

    accum = [0]
    for i in range(cycle_len * 2):
        accum.append(accum[-1] + c_list[cycle[i % cycle_len]])

    cycle_score = accum[cycle_len]

    max_series_sums = [max([accum[i + length] - accum[i]
                            for i in range(cycle_len)])
                       for length in range(cycle_len + 1)]

    if cycle_len >= k:
        score = max(max_series_sums[1:k + 1])
    elif cycle_score <= 0:
        score = max(max_series_sums[1:])
    else:
        max_cycle_num, cycle_rem = divmod(k, cycle_len)
        score = max(cycle_score * (max_cycle_num - 1) + max(max_series_sums),
                    cycle_score * max_cycle_num + max(max_series_sums[:cycle_rem + 1]))

    max_score = max(max_score, score)

print(max_score)