Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
c = input()

cnt = 0
while 'WR' in c:
    w = c.find('W')
    r = c.rfind('R')
    c = c[:w]+'R'+c[w+1:r]+'W'+c[r+1:]
    cnt += 1
print(cnt)

Fixed submission:
n = int(input())
c = input()

print(c[:c.count('R')].count('W'))

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

cnt = 0
while 'WR' in c:
    w = c.find('W')
    r = c.rfind('R')
    c = c[:w]+'R'+c[w+1:r]+'W'+c[r+1:]
    cnt += 1
print(cnt)

Fixed submission:
n = int(input())
c = input()

print(c[:c.count('R')].count('W'))
Buggy python submission (verdict: Runtime Error):
n = int(input())

abc = 'abcdefghijklmnopqrstuvwxyz'
s = ''
while n > 0:
    n -= 1
    n, mod = divmod(n, 26)
    s += a[mod]
print(s[::-1])

Fixed submission:
n = int(input())
abc = 'abcdefghijklmnopqrstuvwxyz'
ans = ''
while n != 0:
    n -= 1
    ans += abc[n%26]
    n //= 26
print(ans[::-1])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
ans = 1
for i in range(len(a)):
  ans *= a[i]
if ans > 10**18:
  ans = -1
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
ans = 1
if 0 in a:
  print(0)
  exit()
for i in range(len(a)):
  ans *= a[i]
  if ans > 10**18:
    print(-1)
    exit()
print(ans)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [[0] * n for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    if h[a-1] > h[b-1]:
        ab[b-1][a-1] = 1
    elif h[b-1] > h[a-1]:
        ab[a-1][b-1] = 1
    else:
        ab[a-1][b-1] = 1
        ab[b-1][a-1] = 1

ans = 0
for i in range(n):
    if 1 not in ab[i]:
        ans += 1
print(ans)

Fixed submission:
n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [True] * n
for _ in range(m):
    a, b = map(int, input().split())
    if h[a-1] > h[b-1]:
        ab[b-1] = False
    elif h[b-1] > h[a-1]:
        ab[a-1] = False
    else:
        ab[a-1] = False
        ab[b-1] = False

print(ab.count(True))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [[0] * n for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    if h[a-1] > h[b-1]:
        ab[b-1][a-1] = 1
    elif h[b-1] > h[a-1]:
        ab[a-1][b-1] = 1
    else:
        ab[a-1][b-1] = 1
        ab[b-1][a-1] = 1

ans = 0
for i in range(n):
    if 1 not in ab[i]:
        ans += 1
print(ans)

Fixed submission:
n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [True] * n
for _ in range(m):
    a, b = map(int, input().split())
    if h[a-1] > h[b-1]:
        ab[b-1] = False
    elif h[b-1] > h[a-1]:
        ab[a-1] = False
    else:
        ab[a-1] = False
        ab[b-1] = False

print(ab.count(True))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [[0] * n for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    ab[a-1][b-1] = 1
    ab[b-1][a-1] = 1

ans = 0
for i in range(n):
    flag = True
    for j in range(n):
        if ab[i][j] == 1:
            if h[j] >= h[i]:
                flag = False
                break
    if flag:
        ans += 1
print(ans)

Fixed submission:
n, m = map(int, input().split())
h = list(map(int, input().split()))
ab = [True] * n
for _ in range(m):
    a, b = map(int, input().split())
    if h[a-1] > h[b-1]:
        ab[b-1] = False
    elif h[b-1] > h[a-1]:
        ab[a-1] = False
    else:
        ab[a-1] = False
        ab[b-1] = False

print(ab.count(True))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

b = sorted(a)
for i in range(1, n+1):
    print(b.count(i))

Fixed submission:
n = int(input())
a = list(map(int, input().split()))

b = [0] * n
for i in a:
    b[i-1] += 1
for i in b:
    print(i)
Buggy python submission (verdict: Time Limit Exceeded):
import math
import itertools

w = 0
k = int(input())
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            m = math.gcd(i, j)
            w += math.gcd(m, l)
print(w)

Fixed submission:
import math
 
w = 0
k = int(input())
for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            m = math.gcd(i, j)
            w += math.gcd(m, l)
print(w)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
s = set([])
for i in range(N-2):
    for j in range(i+1, N-1):
        for k in range(j+1,N):
            s.add(S[i]+S[j]+S[k])
print(len(s))

Fixed submission:
N = int(input())
S = input()
S1 = set([])
S2 = set([])
S3 = set([])
for i in S:
    for s3 in S2:
        S3.add(s3+i)
    for s2 in S1:
        S2.add(s2+i)
    S1.add(i)
print(len(S3))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]

q = deque()
for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            q.append((i, j, 0)) 

while q:
    y, x, d = q.popleft()
    if a[y][x] == '#':
        for xdx, ydy in zip((x, x, x+1, x-1), (y+1, y-1, y, y)):
            if (0 <= xdx < w) and (0 <= ydy < h):
                if a[ydy][xdx] == '.':
                    a[ydy][xdx] = '#'
                    q.append((ydy, xdx, d+1))
print(d)

Fixed submission:
from collections import deque

h, w = map(int, input().split())
a = [list(input()) for _ in range(h)]

q = deque()

for i in range(h):
    for j in range(w):
        if a[i][j] == '#':
            q.append((i, j, 0)) 

while q:
    y, x, d = q.popleft()
    if a[y][x] == '#':
        for xdx, ydy in zip((x, x, x+1, x-1), (y+1, y-1, y, y)):
            if (0 <= xdx < w) and (0 <= ydy < h):
                if a[ydy][xdx] == '.':
                    a[ydy][xdx] = '#'
                    q.append((ydy, xdx, d+1))
print(d)
Buggy python submission (verdict: Runtime Error):
import math
abc = [int(input()) for _ in range(5)]
tmp = 9
ans = 0
for i in range(5):
    tmpp = min(tmp, (abc[i]-1)%10)
    if tmp != tmpp:
        tmp = tmpp
        ii = i
for i in range(5):
    if i == ii:
        ans += abc[i]
    else:
        ans += (math.ceil(abc[i]/10))*10
print(ans)

Fixed submission:
import math
abc = [int(input()) for _ in range(5)]
tmp = 9
ans = 0
ii = 0
for i in range(5):
    tmpp = min(tmp, (abc[i]-1)%10)
    if tmp != tmpp:
        tmp = tmpp
        ii = i
for i in range(5):
    if i == ii:
        ans += abc[i]
    else:
        ans += (math.ceil(abc[i]/10))*10
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0]
for i in range(1, n):
  tmp = float('inf')
  for j in range(k):
    if i-j > 0:
      tmp = min(tmp, dp[-1-j]+abs(h[i]-h[i-1-j]))
  dp.append(tmp)
print(dp[-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0]
INF = float('inf')
for i in range(1, n):
  act = INF
  for j in range(k):
    if i-j > 0:
      act = min(act, dp[-1-j]+abs(h[i]-h[i-j-1]))
    if act == 0:
      break
  dp.append(act)
print(dp[-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [0]
INF = float('inf')
for i in range(1, n):
  act = INF
  for j in range(k):
    if i-j > 0:
      act = min(act, dp[-1-j]+abs(h[i]-h[i-j-1]))
  dp.append(act)
print(dp[-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])

Fixed submission:
N, K = map(int, input().split())
h = list(map(int, input().split()))
 
DP = [0]
for i in range(N-1):
    act = float("inf")
    for k in range(K):
        if i-k < 0 : continue
        act = min(DP[-1-k] + abs(h[i-k]-h[i+1]) , act)
    DP += [act]
 
print(DP[-1])
Buggy python submission (verdict: Runtime Error):
from itertools import combinations
N, M = map(int, input().split())
link = [[0 for i in range(N)] for j in range(N)]
for m in range(M):
    x, y = map(int, input().split())
    link[x-1][y-1] = 1
    link[y-1][x-1] = 1

for i in range(N,1,-1): # N, N-1, N-2, ... 2
    for comb in combinations(range(N),i):
        for c in combinations(comb, 2): # 議員の集合に対して考えられるペアを列挙
            if (link[c[0]][c[1]] == 0): # 一つでも知り合いでない組があればNG
                break
        else: # 派閥が作れた段階でプログラムを終了
            print (i)
            exit()
print (1)

Fixed submission:
d, g = map(int, input().split())
pc = [list(map(int, input().split())) for _ in range(d)]

ans = 100*11
for i in range(2**d):
    sum = 0
    cnt = 0
    a = []
    for j in range(d):
        if (i >> j) & 1 == 1:
            sum += (j+1)*100*pc[j][0] + pc[j][1]
            cnt += pc[j][0]
            a.append(j)
    if sum < g:
        for j in range(d):
            if d-j-1 not in a:
                for k in range(pc[d-j-1][0]):
                    if sum >= g:
                        break
                    sum += (d-j)*100
                    cnt += 1
    ans = min(ans, cnt)
print(ans)
Buggy python submission (verdict: Runtime Error):
n, m, x = map(int, input().split())
s = sum(i if int(i)<x for i in input().split())
print(min(s, m-s))

Fixed submission:
n, m, x = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*(n+1)
for i in a:
    b[i] = 1
print(min(sum(b[:x:]), sum(b[x:n+1:])))
Buggy python submission (verdict: Runtime Error):
N = list(input())
print('Yes' if N[1] == N[2] and (N[0] == N[1] or N[2] == N[3]) else 'No')a

Fixed submission:
N = list(input())
print('Yes' if N[1] == N[2] and (N[0] == N[1] or N[2] == N[3]) else 'No')
Buggy python submission (verdict: Time Limit Exceeded):
import math
n = int(input())
ans = 10
a = int(math.sqrt(n))
for i in range(a, n+1):
    q, mod = divmod(n, i)
    if mod == 0:
        print(max(map(len, map(str, [i, q]))))
        exit()

Fixed submission:
n = int(input())
m = int(n**0.5)

while n%m:
    m -= 1

print(len(str(n//m)))
Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())

ans = 0
for x in range(k+1):
    if x > s:
        break
    if x+k+k < s:
        continue
    for y in range(k+1):
        xy = x+y
        if xy > s:
            break
        if xy+k < s:
            continue
        for z in range(k+1):
            xyz = x+y+z
            if xyz > s:
                break
            if xyz == s:
                ans += 1
print(ans)

Fixed submission:
k, s = map(int, input().split())
 
ans = 0
for x in range(k+1):
    for y in range(k+1):
        ans += max(0, 0<=s-x-y<=k)
print(ans)

Buggy python submission (verdict: Runtime Error):
k, s = map(int, input().split())

ans = 0
for x in range(k+1):
    if x > s:
        break
    if x+k+k < s:
        continue
    for y in range(k+1):
        xy = x+y
        if xy > s:
            break
        if xy+k < s:
            continue:
        for z in range(k+1):
            xyz = x+y+z
            if xyz > s:
                break
            if xyz == s:
                ans += 1
print(ans)

Fixed submission:
k, s = map(int, input().split())
 
ans = 0
for x in range(k+1):
    for y in range(k+1):
        ans += max(0, 0<=s-x-y<=k)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())

ans = 0
for x in range(k+1):
    if x > s:
        break
    for y in range(k+1):
        xy = x+y
        if xy > s:
            break
        for z in range(k+1):
            xyz = x+y+z
            if xyz > s:
                break
            if xyz == s:
                ans += 1
print(ans)

Fixed submission:
k, s = map(int, input().split())
 
ans = 0
for x in range(k+1):
    for y in range(k+1):
        ans += max(0, 0<=s-x-y<=k)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())

ans = 0
for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x+y+z == s:
                ans += 1
print(ans)

Fixed submission:
k, s = map(int, input().split())
 
ans = 0
for x in range(k+1):
    for y in range(k+1):
        ans += max(0, 0<=s-x-y<=k)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
k, s = map(int, input().split())

a = list(itertools.combinations_with_replacement(range(0, k+1), 3))
ans = 0
for i in a:
    if sum(i) == s:
        if i[0] == i[1] == i[2]:
            ans += 1
        elif i[0] != i[1] != i[2] != i[0]:
            ans += 6
        else:
            ans += 3
print(ans)

Fixed submission:
k, s = map(int, input().split())
 
ans = 0
for x in range(k+1):
    for y in range(k+1):
        ans += max(0, 0<=s-x-y<=k)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
k, s = map(int, input().split())

ans = 0
for x in range(k+1):
    for y in range(k+1):
        for z in range(k+1):
            if x+y+z == s:
                ans += 1
print(ans)

Fixed submission:
k, s = map(int, input().split())
 
ans = 0
for x in range(k+1):
    for y in range(k+1):
        ans += max(0, 0<=s-x-y<=k)
print(ans)
Buggy python submission (verdict: Runtime Error):
n, l = map(int, input().split())
s = sorted([map(int, input().split()) for _ in range(n)])
print(*s, sep='')

Fixed submission:
n, l = input().split()
n = int(n)
s = [input() for _ in range(n)]
s.sort()
print(*s, sep='')
Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
scores = list(map(int, input().split()))
right = k - 1
left = right - k + 1
prev = 1
now = 0
for i in range(left, right + 1):
    prev *= scores[left]
left += 1
right += 1

while right < n:
    now = prev / (scores[left - 1]) * scores[right]
    if now > prev:
        print("Yes")
    else:
        print("No")
    
    prev, now = now, 1

    right += 1
    left += 1

Fixed submission:
n, k = map(int, input().split())
scores = list(map(int, input().split()))
right = k
left = right - k + 1

while right < n:
    if scores[right] > scores[left-1]:
        print("Yes")
    else:
        print("No")

    right += 1
    left += 1
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
maze = [list(input()) for i in range(H)]
dp = [[float('inf')]*W for i in range(H)]
dp[0][0] = 0

q = deque([[0, 0]])

while len(q):
    pos = q.popleft()
    posX, posY = pos
    if posX + 1 < W:
        rightX, rightY = posX + 1, posY
        if maze[posY][posX] == "." and maze[rightY][rightX] == "#":
            dp[rightY][rightX] = min(dp[posY][posX] + 1, dp[rightY][rightX])
        else:
            dp[rightY][rightX] = min(dp[posY][posX], dp[rightY][rightX])
        q.append([rightX, rightY])
        
    if posY + 1 < H:
        downX, downY = posX, posY + 1
        if maze[posY][posX] == "." and maze[downY][downX] == "#":
            dp[downY][downX] = min(dp[posY][posX] + 1, dp[downY][downX])
        else:
            dp[downY][downX] = min(dp[posY][posX], dp[downY][downX])
        q.append([downX, downY])

if maze[0][0] == "#":
    print(dp[-1][-1] + 1)
else:
    print(dp[-1][-1])

Fixed submission:
H, W = map(int, input().split())
maze = [list(input()) for i in range(H)]
dp = [[float('inf')]*W for i in range(H)]
dp[0][0] = 0

for y in range(H):
    for x in range(W):
        if y == 0 and x == 0:
            continue
        
        if x >= 1:
            if maze[y][x - 1] == "." and maze[y][x] == "#":
                dp[y][x] = min(dp[y][x - 1] + 1, dp[y][x])
            else:
                dp[y][x] = min(dp[y][x - 1], dp[y][x])
        
        if y >= 1:
            if maze[y - 1][x] == "." and maze[y][x] == "#":
                dp[y][x] = min(dp[y - 1][x] + 1, dp[y][x])
            else:
                dp[y][x] = min(dp[y - 1][x], dp[y][x])

if maze[0][0] == "#":
    print(dp[-1][-1] + 1)
else:
    print(dp[-1][-1])
Buggy python submission (verdict: Runtime Error):
A, B = map(int, input().split())

if B % A == 0:
    print(B // A)
else:
    if B < A:
        print(a)
    else:
        print(B // A + 1)

Fixed submission:
A, B = map(int, input().split())
ans = 0
sum = 0

if B == 1:
    print(0)
else:
    ans += 1
    sum += A
    while True:
        if sum >= B:
            print(ans)
            break
        else:
            ans += 1
            sum += A - 1
Buggy python submission (verdict: Runtime Error):
N = int(input())
H = list(map(int, input().split()))

ans = 0
i = 1
now = H[0]
count = 0

while True:
    if now >= H[i]:
        count += 1
        now = H[i]
    else:
        ans = max(count, ans)
        now = H[i]
        count = 0
    
    now = H[i]
    i += 1

    if i >= len(H):
        ans = max(ans, count)
        break

print(ans)


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

def f():
    ans = 0
    i = 1
    now = H[0]
    count = 0

    if N <= 1:
        return 0
    else:
        while True:
            if now >= H[i]:
                count += 1
                now = H[i]
            else:
                ans = max(count, ans)
                now = H[i]
                count = 0
            
            now = H[i]
            i += 1

            if i >= len(H):
                ans = max(ans, count)
                break

        return ans

print(f())
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
ans = 0

for i in range(N):
    ans += i

print(ans)


Fixed submission:
N = int(input())
print(N * (N-1) // 2)
Buggy python submission (verdict: Time Limit Exceeded):
from functools import reduce

n, m = map(int, input().split())
l = [[] for i in range(n+1)]

for i in range(m):
  p, y = map(int, input().split())
  l[p].append([p, y, i])

for i in range(1, len(l)):

  temp = sorted(l[i], key=lambda x: x[1])

  for j in range(len(temp)):
    temp[j] = [str(temp[j][0]).zfill(6), str(j+1).zfill(6), temp[j][2]]
  
  l[i] = temp

ans = reduce(lambda x, y: x + y, l[1:])

for item in sorted(ans, key=lambda x: x[2]):
  print(item[0] + item[1])

Fixed submission:
n, m = map(int, input().split())
l = [[] for i in range(n+1)]

for i in range(m):
  p, y = map(int, input().split())
  l[p].append([p, y, i])

for i in range(1, len(l)):

  temp = sorted(l[i], key=lambda x: x[1])

  for j in range(len(temp)):
    temp[j] = [str(temp[j][0]).zfill(6), str(j+1).zfill(6), temp[j][2]]
  
  l[i] = temp

ans = []

for item in l:
  ans += item

for item in sorted(ans, key=lambda x: x[2]):
  print(item[0] + item[1])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
c = list(map(int, input().split()))
ans = float('Inf')
for i in range(n-k+1):
  temp = c[i:i+k]
  l, r = abs(temp[0]), abs(temp[-1])
  if temp[0] < 0 and temp[-1] > 0:
    ans = min(ans, min(l, r) * 2 + max(l, r))
  else:
    ans = min(ans, max(map(abs, temp)))
print(ans)

Fixed submission:
n, k = map(int, input().split())
c = list(map(int, input().split()))
ans = float('Inf')
for i in range(n-k+1):
  l, r = c[i], c[i+k-1]
  ans = min(abs(l) + abs(r-l), abs(r) + abs(r-l), ans)

print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
w = int(input())
ans = ''
for i in range(0 ,len(s) , w):
  ans += s[i:i+w][0]

print(ans)s = input()
w = int(input())
ans = ''
for i in range(0 ,len(s) , w):
  ans += s[i]

print(ans)

Fixed submission:
s = input()
w = int(input())
ans = ''
for i in range(0 ,len(s) , w):
  ans += s[i:i+w][0]

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
a, b = map(int, input().split())
l = []
t = 0
i = 1
while i <= 999:
  t += i
  l.append(t)
  i += 1

def f():
  for i in range(1, 499500):
    for j in range(len(l)-1):
      if a + i == l[j] and b + i == l[j+1]:
        return i

print(f())

Fixed submission:
a, b = map(int, input().split())
diff = b - a
b_2 = (diff + 1) * diff // 2
print(b_2 - b)
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

n, c = map(int, input().split())
D = [list(map(int, input().split())) for i in range(c)]
C = [list(map(int, input().split())) for i in range(n)]
zero = []
one = []
two = []
ans = float('Inf')
for i in range(n):
  for j in range(n):
    if (i + j) % 3 == 0:
      zero.append(C[i][j])
    elif (i + j) % 3 == 1:
      one.append(C[i][j])
    elif (i + j) % 3 == 2:
      two.append(C[i][j])

combo = list(permutations(range(0,c), 3))


for a, b, c in combo:
  temp = 0
  
  for v in zero:
    temp += D[v-1][a]
  for v in one:
    temp += D[v-1][b]
  for v in two:
    temp += D[v-1][c]

  ans = min(ans, temp)

print(ans)

Fixed submission:
from itertools import permutations

n, c = map(int, input().split())
D = [list(map(int, input().split())) for i in range(c)]
C = [list(map(int, input().split())) for i in range(n)]
oc = []
for i in range(3):
  oc.append([0]*c)
ans = float('Inf')
for i in range(n):
  for j in range(n):
    if (i + j + 2) % 3 == 0:
      oc[0][C[i][j]-1] += 1
    elif (i + j + 2) % 3 == 1:
      oc[1][C[i][j]-1] += 1
    elif (i + j + 2) % 3 == 2:
      oc[2][C[i][j]-1] += 1

combo = list(permutations(range(0,c), 3))
for a, b, c in combo:
  temp = 0
  for i in range(len(oc[0])):
    temp += oc[0][i] * D[i][a]
    temp += oc[1][i] * D[i][b]
    temp += oc[2][i] * D[i][c]
  ans = min(ans, temp)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
n = int(input())
l = sorted(list(map(int, input().split())), reverse=True)
ans = 0
temp = 0
base = l[0]

for i in range(1, n):
  combo = math.factorial(base) / math.factorial(l[i]) / math.factorial(base-l[i])
  if combo > temp:
    temp = combo
    ans = l[i]

print(base, ans)

Fixed submission:
n = int(input())
l = sorted(list(map(int, input().split())))
base = l[-1]
ans = 0
if base % 2 == 0:
  half = base //2
else:
  half = base // 2 + 1

diff = float('Inf')
for i in l:
  if abs(half-i) < diff:
    diff = abs(half-i)
    ans = i

print(base, ans)