Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
dp = [[0]*(N+1) for i in range(N+1)]
ans = 0
for i in range(N-1,-1,-1):
  for j in range(i,-1,-1):
    if S[i] == S[j]:
      dp[i][j] = dp[i+1][j+1] + 1
      tmp = min(dp[i][j],i-j)
      ans = max(ans,tmp)
print(ans)

Fixed submission:
N = int(input())
S = input()
dp = [[0]*(N+1) for i in range(N+1)]
ans = 0
for i in range(N-1,-1,-1):
  for j in range(i,-1,-1):
    if S[i] == S[j]:
      dp[i][j] = dp[i+1][j+1] + 1
      tmp = min(dp[i][j],i-j)
      ans = max(ans,tmp)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def lcs(s1, s2):
    dp = []
    for s2_k in s2:
        bgn_idx = 0
        for i, cur_idx in enumerate(dp):
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if not chr_idx:
                break
            dp[i] = min(cur_idx, chr_idx)
            bgn_idx = cur_idx
        else:
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if chr_idx:
                dp.append(chr_idx)
    return len(dp)

def solve():
    N = int(input())
    S = input()
    ans = 0
    for i in range(1,N):
      S1 = S[:i]
      S2 = S[i:]
      ans = max(ans,lcs(S1, S2))
    print(ans)

if __name__ == '__main__':
    solve()

Fixed submission:
N = int(input())
S = input()
dp = [[0]*(N+1) for i in range(N+1)]
ans = 0
for i in range(N-1,-1,-1):
  for j in range(i,-1,-1):
    if S[i] == S[j]:
      dp[i][j] = dp[i+1][j+1] + 1
      tmp = min(dp[i][j],i-j)
      ans = max(ans,tmp)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
for i in range(N-1):
  for j in range(i+1,N):
    S1 = S[i:j]
    S2 = S[j:]
    tmp = 0
    M = min(j-i,N-j)
    for k in range(M):
      if S1[k] == S2[k]:
        tmp += 1
      else:
        break
    ans = max(tmp,ans)
print(ans)

Fixed submission:
N = int(input())
S = input()
dp = [[0]*(N+1) for i in range(N+1)]
ans = 0
for i in range(N-1,-1,-1):
  for j in range(i,-1,-1):
    if S[i] == S[j]:
      dp[i][j] = dp[i+1][j+1] + 1
      tmp = min(dp[i][j],i-j)
      ans = max(ans,tmp)
print(ans)
Buggy python submission (verdict: Runtime Error):
from collections import deque
import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
table = [[] for i in range(N)]
for i in range(N-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  table[a].append(b)
  table[b].append(a)
task = [0]*N
point = [-1]*N
point[0] = 0
for i in range(Q):
  p,x = map(int, input().split())
  p -= 1
  task[p] += x
H = deque()
H.append(0)

while H:
  p = H.popleft()
  point[p] += task[p]
  for np in table[p]:
    if point[np] == -1:
      H.append(np)
      point[np] = point[p]

print(" ".join(map(str, point[i])))


Fixed submission:
from collections import deque
import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
table = [[] for i in range(N)]
for i in range(N-1):
  a,b = map(int, input().split())
  a -= 1
  b -= 1
  table[a].append(b)
task = [0]*N
count = [0]*N
for i in range(Q):
  p,x = map(int,input().split())
  p -= 1
  task[p] += x
H = deque()
H.append(0)
while H:
  y = H.popleft()
  count[y] += task[y]
  for ny in table[y]:
    H.append(ny)
    count[ny] = count[y]
print(" ".join(map(str,count)))
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = []
ans = 0
for i in range(N):
  s = input()
  S.append(s)
for i in range(N-1):
  for j in range(i+1,N):
    A = [False]*10
    B = [False]*10
    for k in range(10):
      for l in range(10):
        if S[i][k] == S[j][l] and A[k] == False and B[l] == False:
          A[k] = True
          B[l] = True
          continue
    if all(A):
      ans += 1
print(ans)

Fixed submission:
N = int(input())
memo = {}
ans = 0
for i in range(N):
  s = list(input())
  s.sort()
  s = str(s)
  if s in memo.keys():
    memo[s] += 1
  else:
    memo[s] = 1
for v in memo.values():
  if v >= 2:
    ans += v*(v-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = []
ans = 0
for i in range(N):
  s = list(input())
#  c = Counter(s)
  c = sorted(s)
  S.append(c)
for i in range(N-1):
  for j in range(i+1,N):
    if S[i] == S[j]:
      ans += 1
print(ans)

Fixed submission:
N = int(input())
memo = {}
ans = 0
for i in range(N):
  s = list(input())
  s.sort()
  s = str(s)
  if s in memo.keys():
    memo[s] += 1
  else:
    memo[s] = 1
for v in memo.values():
  if v >= 2:
    ans += v*(v-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = []
ans = 0
for i in range(N):
  s = list(input())
#  c = Counter(s)
  c = sorted(s)
  S.append(c)
for i in range(N-1):
  for j in range(i+1,N):
    if S[i] == S[j]:
      ans += 1
print(ans)

Fixed submission:
N = int(input())
memo = {}
ans = 0
for i in range(N):
  s = list(input())
  s.sort()
  s = str(s)
  if s in memo.keys():
    memo[s] += 1
  else:
    memo[s] = 1
for v in memo.values():
  if v >= 2:
    ans += v*(v-1)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
S = []
ans = 0
for i in range(N):
  s = list(input())
  c = Counter(s)
  S.append(c)
for i in range(N-1):
  for j in range(i+1,N):
    if S[i] == S[j]:
      ans += 1
print(ans)

Fixed submission:
N = int(input())
memo = {}
ans = 0
for i in range(N):
  s = list(input())
  s.sort()
  s = str(s)
  if s in memo.keys():
    memo[s] += 1
  else:
    memo[s] = 1
for v in memo.values():
  if v >= 2:
    ans += v*(v-1)//2
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
C = [[1]*N,[0]*N,[0]*N]
j = 0
while C[0] != C[2]:
  k1 = j%3
  k2 = (j+1)%3
  C[k2] = [0]*N
  for i in range(N):
    if S[i] == "R":
      C[k2][i+1] += C[k1][i]
    else:
      C[k2][i-1] += C[k1][i]
  j += 1
if j % 2 == 0:
  print(" ".join(map(str,C[k2])))
else:
  print(" ".join(map(str,C[k1])))


Fixed submission:
S = input()
N = len(S)
C = [0]*N
count = 0
idxl,idxm,idxr = 0,0,0
for i in range(N-1):
  if S[i] == "R" and S[i+1] == "L":
    idxm = i
  if (S[i] == "L" and S[i+1] == "R") or i == N-2:
    idxr = i if i < N-2 else N-1 #正しい？
    count = (idxr-idxl)+1
    nr = (idxm-idxl)+1
    nl = (idxr-idxm)
#    print(i,count,idxm,idxr)
    if count%2 == 0:
      C[idxm] = C[idxm+1] = count//2
    else:
      m = count//2
      if (nr > nl and nr%2 == 1) or (nr < nl and nl%2 == 0):
        C[idxm] = m+1
        C[idxm+1] = m
      else:
        C[idxm] = m
        C[idxm+1] = m+1
#    print(i,idxl,idxm,idxr)
    idxl = i+1
print(" ".join(map(str,C)))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split()]
r = [-1]*N
s = A.index(min(A))

for i in range(A[s]+1):
  r[s] = i*2
  for j in range(1,N):
    k = (s+j)%N
    r[k] = 2*A[k-1]-r[k-1]
  if (r[s] + r[s-1])//2 == A[s-1]:
    print(" ".join(map(str,r)))
  

Fixed submission:
N = int(input())
A = [int(i) for i in input().split()]
r = [-1]*N
sumA_ev = sum([A[i] for i in range(N) if i%2 == 1])
r[0] = sum(A) - 2*sumA_ev
for i in range(N-1):
  r[i+1] = 2*A[i] - r[i]
print(" ".join(map(str,r)))
Buggy python submission (verdict: Runtime Error):
import collections

N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
ans = N
pq = []

for i in range(N):
  for j in range(N):
    pq.append((xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]))
c = collections.Counter(pq)
if c.most_common()[0][0] == (0,0):
  ans -= c.most_common()[1][1]
else:
  ans -= c.most_common()[0][1]
print(ans)

Fixed submission:
import collections

N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
ans = N
if N == 1:
  print(1)
  exit()
pq = []

for i in range(N):
  for j in range(N):
    pq.append((xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]))
c = collections.Counter(pq)
if c.most_common()[0][0] == (0,0):
  ans -= c.most_common()[1][1]
else:
  ans -= c.most_common()[0][1]
print(ans)

Buggy python submission (verdict: Runtime Error):
import collections

N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
ans = N
pq = []

for i in range(N):
  for j in range(i+1, N):
    pq.append((xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]))
c = collections.Counter(pq)
ans -= c.most_common()[0][1]
print(ans)

Fixed submission:
import collections

N = int(input())
xy = [list(map(int, input().split())) for _ in range(N)]
ans = N
if N == 1:
  print(1)
  exit()
pq = []

for i in range(N):
  for j in range(N):
    pq.append((xy[j][0]-xy[i][0], xy[j][1]-xy[i][1]))
c = collections.Counter(pq)
if c.most_common()[0][0] == (0,0):
  ans -= c.most_common()[1][1]
else:
  ans -= c.most_common()[0][1]
print(ans)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
a = int(input())
P = [0]*(N+1)
P[0] = 1
if a == 1:
  P[1] = 0
  try:
    a = int(input())
  except EOFError:
    pass
    
else:
  P[1] = 1

for i in range(2,N+1):
  if i == a:
    P[i] = 0
    try:
      a = int(input())
    except EOFError:
      continue
  else:
    P[i] = P[i-1] + P[i-2]
print(P[-1]%1000000007)

Fixed submission:
N, M = map(int, input().split())
try:
  a = int(input())
except EOFError:
  a = 0
P = [0]*(N+1)
P[0] = 1
if a == 1:
  P[1] = 0
  try:
    a = int(input())
  except EOFError:
    pass
    
else:
  P[1] = 1

for i in range(2,N+1):
  if i == a:
    P[i] = 0
    try:
      a = int(input())
    except EOFError:
      continue
  else:
    P[i] = P[i-1] + P[i-2]
print(P[-1]%1000000007)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
a = int(input())
P = [0]*(N+1)
P[0] = 1
if a == 1:
  P[1] = 0
  a = int(input())
else:
  P[1] = 1

for i in range(2,N+1):
  if i == a:
    P[i] = 0
    try:
      a = int(input())
    except EOFError:
      continue
  else:
    P[i] = P[i-1] + P[i-2]
print(P[-1]%1000000007)

Fixed submission:
N, M = map(int, input().split())
try:
  a = int(input())
except EOFError:
  a = 0
P = [0]*(N+1)
P[0] = 1
if a == 1:
  P[1] = 0
  try:
    a = int(input())
  except EOFError:
    pass
    
else:
  P[1] = 1

for i in range(2,N+1):
  if i == a:
    P[i] = 0
    try:
      a = int(input())
    except EOFError:
      continue
  else:
    P[i] = P[i-1] + P[i-2]
print(P[-1]%1000000007)
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
tmp = 0

for i in range(N):
    n = i
    count = 0
    while n <= K:
        n *= 2
        count += 1
    tmp += (1/N) * (1/2)**count
print(tmp)



Fixed submission:
N, K = map(int, input().split())
tmp = 0

for i in range(1,N+1):
    n = i
    if n < K:
      count = 0
      while n < K:
        n *= 2
        count += 1
    else:
      count = 0
    tmp += (1/N) * (1/2)**count
#    print(i, count, n, tmp)
print(tmp)
Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
tmp = [R, G, B]
tmp.sort()

rmax = N // tmp[2]
gmax = N // tmp[1]

for i in range(rmax + 1):
  for j in range(gmax + 1):
    if (N - i * tmp[2] - j * tmp[1]) < 0:
      break
    if (N - i * tmp[2] - j * tmp[1]) % tmp[0] == 0:
      ans += 1
#      print(ans, i, j)
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
r, g = 0, 0
rmax = N // R
gmax = N // G

if R * G * B == 1:
  for i in range(N + 1):
    ans += N + 1 - i
  print(ans)
  exit()

for i in range(rmax + 1):
  for j in range(gmax + 1):
    if (N - i * R - j * G) < 0:
      break
    if (N - i * R - j * G) % B == 0:
      ans += 1
#      print(ans, i, j)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
r, g = 0, 0
rmax = N // R
gmax = N // G

for i in range(rmax + 1):
  for j in range(gmax + 1):
    if (N - i * R - j * G) < 0:
      break
    if (N - i * R - j * G) % B == 0:
      ans += 1
#      print(ans, i, j)
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
r, g = 0, 0
rmax = N // R
gmax = N // G

if R * G * B == 1:
  for i in range(N + 1):
    ans += N + 1 - i
  print(ans)
  exit()

for i in range(rmax + 1):
  for j in range(gmax + 1):
    if (N - i * R - j * G) < 0:
      break
    if (N - i * R - j * G) % B == 0:
      ans += 1
#      print(ans, i, j)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
r, g0 = 0, 0

while r * R <= N:
  g = g0
#  Nr = N
  Nr = N - r * R
#  print("Nr:", Nr, "r:", r)
  while g * G <= Nr:
#    Ng = Nr
    Ng = Nr - g * G
#    print("Ng:", Ng, "g:", g)
    if Ng % B == 0:
#      b = (Ng/B)
      ans += 1
#      print("ANS:", ans, "r:", r, "g:", g, "b:", b)
    g += 1
  r += 1
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
r, g = 0, 0
rmax = N // R
gmax = N // G

if R * G * B == 1:
  for i in range(N + 1):
    ans += N + 1 - i
  print(ans)
  exit()

for i in range(rmax + 1):
  for j in range(gmax + 1):
    if (N - i * R - j * G) < 0:
      break
    if (N - i * R - j * G) % B == 0:
      ans += 1
#      print(ans, i, j)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
R, G, B, N = map(int, input().split())
ans = 0
r, g0, b0 = 0, 0, 0

while r * R <= N:
  g = g0
  Nr = N
  Nr -= r * R
#  print("Nr:", Nr, "r:", r)
  while g * G <= Nr:
    Ng = Nr
    Ng -= g * G
#    print("Ng:", Ng, "g:", g)
    if Ng % B == 0:
      b = (Ng/B)
      ans += 1
#      print("ANS:", ans, "r:", r, "g:", g, "b:", b)
    g += 1
  r += 1
print(ans)

Fixed submission:
R, G, B, N = map(int, input().split())
ans = 0
r, g = 0, 0
rmax = N // R
gmax = N // G

if R * G * B == 1:
  for i in range(N + 1):
    ans += N + 1 - i
  print(ans)
  exit()

for i in range(rmax + 1):
  for j in range(gmax + 1):
    if (N - i * R - j * G) < 0:
      break
    if (N - i * R - j * G) % B == 0:
      ans += 1
#      print(ans, i, j)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys, fractions
from collections import deque
input = sys.stdin.readline

N = int(input())
A = deque(int(i) for i in input().split())
ans = 0

for i in range(N):
  a = A.popleft()
  tmp = A[0]
  for j in range(1,N-1):
    tmp = fractions.gcd(tmp, A[j])
  ans = max(ans, tmp)
  A.append(a)
print(ans)


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

def gcd(m, n):
  x = max(m, n)
  y = min(m, n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x % y
      x = y
      y = z
    else:
      return z

L, R = {}, {}
for i in range(N):
  if i == 0:
    L[0] = A[0]
    R[N-(i+1)] = A[N-(i+1)]
  else:
    L[i] = gcd(L[i-1], A[i])
    R[N-(i+1)] = gcd(R[N-i], A[N-(i+1)])

for i in range(N):
  if i == 0:
    tmp = R[1]
  elif i == N-1:
    tmp = L[N-2]
  else:
    tmp = gcd(L[i-1], R[i+1])
  ans = max(tmp, ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys, fractions
input = sys.stdin.readline

N = int(input())
A = [int(i) for i in input().split()]
ans = 0

for i in range(N):
  a = A.pop(0)
  tmp = A[0]
  for j in range(1,N-1):
    tmp = fractions.gcd(tmp, A[j])
  ans = max(ans, tmp)
  A.append(a)
print(ans)


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

def gcd(m, n):
  x = max(m, n)
  y = min(m, n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x % y
      x = y
      y = z
    else:
      return z

L, R = {}, {}
for i in range(N):
  if i == 0:
    L[0] = A[0]
    R[N-(i+1)] = A[N-(i+1)]
  else:
    L[i] = gcd(L[i-1], A[i])
    R[N-(i+1)] = gcd(R[N-i], A[N-(i+1)])

for i in range(N):
  if i == 0:
    tmp = R[1]
  elif i == N-1:
    tmp = L[N-2]
  else:
    tmp = gcd(L[i-1], R[i+1])
  ans = max(tmp, ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys, fractions
input = sys.stdin.readline

N = int(input())
A = [int(i) for i in input().split()]
ans = 0

for i in range(N):
  a = A.pop(0)
  tmp = A[0]
  for j in range(1,N-1):
    tmp = fractions.gcd(tmp, A[j])
  ans = max(ans, tmp)
  A.append(a)
print(ans)


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

def gcd(m, n):
  x = max(m, n)
  y = min(m, n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x % y
      x = y
      y = z
    else:
      return z

L, R = {}, {}
for i in range(N):
  if i == 0:
    L[0] = A[0]
    R[N-(i+1)] = A[N-(i+1)]
  else:
    L[i] = gcd(L[i-1], A[i])
    R[N-(i+1)] = gcd(R[N-i], A[N-(i+1)])

for i in range(N):
  if i == 0:
    tmp = R[1]
  elif i == N-1:
    tmp = L[N-2]
  else:
    tmp = gcd(L[i-1], R[i+1])
  ans = max(tmp, ans)

print(ans)
Buggy python submission (verdict: Runtime Error):
import bisect
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
idx = bisect.bisect_left(A,0)
if idx%2 == 0:
  ans = abs(sum(A[0:idx])) + sum(A[idx:])
else:
  ans = abs(sum(A[0:idx-1])) + abs(A[idx-1]+A[idx]) + sum(A[idx+1:])
print(ans)

Fixed submission:
import bisect
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
idx = bisect.bisect_left(A,0)
ans = 0
if idx%2 == 0:
  ans += abs(sum(A[0:idx]))
  if N > idx:
    ans += sum(A[idx:])
else:
  ans += abs(sum(A[0:idx-1]))
  if idx < N:
    ans += abs(A[idx-1]+A[idx])
  elif idx == N:
    ans += A[idx-1]
  if idx + 1 < N:
    ans += sum(A[idx+1:])
print(ans)

Buggy python submission (verdict: Runtime Error):
import copy

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

plus = [i for i in A if i >= 0]
minus = [i for i in A if i < 0]

M = len(minus)


if M % 2 == 0:
  ans = sum(A) + abs(sum(minus))*2
elif M % 2 == 1:
  minus2 = copy.copy(minus)
  minus2.pop(-1)
  tmp = sum(A) + abs(sum(minus2))*2
  tmp2 = sum(A) + abs(sum(minus))*2 - min(plus) * 2
  ans = max(tmp, tmp2)
print(ans)

Fixed submission:
import bisect
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
idx = bisect.bisect_left(A,0)
ans = 0
if idx%2 == 0:
  ans += abs(sum(A[0:idx]))
  if N > idx:
    ans += sum(A[idx:])
else:
  ans += abs(sum(A[0:idx-1]))
  if idx < N:
    ans += abs(A[idx-1]+A[idx])
  elif idx == N:
    ans += A[idx-1]
  if idx + 1 < N:
    ans += sum(A[idx+1:])
print(ans)

Buggy python submission (verdict: Runtime Error):
import copy

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

plus = [i for i in A if i >= 0]
minus = [i for i in A if i < 0]

M = len(minus)


if M % 2 == 0:
  ans = sum(A) + abs(sum(minus))*2
else:
  minus2 = copy.copy(minus)
  minus2.pop(-1)
  tmp = sum(A) + abs(sum(minus2))*2
  tmp2 = sum(A) + abs(sum(minus))*2 - min(plus) * 2
  ans = max(tmp, tmp2)
print(ans)
  
  
  

Fixed submission:
import bisect
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
idx = bisect.bisect_left(A,0)
ans = 0
if idx%2 == 0:
  ans += abs(sum(A[0:idx]))
  if N > idx:
    ans += sum(A[idx:])
else:
  ans += abs(sum(A[0:idx-1]))
  if idx < N:
    ans += abs(A[idx-1]+A[idx])
  elif idx == N:
    ans += A[idx-1]
  if idx + 1 < N:
    ans += sum(A[idx+1:])
print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
width,l,r = [],[],[]

for i in range(K):
  if "0" not in S:
    break
  for j in range(N-1):
    if j == 0:
      sw = 1
      l.append(0)
      for k in range(j,N-1-j):
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
    if S[j] == "0" and S[j+1] == "1":
      sw = 1
      for k in range(j+1,N-1-j):
        if S[k] == "1" and S[k+1] == "0":
          l.append(k+1)
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
  idx = width.index(max(width))
  ans = max(width)
  S = S[:l[idx]] + "1"*(r[idx]-l[idx]) + S[r[idx]:]
print(ans)

Fixed submission:
N, K = map(int, input().split())
S = list(map(int,list(input())))
A = []
B = []
if S[0] == 1:
  A.append(0)
  B.append(1)
else:
  A.extend([0,1])
  B.extend([0,0])
  
for i in range(1,N):
  if S[i] == S[i-1]:
    if S[i] == 0:
      A[-1] += 1
    else:
      B[-1] += 1
  else:
    if S[i] == 0:
      A.append(1)
      B.append(0)
    else:
      A.append(0)
      B.append(1)
if B[-1] == 0:
  A.append(0)
  B.append(0)

M = len(A)
tmp = sum(A[0:2*K+1]) + sum(B[0:2*K+1])
ans = tmp
for i in range(2,M-2*K,2):
  tmp -= A[i-1] + B[i-2]
  tmp += A[i+2*K-1] + B[i+2*K]
  ans = max(ans,tmp)
print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
width,l,r = [],[],[]

for i in range(K):
  if "0" not in S:
    break()
  for j in range(N-1):
    if j == 0:
      sw = 1
      l.append(0)
      for k in range(j,N-1-j):
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
    if S[j] == "0" and S[j+1] == "1":
      sw = 1
      for k in range(j+1,N-1-j):
        if S[k] == "1" and S[k+1] == "0":
          l.append(k+1)
        if S[k] == "0" and S[k+1] == "1":
          r.append(k+1) #r自体は1
          sw = 2
        if sw == 2 and S[k] == "1" and S[k+1] == "0":
          width.append(k-j)
          break
  idx = width.index(max(width))
  ans = max(width)
  S = S[:l[idx]] + "1"*(r[idx]-l[idx]) + S[r[idx]:]
print(ans)

Fixed submission:
N, K = map(int, input().split())
S = list(map(int,list(input())))
A = []
B = []
if S[0] == 1:
  A.append(0)
  B.append(1)
else:
  A.extend([0,1])
  B.extend([0,0])
  
for i in range(1,N):
  if S[i] == S[i-1]:
    if S[i] == 0:
      A[-1] += 1
    else:
      B[-1] += 1
  else:
    if S[i] == 0:
      A.append(1)
      B.append(0)
    else:
      A.append(0)
      B.append(1)
if B[-1] == 0:
  A.append(0)
  B.append(0)

M = len(A)
tmp = sum(A[0:2*K+1]) + sum(B[0:2*K+1])
ans = tmp
for i in range(2,M-2*K,2):
  tmp -= A[i-1] + B[i-2]
  tmp += A[i+2*K-1] + B[i+2*K]
  ans = max(ans,tmp)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, Q = map(int, input().split())
S = input()
for i in range(Q):
  L, R = map(int, input().split())
  print(S[L-1:R].count("AC"))

Fixed submission:
N, Q = map(int, input().split())
S = input()
t = [0]*(N)
for i in range(N-1):
  t[i+1] = t[i] + (1 if S[i:i+2] == "AC" else 0) 
for i in range(Q):
  L, R = map(int, input().split())
  print(t[R-1]-t[L-1])
Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())

def divlist(x):
  numbers = []
  divX = []
  try:
    for i in range(1,x+1):
      numbers.append(i)
      if x % i == 0:
        divX.append(i)
    divX = set(divX)
    return divX
  except ZeroDivisionError:
      print("ZeroDivisionError!!")

divA = divlist(A)
divB = divlist(B)
common = sorted(list(divA & divB))

#print("divA:", divA)
#print("divB:", divB)
#print("divC:", common)
print(common[K-1])

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

answer = 0

if B//A < C:
  answer = B//A
else:
  answer = C

print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
x = [int(input()) for i in range(Q)]

def search(a):
  cur = a
  sx, tx = [], [] #xiとsi、tiの差のリスト
  for si in s:
    sx.append(abs(si - a))
  for ti in t:
    tx.append(abs(ti - a))
  stmin = [[sx.index(min(sx)),min(sx)], [tx.index(min(tx)),min(tx)]]
  ans = min(stmin[0][1], stmin[1][1])
  
#  if stmin[0][1] <= stmin[1][1]: #sのほうが近いとき
  cur1 = s[stmin[0][0]]
  tcur = []
  for ti in t:
    tcur.append(abs(ti - cur1))
  ans1 = stmin[0][1] + min(tcur)
    
#  if stmin[0][1] > stmin[1][1]: #tのほうが近いとき
  cur2 = t[stmin[1][0]]
  scur = []
  for si in s:
    scur.append(abs(si - cur2))
  ans2 = stmin[1][1] + min(scur)
  
  print(min(ans1, ans2))
  
for xi in x:
  search(xi)

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
  x = int(input())
  b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
  res = INF
  for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
      d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
      res = min(res, d1, d2)
  print(res)