Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from collections import deque
x0,y0,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort()
q.sort()
r.sort()
p = deque(p)
q = deque(q)
r = deque(r)
x,y = 0,0
red = p.pop()
green = q.pop()
mu = r.pop()
ans = 0

def newmu():
  if r:
    return r.pop()
  return 0


while x < x0 or y < y0:
  if x < x0 and y < y0:
    if red >= mu and green >= mu:
      ans += red + green
      red = p.pop()
      green = q.pop()
    elif red >= mu:
      ans += red + mu
      red = p.pop()
      mu = newmu()
    elif green >= mu:
      ans += green + mu
      green = q.pop()
      mu = newmu()
    elif r:
      r2 = r.pop()
      if red >= green and red >= r2:
        ans += mu + red
        mu = r2
        red = p.pop()
      elif green >= red and green >= r2:
        ans += mu + green
        mu = r2
        green = q.poop()
      else:
        ans += mu + r2
        mu = newmu()
    elif red >= green:
      ans += mu + red
      red = p.pop()
      newmu()
    else:
      ans += mu + green
      green = q.pop()
      newmu()
    x += 1
    y += 1
  elif x < x0:
    if red >= mu:
      ans += red
      red = p.pop()
    else:
      ans += mu
      newmu()
    x += 1
  else:
    if green >= mu:
      ans += green
      green = q.pop()
    else:
      ans += mu
      newmu()
    y += 1
print(ans)

Fixed submission:
from collections import deque
x,y,a,b,c = map(int,input().split())
p = list(map(int,input().split()))
q = list(map(int,input().split()))
r = list(map(int,input().split()))
p.sort(reverse=True)
q.sort(reverse=True)
l = p[:x] + q[:y] + r
l.sort(reverse=True)
print(sum(l[:(x+y)]))
Buggy python submission (verdict: Runtime Error):
h,w = map(int,input().split())
if (h*w)%2 == 0:
  print((h*w)//2)
else:
  print((h*w)//2+1)


Fixed submission:
print([1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51][int(input())-1])
Buggy python submission (verdict: Runtime Error):
s = str(input())
n = len(s)
dp = [[0]*2 for _ in range(n+1)]
dp[0][1] = 1
for i in range(n):
    p = int(s[i])
    dp[i+1][0] = min(dp[i][1]+10-p,dp[i][0]+p)
    dp[i+1][1] = min(dp[i][1]+10-p-1,dp[i][0]+p+1)
print(dp[-1][0])

Fixed submission:
a,b,c = map(int,input().split())
if (a==b) + (b==c) + (c==a) ==1:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
n= int(input())
a = list(map(int,input().split()))
for i in range(n-1):
  for j in range(i+1,n):
    if a[i]==a[j]:
      print("NO")
      quit()
print("YES")

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
check = set(a)
if len(a) == len(check):
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = list(map(int,input().split()))
from fractions import gcd
def lcm(x,y):
  return x*y // gcd(x, y)
q = a[0]//2
for i in range(n):
  a[i] = a[i]//2
  q = lcm(q,a[i])

cnt = 0
flag = 1
while flag:
  for i in range(n):
    if a[i] % 2 == 1:
      cnt += 1
    else:
      a[i]//1
    if cnt != 0:
      flag = 0
ans = (m//q) - (m//(2*q))
if cnt == n:
  print(ans)
else:
  print(0)

Fixed submission:
from math import gcd
def lcm(x,y):
    return x*y//gcd(x,y)
def cnt(x):
    i = 0
    while x%2 == 0:
        x = x//2
        i += 1
    return i 
n,m = map(int,input().split())
a = list(map(int,input().split()))
memo = cnt(a[0])
for i in range(n):
    if cnt(a[i]) != memo:
        print(0)
        quit()
    a[i] = a[i]//2

t = 1
for i in range(n):
    t = lcm(t,a[i])
print(m//t-m//(2*t))

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a = list(map(int,input().split()))
def gcd(a, b):
  while b:
    a, b = b, a % b
  return a
def lcm(a, b):
  return a * b // gcd(a, b)
q = a[0]//2
for i in range(n):
  a = a[i]//2
  q = lcm(q,a)
cnt = 0
while True:
  for i in range(n):
    if a[i] % 2 == 0:
      cnt += 1
    else:
      a[i]//1
    if cnt:
      break
ans = (m//q) - (m//(2*q))
if cnt == n:
  print(ans)
else:
  print(0)

Fixed submission:
from math import gcd
def lcm(x,y):
    return x*y//gcd(x,y)
def cnt(x):
    i = 0
    while x%2 == 0:
        x = x//2
        i += 1
    return i 
n,m = map(int,input().split())
a = list(map(int,input().split()))
memo = cnt(a[0])
for i in range(n):
    if cnt(a[i]) != memo:
        print(0)
        quit()
    a[i] = a[i]//2

t = 1
for i in range(n):
    t = lcm(t,a[i])
print(m//t-m//(2*t))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int,input().split()))
mod = 10**9+7
m = len(bin(max(A)))-2
zero = [0]*m
one = [0]*m
for a in A:
  for i in range(m):
    j = (a>>i)&1
    zero[i] += 1-j
    one[i] += j
ans = 0
d = [0]*m
d[0] = 1
for i in range(1,m):
  d[i] = (2*d[i-1])%mod
for i in range(m):
  ans = (ans+(zero[i]%mod)*(one[i]%mod)*d[i])%mod
print(ans)

Fixed submission:
import numpy as np
n = int(input())
A = np.array(list(map(int,input().split())))
mod = 10**9+7
m = len(bin(max(A)))-2
ans = 0

for i in range(m):
  one = np.count_nonzero((A >> i)&1)
  zero = n - one
  ans += (pow(2,i,mod)*zero*one)%mod
print(ans%mod)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int,input().split()))
mod = 10**9+7
m = len(bin(max(A)))-2
zero = [0]*m
one = [0]*m
for a in A:
  for i in range(m):
    zero[i] += 1-(a>>i)&1
    one[i] += (a>>i)&1
ans = 0
for i in range(m):
  ans = (ans+zero[i]*one[i]*2**i)%mod
print(ans)

Fixed submission:
import numpy as np
n = int(input())
A = np.array(list(map(int,input().split())))
mod = 10**9+7
m = len(bin(max(A)))-2
ans = 0

for i in range(m):
  one = np.count_nonzero((A >> i)&1)
  zero = n - one
  ans += (pow(2,i,mod)*zero*one)%mod
print(ans%mod)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
ans = 0
for i in range(10):
  for j in range(10):
    for k in range(10):
      lst = [i,j,k]
      flag = 0
      for l in range(n):
        if int(s[l]) == lst[flag]:
          flag += 1
          if flag == 3:
            ans += 1
            break
print(ans)

Fixed submission:
n = int(input())
s = input()
ans = 0
for i in range(1000):
  i = str(i).zfill(3)
  flag = 0
  for l in range(n):
    if s[l] == i[flag]:
      flag += 1
      if flag == 3:
        ans += 1
        break
print(ans)
Buggy python submission (verdict: Runtime Error):
a,b = map(int,input().split())
print(max(0,int(a-2b)))

Fixed submission:
a,b = map(int,input().split())
print(max(0,int(a-2*b)))
Buggy python submission (verdict: Runtime Error):
def warshall_floyd(g):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                g[i][j] = min(g[i][j], g[i][k] + g[k][j])

n = int(input())
s = [[int(i) for i in list(input())] for _ in range(n)]
graph = [[float("inf")]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if s[i][j] == 1:
            graph[i][j] = 1
        if i == j:
            graph[i][j] = 0

warshall_floyd(graph)
d = 0
#print(graph)
for i in range(n-1):
    for j in range(i+1,n):
        if graph[i][j] != float("inf"):
            if d < graph[i][j]:
                start = i
                end = j
                d = graph[i][j]

v = [[] for _ in range(d+1)]
for i in range(n):
    l = graph[start]
    v[l[i]].append(i)
#print(v)
#print(start,end)

for l in v:
    m = len(l)
    for i in range(m-1):
        for j in range(i+1,m):
            if s[l[i]][l[j]] == 1:
                print(-1)
                quit()
print(d+1)

Fixed submission:
s = input()
k = int(input())
n = len(s)
a = []
cnt = 1
for i in range(n-1):
  if s[i] == s[i+1]:
    cnt += 1
  else:
    a.append(cnt)
    cnt = 1
if sum(a) != n:
  a.append(cnt)
m = len(a)
if m == 1:
  print((a[0]*k)//2)
  exit()
if m == 2:
  print((a[0]//2+a[1]//2)*k)
  exit()
cnt = 0
for i in range(1,m-1):
  cnt += a[i] // 2
cnt *= k
if s[0] == s[n-1]:
  cnt += (k-1)*((a[0]+a[m-1])//2)
else:
  cnt += (k-1)*(a[0]//2+a[m-1]//2)
cnt += a[0]//2 + a[m-1]//2
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
def warshall_floyd(g):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                g[i][j] = min(g[i][j], g[i][k] + g[k][j])

n = int(input())
s = [[int(i) for i in list(input())] for _ in range(n)]
graph = [[float("inf")]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if s[i][j] == 1:
            graph[i][j] = 1
        if i == j:
            graph[i][j] = 0

warshall_floyd(graph)
d = 0
#print(graph)
for i in range(n-1):
    for j in range(i+1,n):
        if graph[i][j] != float("inf"):
            if d < graph[i][j]:
                start = i
                end = j
                d = graph[i][j]

v = [[] for _ in range(d+1)]
for i in range(n):
    l = graph[start]
    v[l[i]].append(i)
#print(v)
#print(start,end)

for l in v:
    m = len(l)
    for i in range(m-1):
        for j in range(i+1,m):
            if s[l[i]][l[j]] == 1:
                print(-1)
                quit()
print(d+1)

Fixed submission:
def warshall_floyd(g):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                g[i][j] = min(g[i][j], g[i][k] + g[k][j])

n = int(input())
s = [[int(i) for i in list(input())] for _ in range(n)]
graph = [[float("inf")]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        if s[i][j] == 1:
            graph[i][j] = 1
        if i == j:
            graph[i][j] = 0

warshall_floyd(graph)
d = 0
#print(graph)
for i in range(n-1):
    for j in range(i+1,n):
        if graph[i][j] != float("inf"):
            if d < graph[i][j]:
                start = i
                end = j
                d = graph[i][j]

v = [[] for _ in range(d+1)]
for i in range(n):
    l = graph[start]
    v[l[i]].append(i)
#print(v)
#print(start,end)

for l in v:
    m = len(l)
    for i in range(m-1):
        for j in range(i+1,m):
            if s[l[i]][l[j]] == 1:
                print(-1)
                quit()
print(d+1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = str(input())
ans = 0
cnt = 0
for i in range(n-1):
  start = i
  for j in range(i+1,n):
    if s[start] == s[j]:
      cnt += 1
      start += 1
      if start + cnt - 1 == j:
        ans = max(ans,cnt)
        cnt = 0
        start = i
    else:
      ans = max(ans,cnt)
      cnt = 0
      start = i
print(ans)

Fixed submission:
n,s = int(input()), list(input())
def z_algo(s):
    n = len(s)
    a = [0]*n
    i,j,l = 1,0,n
    a[0] = n
    while i < l:
        while i+j < l and s[j] == s[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        a[i] = j
        k = 1
        while l-i > k < j-a[k]:
            a[i+k] = a[k]
            k += 1
        i += k
        j -= k
    return a

ans = 0
for i in range(n-1):
    a = z_algo(s[i:])
    len_a = len(a)
    for j in range(len_a):
        ans = max(ans, min(j,a[j]))
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)
n,q = map(int,input().split())
tree = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
ans = [0]*n
for i in range(q):
    p,x = map(int,input().split())
    ans[p-1] += x
done = [0]*n
def dfs(now,m):
    done[now] = 1
    ans[now] += m
    for i in tree[now]:
        if not done[i]:
            dfs(i,ans[now])
dfs(0,0)
print(*ans)

Fixed submission:
if int(input()) < 3200:
  print("red")
else:
  print(input())
Buggy python submission (verdict: Runtime Error):
n = int(input())
l = ["".join(sorted(input())) for _ in range(n)]
nCr = {}
def cmb(n, r):
  if r == 0 or r == n: return 1
  if r == 1: return n
  if (n,r) in nCr: return nCr[(n,r)]
  nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
  return nCr[(n,r)]

d = {}
for i in range(n):
  if l[i] not in d:
    d[l[i]] = 1
  else:
    d[l[i]] += 1
#print(d)
    
ans = 0
for a in d.values():
  #print(a)
  if a > 1:
    ans += cmb(a,2)
print(ans)

Fixed submission:
n = int(input())
l = ["".join(sorted(input())) for _ in range(n)]
ans = 0
d = {}
for i in range(n):
  if l[i] not in d:
    d[l[i]] = 1
  else:
    ans += d[l[i]]
    d[l[i]] += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
l = ["".join(sorted(input())) for _ in range(n)]
nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

d = {}
for i in range(n):
  if l[i] not in d:
    d[l[i]] = 1
  else:
    d[l[i]] += 1

ans = 0
for a in d.values():
  if a > 1:
    ans += cmb(a,2)
print(ans)

Fixed submission:
n = int(input())
l = ["".join(sorted(input())) for _ in range(n)]
ans = 0
d = {}
for i in range(n):
  if l[i] not in d:
    d[l[i]] = 1
  else:
    ans += d[l[i]]
    d[l[i]] += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n,m = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(n)]
ab = sorted(ab, key=lambda x:(x[0], x[1])) + [[0,0]]
lst = []
now = 0
ans = 0
heapq.heapify(lst)
for i in range(1,m+1):
  while ab[now][0] == i:
    heapq.heappush(lst,-ab[now][1])
    now += 1
  heapq.heapify(lst)
  if lst:
    ans -= heapq.heappop(lst)
print(ans)

Fixed submission:
import heapq
n,m = map(int,input().split())
ab = [[] for _ in range(m+1)]
for i in range(n):
  a,b = map(int,input().split())
  if a < m+1:
    ab[a].append(b)
lst = []
now = 0
ans = 0
heapq.heapify(lst)
for i in range(1,m+1):
  for j in ab[i]:
    heapq.heappush(lst,-j)
  #heapq.heapify(lst)
  if lst:
    ans -= heapq.heappop(lst)
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int,input().split()))
lowest = h[i]
for i in range(1,n):
  if h[i] < lowest - 1:
    print("No")
    quit()
  else:
    lowest = max(lowest,h[i]-1)
print("Yes")

Fixed submission:
n = int(input())
h = list(map(int,input().split()))
lowest = h[0]-1
for i in range(1,n):
  if h[i] < lowest:
    print("No")
    quit()
  else:
    lowest = max(lowest,h[i]-1)
print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
import copy
import numpy as np
s = list(str(input()))
mod = 10**9 + 7
a = []
n = len(s)
for i in range(13):
  r = [0,0,0,0,0,0,0,0,0,0,0,0,0]
  for j in range(10):
    r[(i*10+j)%13] = 1
  a.append(copy.deepcopy(r))
a = np.array(a)
if s[0] == "?":
  start = np.array([1,1,1,1,1,1,1,1,1,1,0,0,0])
else:
  start = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0])
  start[int(s[0])] = 1
for i in range(1,n):
  if s[i] == "?":
    start = np.dot(start,a)
    for j in range(13):
      start[j] = start[j] % mod
  else:
    nxt = [0]*13
    for j in range(13):
      nxt[(j*10+int(s[i]))%13] += int(start[j])
    start = copy.deepcopy(np.array(nxt))
print(start[5])

Fixed submission:
s = str(input())
mod = 10**9 + 7
n = len(s)

if s[0] == "?":
  ans = [1,1,1,1,1,1,1,1,1,1,0,0,0]
else:
  ans = [0,0,0,0,0,0,0,0,0,0,0,0,0]
  ans[int(s[0])] = 1


for i in s[1:]:
  dp = [0]*13
  for j in range(13):
    dp[j*10%13] = ans[j] % mod
  dp += dp
  if i == "?":
    for j in range(13):
      ans[j] = sum(dp[j+4:j+14])
  else:
    for j in range(13):
      ans[j] = dp[j+13-int(i)]
print(ans[5]%mod)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
n,m = map(int,input().split())
graph = [[[] for i in range(3)] for j in range(n+1)]
for i in range(m):
  u,v = map(int,input().split())
  graph[u][0].append(v)
s,t = map(int,input().split())
for i in range(1,3):
  for j in range(1,n+1):
    l = set([])
    for k in graph[j][0]:
      l = l | set(graph[k][i-1])
    graph[j][i] = list(l)
for i in range(n+1):
  graph[i] = graph[i][2]
cnt = 0
done = [0]*(n+1)
done[s] = 1
wait = deque([s])
while wait:
  cnt += 1
  q = wait
  wait = deque([])
  for i in q:
    for j in graph[i]:
      if not done[j]:
        wait.append(j)
        done[j] = 1
      if j == t:
        print(cnt)
        quit()
print(-1)

Fixed submission:
import copy
n,m = map(int,input().split())
graph = [[] for _ in range(n*3)]
for i in range(m):
  u,v = map(int,input().split())
  for j in range(3):
    graph[n*j+u-1].append((n*(j+1)+v-1)%(3*n))
s,t = map(int,input().split())
wait = [s-1]
cnt = 0
done = [0]*(3*n)
while wait:
  cnt += 1
  now = copy.copy(wait)
  wait = []
  for i in now:
    for j in graph[i]:
      if not done[j]:
        done[j] = 1
        wait.append(j)
      if j == t-1:
        print(cnt//3)
        quit()
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import copy
n,m = map(int,input().split())
graph = [[[] for i in range(3)] for j in range(n+1)]
for i in range(m):
  u,v = map(int,input().split())
  graph[u][0].append(v)
s,t = map(int,input().split())
for i in range(1,3):
  for j in range(1,n+1):
    l = set([])
    for k in graph[j][0]:
      l = l | set(graph[k][i-1])
    graph[j][i] = list(l)
for i in range(n+1):
  graph[i] = graph[i][2]
cnt = 0
done = [0]*(n+1)
done[s] = 1
wait = deque([s])
while wait:
  cnt += 1
  q = copy.copy(wait)
  wait = deque([])
  for i in q:
    for j in graph[i]:
      if not done[j]:
        wait.append(j)
        done[j] = 1
      if j == t:
        print(cnt)
        quit()
print(-1)

Fixed submission:
import copy
n,m = map(int,input().split())
graph = [[] for _ in range(n*3)]
for i in range(m):
  u,v = map(int,input().split())
  for j in range(3):
    graph[n*j+u-1].append((n*(j+1)+v-1)%(3*n))
s,t = map(int,input().split())
wait = [s-1]
cnt = 0
done = [0]*(3*n)
while wait:
  cnt += 1
  now = copy.copy(wait)
  wait = []
  for i in now:
    for j in graph[i]:
      if not done[j]:
        done[j] = 1
        wait.append(j)
      if j == t-1:
        print(cnt//3)
        quit()
print(-1)
Buggy python submission (verdict: Runtime Error):
w,h,x,y = map(int,input().split())
if x == w/2 and y = h/2:
  flag = 1
else:
  flag = 0
print(h*w/2,flag)

Fixed submission:
w,h,x,y = map(int,input().split())
if x == w/2 and y == h/2:
  flag = 1
else:
  flag = 0
print(h*w/2,flag)
Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
mod = 10**9+7
a = [int(input()) for _ in range(m)]
s = [0]*(n+1)
s[0],s[1]=1,1
for i in range(2,n+1):
  s[i] = (s[i-1]+s[i-2])%mod
ans = s[a[0]-1]
for i in range(1,m):
  if a[i]-a[i-1] == 1:
    print(0)
    quit()
  ans = (ans*s[a[i]-a[i-1]-2])%mod
print((ans*s[n-a[m-1]-1])%mod)

Fixed submission:
n,m = map(int,input().split())
mod = 10**9+7
a = [int(input()) for _ in range(m)]
s = [0]*(n+1)
s[0],s[1]=1,1
for i in range(2,n+1):
  s[i] = (s[i-1]+s[i-2])%mod
if m == 0:
  print(s[n])
  quit()
ans = s[a[0]-1]
for i in range(1,m):
  if a[i]-a[i-1] == 1:
    print(0)
    quit()
  ans = (ans*s[a[i]-a[i-1]-2])%mod
print((ans*s[n-a[m-1]-1])%mod)
Buggy python submission (verdict: Time Limit Exceeded):
h,w = map(int,input().split())
s = [[(l == '.')*1 for l in list(input())] for _ in range(h)]
U = [[0]*w for _ in range(h)]
D = [[0]*w for _ in range(h)]
L = [[0]*w for _ in range(h)]
R = [[0]*w for _ in range(h)]
ans = 0
for i in range(h):
  for j in range(w):
    if i == 0:
      U[i][j] = s[i][j]
      D[h-i-1][j] = s[h-i-1][j]
    else:
      U[i][j] = (U[i-1][j]+1)*s[i][j]
      D[h-i-1][j] = (D[h-i][j]+1)*s[h-i-1][j]
    if j == 0:
      L[i][j] = s[i][j]
      R[i][w-j-1] = s[i][w-j-1]
    else:
      L[i][j] = (L[i][j-1]+1)*s[i][j]
      R[i][w-j-1] = (R[i][w-j]+1)*s[i][w-j-1]
for i in range(h):
  for j in range(w):
    m = L[i][j]+R[i][j]+U[i][j]+D[i][j]-3
    if ans < m:
      ans = m
print(ans)

Fixed submission:
h,w = map(int,input().split())
s = [[(l == '.')*1 for l in list(input())] for _ in range(h)]
U = [[0]*w for _ in range(h)]
D = [[0]*w for _ in range(h)]
L = [[0]*w for _ in range(h)]
R = [[0]*w for _ in range(h)]
ans = 0
for i in range(h):
  for j in range(w):
    if i == 0:
      U[i][j] = s[i][j]
      D[h-i-1][j] = s[h-i-1][j]
    else:
      U[i][j] = (U[i-1][j]+1)*s[i][j]
      D[h-i-1][j] = (D[h-i][j]+1)*s[h-i-1][j]
    if j == 0:
      L[i][j] = s[i][j]
      R[i][w-j-1] = s[i][w-j-1]
    else:
      L[i][j] = (L[i][j-1]+1)*s[i][j]
      R[i][w-j-1] = (R[i][w-j]+1)*s[i][w-j-1]
for i in range(h):
  for j in range(w):
    m = L[i][j]+R[i][j]+U[i][j]+D[i][j]-3
    if ans < m:
      ans = m
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n,q = map(int,input().split())
s = input()
query = [list(map(str,input().split())) for _ in range(q)]
def sim(x):
    now = s[x]
    for i in range(q):
        t,d = query[i][0],query[i][1]
        if now != t:
            continue
        if d == "L":
            x -= 1
            if x == -1:
                return -1
        else:
            x += 1
            if x == n:
                return 1
        now = s[x]
    return 0

max_left = -1
min_right = n
min_safe = n
max_safe = -1
save = (max_left, min_right)
flag = 0
while (min_right-max_left) > 1:
    m = (max_left+min_right)//2
    j = sim(m)
    if j == -1:
        max_left = m
    if j == 0:
        max_safe = max(max_safe, m)
        min_safe = min(min_safe, m)
    if j == 1:
        min_right = m
    
    if (max_left, min_right) != save:
        save = (max_left, min_right)
    else:
        #left
        #print(max_left, min_safe, max_safe, min_right)
        l,r = max_left, min_safe
        while (r-l) > 1:
            m = (r+l)//2
            j = sim(m)
            if j == -1:
                l = m
            elif j == 0:
                r = m
        max_left, min_safe = l,r
        #right
        l,r = max_safe, min_right
        while (r-l) > 1:
            m = (r+l)//2
            j = sim(m)
            if j == 0:
                l = m
            elif j == 1:
                r = m
        max_safe, min_right = l,r

        flag = 1
        break
if flag:
    print(max_safe-min_safe+1)
else:
    print(0)

Fixed submission:
n,q = map(int,input().split())
s = input()
query = [list(map(str,input().split())) for _ in range(q)]
def sim(x):
    now = s[x]
    for i in range(q):
        t,d = query[i][0],query[i][1]
        if now != t:
            continue
        if d == "L":
            x -= 1
            if x == -1:
                return -1
        else:
            x += 1
            if x == n:
                return 1
        now = s[x]
    return 0

max_left = -1
min_right = n
min_safe = n
max_safe = -1
save = (max_left, min_right)
flag = 0
while (min_right-max_left) > 1:
    m = (max_left+min_right)//2
    j = sim(m)
    if j == -1:
        max_left = m
    if j == 0:
        max_safe = max(max_safe, m)
        min_safe = min(min_safe, m)
    if j == 1:
        min_right = m
    
    if (max_left, min_right) != save:
        save = (max_left, min_right)
    else:
        #left
        #print(max_left, min_safe, max_safe, min_right)
        l,r = max_left, min_safe
        while (r-l) > 1:
            m = (r+l)//2
            j = sim(m)
            if j == -1:
                l = m
            elif j == 0:
                r = m
        max_left, min_safe = l,r
        #right
        l,r = max_safe, min_right
        while (r-l) > 1:
            m = (r+l)//2
            j = sim(m)
            if j == 0:
                l = m
            elif j == 1:
                r = m
        max_safe, min_right = l,r

        flag = 1
        break
if flag:
    print(max_safe-min_safe+1)
else:
    print(0)
Buggy python submission (verdict: Runtime Error):
import bisect
a,b,q = map(int,input().split())
s = [-10**11]+[int(input()) for _ in range(a)]+[10**11]
t = [-10**11]+[int(input()) for _ in range(b)]+[10**11]
for i in range(q):
  ans = 10**11
  x = int(input())
  l = bisect.bisect(s,x)
  m = bisect.bisect(t,x)
  arr = [[s[l-1],s[l]],[t[m-1],t[m]]]
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += (now-arr[j][i >> j & 1])*(-1)**(((now-arr[j][i >> j & 1])<0))
      now = arr[j][i >> j & 1]
    if ans > S:
      ans = S
  for i in range(4):
    S = 0
    now = x
    for j in range(2):
      S += (now-arr[1-j][i >> j & 1])*((-1)**((now-arr[1-j][i >> j & 1])<0))
      now = arr[1-j][i >> j & 1]
    if ans > S:
      ans = S
  print(ans)

Fixed submission:
if int(input()[5:7]) >= 5:
  print("TBD")
else:
  print("Heisei")