Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N = int(input())
L = sorted(list(map(int,input().split())),reverse = True)

ans = 0

for i in range(N-2):
  c = itertools.combinations(L[i+1:],2)
  for j in c:
    if sum(j) > L[i]:
      ans += 1
print(ans)
  
         

Fixed submission:
import bisect
N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0

for i in range(N):
  for j in range(i+1,N):
    c = bisect.bisect_left(L, L[i]+L[j])
    ans += c - j - 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
L,R = map(int,input().split())
mi = 2018
for i in range(R-L):
  for j in range(R-L):
    mi = min(mi,(L+i)*(L+j+1)%2019)
    if mi == 0:
      break
print(mi)

Fixed submission:
L,R = map(int,input().split())
mi = 2018
break_loop = False
for i in range(R-L):
  for j in range(R-L):
    mi = min(mi,(L+i)*(L+j+1)%2019)
    if mi == 0:
       break_loop = True
       break
  if break_loop:
    break
print(mi)
Buggy python submission (verdict: Runtime Error):
S = input()
N = len(S)
ans = 0
for i in range(N):
	for j in range(i, N):
      if all('ACGT'.count(c) == 1 for c in S[i : j + 1]):
        ans = max(ans, j - i + 1)
print(ans)


Fixed submission:
S = input()

A = 0
B = 0
for i in S:
  if i == "A" or i == "C" or i == "G" or i == "T":
    B += 1
  else:
    A = max(A,B)
    B = 0
A = max(A,B)
print(A)
Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
H = list(map(int,input().split()))
 
dp = [0]*N
for i in range(1,N):
	L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
	dp[i] = min(L)
print(dp[-1])


Fixed submission:
N,K = map(int,input().split())
H = [int(x) for x in input().split()]
 
def main(N,K,H):
    dp = [0]*N
    for i in range(1,N):
        L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
        dp[i] = min(L)
    print(dp[-1])
main(N,K,H)

Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
h = list(map(int,input().split()))
 
def main(N,K,H):
    dp = [0]*N
    for i in range(1,N):
        L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
        dp[i] = min(L)
    print(dp[-1])
main(N,K,H)

Fixed submission:
N,K = map(int,input().split())
H = [int(x) for x in input().split()]
 
def main(N,K,H):
    dp = [0]*N
    for i in range(1,N):
        L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
        dp[i] = min(L)
    print(dp[-1])
main(N,K,H)

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

dp = [0]*N
for i in range(1,N):
  L = [dp[j] +abs(h[i] - h[j]) for j in range(max(0,i-K),i)]
  dp[i] = min(L)
print(dp[-1])

Fixed submission:
N,K = map(int,input().split())
H = [int(x) for x in input().split()]
 
def main(N,K,H):
    dp = [0]*N
    for i in range(1,N):
        L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
        dp[i] = min(L)
    print(dp[-1])
main(N,K,H)

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

dp = [0]*N
dp[1] = abs(h[1]-h[0])
for i in range(2,N):
  L = [dp[j] +abs(h[i] - h[j]) for j in range(max(0,i-K),i)]
  dp[i] = min(L)
print(dp[N-1])

Fixed submission:
N,K = map(int,input().split())
H = [int(x) for x in input().split()]
 
def main(N,K,H):
    dp = [0]*N
    for i in range(1,N):
        L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
        dp[i] = min(L)
    print(dp[-1])
main(N,K,H)

Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
h = list(map(int,input().split()))

dp = [0]*N
dp[1] = abs(h[1]-h[0])
for i in range(2,N):
  L = [dp[j] +abs(H[i] - H[j]) for j in range(max(0,i-K),i)]
  dp[i] = min(L)
print(dp[N-1])

Fixed submission:
N,K = map(int,input().split())
H = [int(x) for x in input().split()]
 
def main(N,K,H):
    dp = [0]*N
    for i in range(1,N):
        L = [dp[j]+abs(H[i]-H[j]) for j in range(max(0,i-K),i)]
        dp[i] = min(L)
    print(dp[-1])
main(N,K,H)
Buggy python submission (verdict: Runtime Error):
X = int(input())

if X == 3 or X == 5 or x == 7:
  print("YES")
else:
  print("NO")

Fixed submission:
X = int(input())
 
if X == 3 or X == 5 or X == 7:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
H,W = map(int,input().split())
a = [input() for _ in range(H)]

row = [False]*H
col = [Flase]*W

for i in range(H):
  for j in range(w):
    if a[i][j] == "#":
      row[i] = True
      col[j] = True

for i in range(h):
  if row[i]:
    for j in range(w):
      if col[j]:
        print(a[i][j],end = '')
  print()

Fixed submission:
H,W = map(int,input().split())
a = [input() for _ in range(H)]

row = [False]*H
col = [False]*W

for i in range(H):
  for j in range(W):
    if a[i][j] == "#":
      row[i] = True
      col[j] = True

for i in range(H):
  if row[i]:
    for j in range(W):
      if col[j]:
        print(a[i][j],end = '')
    print()
Buggy python submission (verdict: Runtime Error):
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  exit()
if len(S) != 26:
  L = [0]*26
  for i in range(len(S)):
    L[ord(S[i])-97] = 1
  for i in range(26):
    if L[i] == 0:
      print(S + chr(97+i))
      exit()
else:
  B = []
  for i in reversed(range(26)):
    if ord(S[i]) - ord(S[i-1]) > 1:
      U = i
      M = i-1
      B += [ord(S[i])]
      break
    else:
      B += [ord(S[i])]
  B.sort()
  print(S[:M] + chr(B[0]))

Fixed submission:
S = input()
if S == "zyxwvutsrqponmlkjihgfedcba":
  print(-1)
  exit()
if len(S) != 26:
  L = [0]*26
  for i in range(len(S)):
    L[ord(S[i])-97] = 1
  for i in range(26):
    if L[i] == 0:
      print(S + chr(97+i))
      exit()
else:
  B = []
  for i in reversed(range(26)):
    if ord(S[i]) > ord(S[i-1]):
      U = i
      M = i-1
      B += [ord(S[i])]
      break
    else:
      B += [ord(S[i])]
  B.sort()
  for i in B:
    if chr(i) > S[M]:
      print(S[:M] + chr(i))
      exit()
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = list(map(int,input().split()))

S.sort()

if S[-1] == "Y":
  print("Four")
else:
  print("Three")
         
         

Fixed submission:
N = int(input())
S = input().split()
 
S.sort()
 
if S[-1] == "Y":
  print("Four")
else:
  print("Three")
Buggy python submission (verdict: Runtime Error):
N = int(input())
a = list(map(int,input().split()))

check = [0] * N
ans = 0

for i in range(N):
  if a[i] > 10**5:
    ans += 1
  else:
    check[a[i]-1] += 1

for j in range(N):
  if j+1 > check[j]:
    ans += check[j]
  elif j + 1 < check[j]:
    ans += check[j] -j - 1
print(ans)

Fixed submission:
import collections
N = int(input())
a = list(map(int,input().split()))
ans = 0
c = collections.Counter(a)

key = list(c.keys())


for i in key:
  if i > c[i]:
    ans += c[i]
  elif i < c[i]:
    ans += c[i] - i
print(ans)
Buggy python submission (verdict: Runtime Error):
S = input()
a = S.count("a")
b = S.count("b")
c = S.count("c")

if abs(a-b) <= 1 and abs(a-c) <= 1 and abs(b-c) <= 1:
  print("YES")
else:
  print("NO")

Fixed submission:
S = input()
a = S.count("a")
b = S.count("b")
c = S.count("c")

if abs(a-b) <= 1 and abs(a-c) <= 1 and abs(b-c) <= 1:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
print(1900*M + 100*(N-M))*2**M

Fixed submission:
N,M = map(int,input().split())
a = 1900*M + 100*(N-M)
b = 2**M
print(a*b)
Buggy python submission (verdict: Runtime Error):
N = int(input())
list = [0]*1000000000

for i in range(N):
  A = int(input())
  if list[A-1] == 0:
    list[A-1] = 1
  else:
    list[A-1] = 0
print(sum(list))

Fixed submission:
N = int(input())
A = [int(input()) for i in range(N)]

A.sort()

odd = 0
count = 1
a = A[0]
for i in A[1:]:
  if i == a:
    count += 1
  else:
    a = i
    if count % 2 == 1:
      odd += 1
    count = 1
if count % 2 == 1:
      odd += 1
print(odd)
Buggy python submission (verdict: Runtime Error):
N = int(input())
a = list(map(int,input().split()))

num = set(a)

cnt = [0]*max(num+1)

for i in a:
  cnt[i] += 1

if len(cnt) == 1:
    print(cnt[0])
elif len(cnt) == 2:
    print(sum(cnt))
else:
    for i in range(1, max(a)):
        ans = max(ans, cnt[i-1]+cnt[i]+cnt[i+1])

    print(ans)



Fixed submission:
N = int(input())
a = list(map(int, input().split()))
 
set_a = set(a)
cnt = [0] * (max(a)+1)
ans = 0
 
for i in a:
    cnt[i] += 1
 
if len(cnt) == 1:
    print(cnt[0])
elif len(cnt) == 2:
    print(sum(cnt))
else:
    for i in range(1, max(a)):
        ans = max(ans, cnt[i-1]+cnt[i]+cnt[i+1])

    print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N):
  if p[i] == i+1:
    p[i] = -1
  else:
    p[i] = 0
    
for j in range(N):
  if p[j] == -1:
    ans += 1
    p[j] = 0
    p[j+1] = 0
print(ans)
    

Fixed submission:
N = int(input())
p = list(map(int,input().split()))
ans = 0
for i in range(N):
  if p[i] == i+1:
    p[i] = -1
  else:
    p[i] = 0
    
for j in range(N-1):
  if p[j] == -1:
    ans += 1
    p[j] = 0
    p[j+1] = 0
if p[N-1] == -1:
  ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,(input().split())))
X = sum(a)
A = X-a[0]*2
for i in range(N-1):
  B = X -sum(a[0:i+1])*2
  if abs(B) < abs(A):
    A = B
print(abs(A))

         


Fixed submission:
N = int(input())
A = list(map(int,input().split()))
total = sum(A)
snk = A[0]
ari = total - snk
ans = abs(snk - ari)
for i in range(1,N-1):
  snk += A[i]
  ari -= A[i]
  ans = min(ans,abs(snk - ari))

print(ans)
Buggy python submission (verdict: Runtime Error):
X,A,B = map(int.input().split())

C = B - A

if C <= 0:
  print("delicious")
elif C > X:
  print("dangerous")
else:
  print("safe")

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

C = B - A

if C <= 0:
  print("delicious")
elif C > X:
  print("dangerous")
else:
  print("safe")
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = [int(input()) for _ in range(N)]
ans = 0
for i in range(2**N):
    correct = []
    su = 0
    for j in range(N):
        if ((i >> j) & 1):
            su += s[j]
    if su % 10 == 0:
      su = 0
    ans = max(ans,su)
print(ans)
            


Fixed submission:
N = int(input())
s = [int(input()) for _ in range(N)]
s.sort()
ans = sum(s)

if ans % 10 != 0:
  print(ans)
else:
  for i in range(N):
    if s[i] % 10 != 0:
      print(ans - s[i])
      exit()
  print(0)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
s = [int(input()) for _ in range(N)]
ans = 0
for i in range(2**N):
    correct = []
    su = 0
    for j in range(N):
        if ((i >> j) & 1):
            correct.append(j)
    for k in correct:
      su += s[k]
    if su % 10 == 0:
      su = 0
    ans = max(ans,su)
print(ans)
            

Fixed submission:
N = int(input())
s = [int(input()) for _ in range(N)]
s.sort()
ans = sum(s)

if ans % 10 != 0:
  print(ans)
else:
  for i in range(N):
    if s[i] % 10 != 0:
      print(ans - s[i])
      exit()
  print(0)
Buggy python submission (verdict: Runtime Error):
N,K,X,Y =[int(input()) for _ in range(4)]

if N > K:
  A = X*K + Y*(N-K)
elif:
  A = X*N
print(A)

Fixed submission:
N,K,X,Y =[int(input()) for _ in range(4)]

if N > K:
  A = X*K + Y*(N-K)
else:
  A = X*N
print(A)
Buggy python submission (verdict: Time Limit Exceeded):
a,b = map(int,input().split())

A = 1

for i in range(a,b+1):
  A *= i

if A == 0:
  print("Zero")
elif A > 0:
  print("Positive")
else:
  print("Negative")

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

if a > 0:
  print("Positive")
elif (a <= 0 and b >= 0):
  print("Zero")
else:
  if (b - a) % 2 == 0:
    print("Negative")
  else:
    print("Positive")
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
h = list(map(int, input().split()))

a = [10**10] * n

a[0] = 0
for i in range(0,n):
    for x in range(1,k+1):
        j = i+x
        if j >= n:
            continue
        a[j] = min(a[j], a[i] + abs(h[i] - h[j]))

print(a[n-1])


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

a = [10**10] * n

a[0] = 0
for i in range(0,n):
    for x in range(1,k+1):
        j = i+x
        if j >= n:
            continue
        a[j] = min(a[j], a[i] + abs(h[i] - h[j]))

print(a[n-1])
Buggy python submission (verdict: Time Limit Exceeded):
N, W = map(int, input().split())

w = [0] * N
v = [0] * N

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

INF = 10**13 

V = (10**5)
# dp[n][v] = n番目までで価値vとなる最小の重さ
dp = [INF] * (V + 1)
dp[0] = 0
for i in range(N):
    for j in reversed(range(0, V)):
        if (j - v[i] >= 0):
            dp[j] = min(dp[j], dp[j - v[i]] + w[i])

ans = 0
for j in range(V):
    if dp[j] <= W:
        ans = max(ans, j)

print(ans)


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

w = [0] * N
v = [0] * N

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

INF = 10**13 

V = (10**5)+1
# dp[n][v] = n番目までで価値vとなる最小の重さ
dp = [INF] * V
dp[0] = 0
for i in range(N):
    for j in reversed(range(0, V)):
        if (j - v[i] >= 0):
            dp[j] = min(dp[j], dp[j - v[i]] + w[i])

ans = 0
for j in range(V):
    if dp[j] <= W:
        ans = max(ans, j)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
t = input()
S = (len(s)+1)
T = (len(t)+1)

dp = [[0]*T for i in range(S)]

for i in range(1, S):
   for j in range(1, T):
       dp[i][j] = max(dp[i-1][j], dp[i][j-1])
       if s[i-1] == t[j-1]:
           dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1)

# print(dp[len(s)][len(t)])

ans = ""
i = len(s)
j = len(t)
while i > 0 and j > 0:
    if dp[i][j] == dp[i-1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j-1]:
        j -= 1
    else:
        ans += s[i-1]
        i -= 1
        j -= 1
    
print(ans[::-1])


Fixed submission:
s = input()
t = input()
S = (len(s)+1)
T = (len(t)+1)

dp = [[0]*T for i in range(S)]

for i in range(1, S):
   for j in range(1, T):
       dp[i][j] = max(dp[i-1][j], dp[i][j-1])
       if s[i-1] == t[j-1]:
           dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1)

# print(dp[len(s)][len(t)])

ans = ""
i = len(s)
j = len(t)
while i > 0 and j > 0:
    if dp[i][j] == dp[i-1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j-1]:
        j -= 1
    else:
        ans += s[i-1]
        i -= 1
        j -= 1
    
print(ans[::-1])
Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
read = sys.stdin.buffer.read


N, M, K = map(int, input().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)

left = 0
for i, b in enumerate(B):
    if b > K:
        left = i - 1
        break

ans = 0
if A[-1] + B[-1] <= K:
    ans = N + M
else:
    for i in range(len(A)):
        if A[i] > K:
            break

        while A[i] + B[left] > K:
            left -= 1

        ans = max(ans, left + i)

print(ans)

Fixed submission:
import numpy as np
import sys
read = sys.stdin.read


N, M, K = map(int, input().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)

ans = 0
left = M
for i in range(len(A)):
    if A[i] > K:
        break

    while A[i] + B[left] > K:
        left -= 1

    ans = max(ans, left + i)

print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
read = sys.stdin.buffer.read


N, M, K = map(int, input().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)

left = M
for i, b in enumerate(B):
    if b > K:
        left = i

ans = 0
for i in range(len(A)):
    if A[i] > K:
        break

    while A[i] + B[left] > K:
        left -= 1

    ans = max(ans, left + i)

print(ans)

Fixed submission:
import numpy as np
import sys
read = sys.stdin.read


N, M, K = map(int, input().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)

ans = 0
left = M
for i in range(len(A)):
    if A[i] > K:
        break

    while A[i] + B[left] > K:
        left -= 1

    ans = max(ans, left + i)

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
readline = sys.stdin.buffer.readline

from collections import Counter


N = int(input())
A = list(map(int, readline().split()))
Q = int(input())

S = sum(A)
C = Counter(A)

ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    count = C[b]
    S += (c - b) * count
    C[b], C[c] = 0, C[c] + count
    ans.append(S)

print('\n'.join(map(str, ans)))

Fixed submission:
#import numpy as np
from collections import Counter
from time import time

import sys
readline = sys.stdin.buffer.readline


N = int(input())
A = list(map(int, input().split()))
#A = np.random.randint(1, 10 ** 5 + 1, 10 ** 5)

S = sum(A)
C = Counter(A)

Q = int(input())
#Q = 10 ** 5

ans = []
s = time()
for _ in range(Q):
    b, c = map(int, input().split())
    #b, c = np.random.randint(1, 10 ** 5 + 1, 2)
    #b, c = 2, 5

    count = C[b]
    S = S + (c - b) * count

    C[c] += count
    C[b] = 0
    
    ans.append(S)

print('\n'.join(map(str, ans)))
#print('time: ', time() - s)
Buggy python submission (verdict: Runtime Error):
X = list(int, input().split())

for i, x in enumerate(X):
    if x == 0:
        print(i + 1)
        break

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

for i, x in enumerate(X):
    if x == 0:
        print(i + 1)
        break
Buggy python submission (verdict: Runtime Error):
import sys
readline = sys.stdin.readline
 
import numpy as np
from collections import Counter
 

N = int(input())
A = np.array(readline().split())
count = Counter(A)
A = np.array([
    a for a, c in count.items() if c == 1
])
 
if len(A) != 0:
    A = np.sort(A)

i = 0
while True:
    if len(A) == 0:
        break
    pre = A
    a = A[i]
    A = np.delete(A, np.where((A % a == 0) & (A != a)))
    i += 1
    if i >= len(A):
        break

print(len(A))

Fixed submission:
import sys
readline = sys.stdin.readline

import numpy as np
from collections import Counter
from numba import njit




N = int(input())
A = np.array(readline().split(), np.int32)

count = np.zeros((A.max() + 1))
for a in A:
    if count[a] > 1:
        continue
    count[::a] += 1

ret = 0
for a in A:
    ret += count[a] == 1

print(ret)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))

p = 10 ** 18

ans = 1
for a in A:
    ans *= a

if ans > p:
    ans = -1

print(ans)

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

p = 10 ** 18

ans = 1

if 0 in A:
    ans = 0
else:
    for a in A:
        ans = ans * a
        if ans > p:
            ans = -1
            break

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline


def main():
    X = int(input().rstrip())
    for b in range(64):
        if (X - b ** 5) ** (1 / 5) % 1 == 0:
            A = int((X - b ** 5) ** (1 / 5))
            B = b
            break

    print(A, B)


if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
 
 
def main():
    X = int(input().rstrip())
    f = 0
    for a in range(-150, 150):
        for b in range(-150, 150):
            i = a ** 5 - b ** 5
            if i == X:
                print(a, b)
                f = 1
                break
 
        if f:
            break
 
 
if __name__ == '__main__':
    main()

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


def main():
    X = int(input().rstrip())
    B = -1
    for i in range(100):
        if X - 1 == i ** 5:
            A = i
            break

    print(A, B)


if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
 
 
def main():
    X = int(input().rstrip())
    f = 0
    for a in range(-150, 150):
        for b in range(-150, 150):
            i = a ** 5 - b ** 5
            if i == X:
                print(a, b)
                f = 1
                break
 
        if f:
            break
 
 
if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
import sys
imput = sys.stdin.readline

import numpy as np

def main():
    N, M, Q = map(int, input().split())
    I = [list(map(int, input().split())) for _ in range(Q)]
    A = np.array([M] * N)
    ans = []
    
    j = 1
    while True:
        if A[-1] == 1:
            break
        
        tmp = 0
        for i in I:
            a, b, c, d = i
            tmp += d if A[b-1] - A[a-1] == c else 0

        ans.append(tmp)

        if A[0] == 1:
            n = A[j]
            A[:j+1] = n - 1
            if n - 1 == 1:
                j += 1
        else:
            A[0] -= 1
            
    print(max(ans))    


if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline

from itertools import combinations_with_replacement as cwr

def main():
    N, M, Q = map(int, input().split())
    I = [list(map(int, input().split())) for _ in range(Q)]
    J = cwr(range(1, M+1), N)
    ans = []

    for j in J:
        tmp = 0
        for i in I:
            a, b, c, d = i
            tmp += d if j[b-1] - j[a-1] == c else 0
        ans.append(tmp)

    print(max(ans))

    
if __name__  == '__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
 
    
S = [int(s) for s in input()]
N = len(S)
y = 2019
T = [S[-1]] * N
U = [0] * y
U[0] += 1

for i in range(N-2, -1, -1):
    t = (T[i+1] + S[i] * 10 ** (N-1-i)) % y
    T[i] = t
    U[t] += 1


cnt = 0
for u in U:
    cnt += u * (u - 1) // 2

print(cnt)

Fixed submission:
from queue import deque
 
    
S = deque(input())
y = 2019
U = [0] * y
t = 0
U[0] = 1
k = 1

for s in reversed(S):
    t = t + int(s) * k
    t %= y
    U[t] += 1
    k *= 10
    k %= y

cnt = 0
for u in U:
    cnt = cnt + u * (u - 1) // 2

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
 
    
S = [int(s) for s in input()]
N = len(S)
y = 2019
T = [S[-1]] * N
U = [0] * y
U[0] += 1

for i in range(N-2, -1, -1):
    t = (T[i+1] + S[i] * 10 ** (N-1-i)) % y
    T[i] = t
    U[t] += 1


cnt = 0
for u in U:
    cnt += u * (u - 1) // 2

print(cnt)

Fixed submission:
from queue import deque
 
    
S = deque(input())
y = 2019
U = [0] * y
t = 0
U[0] = 1
k = 1

for s in reversed(S):
    t = t + int(s) * k
    t %= y
    U[t] += 1
    k *= 10
    k %= y

cnt = 0
for u in U:
    cnt = cnt + u * (u - 1) // 2

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from queue import deque


y = 2019
S = deque(input())
N = len(S)
T = [0] * (N + 1)

for i in range(N-1, -1, -1):
    T[i] = T[i+1] + int(S[i]) * 10 ** (N - 1 - i)
    
T = [t % y for t in T]

cnt = 0
for c in Counter(T).values():
    cnt += c * (c - 1) // 2
    
print(cnt)

Fixed submission:
from queue import deque
 
    
S = deque(input())
y = 2019
U = [0] * y
t = 0
U[0] = 1
k = 1

for s in reversed(S):
    t = t + int(s) * k
    t %= y
    U[t] += 1
    k *= 10
    k %= y

cnt = 0
for u in U:
    cnt = cnt + u * (u - 1) // 2

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter


y = 2019
S = [s for s in input()]
N = len(S)
T = [0] * N

for i in range(N):
    T[i] = int(''.join(S[i:N])) % y

cnt = 0
for t, c in Counter(T).items():
    if t == 0:
        cnt += 1
        continue
    cnt += c * (c - 1) // 2
    
print(cnt)

Fixed submission:
from queue import deque
 
    
S = deque(input())
y = 2019
U = [0] * y
t = 0
U[0] = 1
k = 1

for s in reversed(S):
    t = t + int(s) * k
    t %= y
    U[t] += 1
    k *= 10
    k %= y

cnt = 0
for u in U:
    cnt = cnt + u * (u - 1) // 2

print(cnt)