Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
import collections
from operator import mul
from functools import reduce
N, M = map(int, input().split())

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

ps = []
tmp = M
for m in range(2, M+1):
    while tmp % m == 0:
        tmp /= m
        ps.append(m)
    if int(tmp) == 1:
        break
ps = collections.Counter(ps)

ans = 1
mod = 10**9+7
for c in ps.values():
    ans *= cmb(c+N-1, c) % mod
    if ans > mod:
        ans %= mod

print(ans)


Fixed submission:
#!/usr/bin/env python
import collections
from operator import mul
from functools import reduce
N, M = map(int, input().split())

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

ps = []
tmp = M
for m in range(2, M+1):
    while tmp % m == 0:
        tmp /= m
        ps.append(m)
    if int(tmp) == 1:
        break
    if m*m > M:
        if N > 1:
            ps.append(M)
        break

ps = collections.Counter(ps)

ans = 1
mod = 10**9+7
for c in ps.values():
    ans *= cmb(c+N-1, c) % mod
    if ans > mod:
        ans %= mod

print(ans)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
H, W = map(int, input().split())
A_raw = [[int(i) for i in input().split()] for j in range(H)]

c = 1
A = []
d = {}
for h in range(H):
    if h % 2 == 0:
        for a in A_raw[h]:
            A.append(a)
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)]))
    else:
        for a in A_raw[h][::-1]:
            A.append(a)
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)[::-1]]))
    c += W

c = 0
ans = []
for i in range(len(A)-1):
    if A[i] % 2 == 1:
        A[i+1] += 1
        A[i] -= 1
        c += 1
        ans.append([*d[i+1], *d[i+2]])
print(c)
for x in ans:
    print(*x)


Fixed submission:
#!/usr/bin/env python
H, W = map(int, input().split())
A_raw = [[int(i) for i in input().split()] for j in range(H)]

c = 1
A = []
d = {}
for h in range(H):
    if h % 2 == 0:
        for a in A_raw[h]:
            A.append(a)
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)]))
    else:
        for a in A_raw[h][::-1]:
            A.append(a)
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)[::-1]]))
    c += W

c = 0
ans = []
for i in range(len(A)-1):
    if A[i] % 2 == 1:
        A[i+1] += 1
        A[i] -= 1
        c += 1
        ans.append([d[i+1], d[i+2]])
print(c)
for x in ans:
    print(x[0][0], x[0][1], x[1][0], x[1][1])

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python
from itertools import chain
H, W = map(int, input().split())
A_raw = [[int(i) for i in input().split()] for j in range(H)]

c = 1
A = []
d = {}
for h in range(H):
    if h % 2 == 0:
        A.append(A_raw[h])
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)]))
    else:
        A.append(A_raw[h][::-1])
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)[::-1]]))
    c += W
A = list(chain.from_iterable(A))

c = 0
ans = []
for i in range(len(A)-1):
    if A[i] % 2 == 1:
        A[i+1] += 1
        A[i] -= 1
        c += 1
        ans.append([*d[i+1], *d[i+2]])
print(c)
for x in ans:
    print(*x)


Fixed submission:
#!/usr/bin/env python
H, W = map(int, input().split())
A_raw = [[int(i) for i in input().split()] for j in range(H)]

c = 1
A = []
d = {}
for h in range(H):
    if h % 2 == 0:
        for a in A_raw[h]:
            A.append(a)
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)]))
    else:
        for a in A_raw[h][::-1]:
            A.append(a)
        d.update(zip(range(c, c + W), [(h+1, w) for w in range(1, W+1)[::-1]]))
    c += W

c = 0
ans = []
for i in range(len(A)-1):
    if A[i] % 2 == 1:
        A[i+1] += 1
        A[i] -= 1
        c += 1
        ans.append([d[i+1], d[i+2]])
print(c)
for x in ans:
    print(x[0][0], x[0][1], x[1][0], x[1][1])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input().split()[0])
min = N

for i in range(N):
  cnt = 0
  for j in range(N):
    if j < i and S[j] == 'W':
      cnt += 1
    if i < j and S[j] == 'E':
      cnt += 1
  if min > cnt: min = cnt 

print(min)

Fixed submission:
N = int(input())
S = input()
min = S[1:N].count('E')
now = S[1:N].count('E')

for i in range(1,N):
  if S[i-1] == 'E' and S[i] == 'E':
    now -= 1
  if S[i-1] == 'W' and S[i] == 'W':
    now += 1
  if now < min:
    min = now

print(min)
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python
S = input()
K = int(input())
s_len = len(S)
s_set = set()
for i in range(1, s_len+1):
    j = 0
    while j+i <= s_len:
        s_set.add(S[j:j+i])
        j += 1
    
s_sort = sorted(list(s_set))
print(s_sort[K-1])

Fixed submission:
#!/usr/bin/env python
S = input()
K = int(input())
s_len = len(S)
s_set = set()
for i in range(1, s_len+1):
    j = 0
    if i > K:
        break
    else:
        while j+i <= s_len:
            s_set.add(S[j:j+i])
            j += 1
    
s_sort = sorted(list(s_set))
print(s_sort[K-1])
Buggy python submission (verdict: Time Limit Exceeded):
r, g, b, n = map(int, input().split())
cnt = 0
for red in range(n // r + 1):
    for green in range(n // g + 1):
        blue = n - red * r - green * g
        if blue >= 0 and blue % b == 0:
            cnt += 1
print(cnt)

Fixed submission:
r, g, b, n = map(int, input().split())
cnt = 0
for red in range(n // r + 1):
    for green in range((n - r * red) // g + 1):
        blue = n - red * r - green * g
        if blue >= 0 and blue % b == 0:
            cnt += 1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

S = input().strip()
N = len(S)
converges = [0] * N

for i in range(N):
    s = S[i]
    if s == 'R':
        for j in range(1, 100000):
            if S[i + j] == 'L':
                if j % 2 == 0:
                    converges[i + j] += 1
                    break
                else:
                    converges[i + j - 1] += 1
                    break
    else:
        for j in range(1, 100000):
            if S[i - j] == 'R':
                if j % 2 == 0:
                    converges[i - j] += 1
                    break
                else:
                    converges[i - j + 1] += 1
                    break

print(' '.join(map(str, converges)))

Fixed submission:
import sys

input = sys.stdin.readline

S = input().strip()
N = len(S)

crs = list(map(len, S.split('R')))
cls = list(map(len, S.split('L')))
converges=[0] * N

current_index = 0
for cr in crs:
    if cr == 0:
        current_index += 1
    else:
        odd = cr // 2
        even = cr - odd
        converges[current_index - 1] += odd
        converges[current_index] += even
        current_index += cr+1

current_index = N - 1
for cl in cls[::-1]:
    if cl == 0:
        current_index -= 1
    else:
        odd = cl // 2
        even = cl - odd
        converges[current_index + 1] += odd
        converges[current_index] += even
        current_index -= cl + 1

print(' '.join(map(str, converges)))
Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

N = int(input().strip())
As = list(map(int, input().strip().split(' ')))

xs = [0] * N
for n in range(N):
    rotate = As[n:] + As[:n]
    xs[n] = sum(rotate[0::2]) - sum(rotate[1::2])
print(" ".join(map(str, xs)))

Fixed submission:
import sys

input = sys.stdin.readline

N = int(input().strip())
As = list(map(int, input().strip().split(' ')))


xs = [0] * N
xs[0] = sum(As[0::2]) - sum(As[1::2])
for n in range(1, N):
    xs[n] = As[n-1] * 2 - xs[n - 1]
print(" ".join(map(str, xs)))
Buggy python submission (verdict: Runtime Error):
import math
import sys


def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


input = sys.stdin.readline

N, K = map(int, input().strip().split(' '))
div = 1000000000 + 7

for i in range(K):
    perm = comb(N - K + 1, i + 1) * comb(K - 1, i)
    print(perm % div)


Fixed submission:
import math
import sys


def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


input = sys.stdin.readline

N, K = map(int, input().strip().split(' '))
div = 1000000000 + 7

for i in range(K):
    if i > N - K:
        print(0)
    else:
        perm = comb(N - K + 1, i + 1) * comb(K - 1, i)
        print(perm % div)
Buggy python submission (verdict: Runtime Error):
import sys, math

input = sys.stdin.readline
gcd = math.gcd
A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)

Fixed submission:
import sys
from fractions import gcd

input = sys.stdin.readline
A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)

Buggy python submission (verdict: Runtime Error):
import math
 
A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // math.gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)

Fixed submission:
import sys
from fractions import gcd

input = sys.stdin.readline
A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)

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

A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // math.gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)

Fixed submission:
import sys
from fractions import gcd

input = sys.stdin.readline
A, B, C, D = map(int, input().split(' '))
num = B - A + 1
div_c = B // C - (A - 1) // C
div_d = B // D - (A - 1) // D
lcd = C * D // gcd(C, D)
div_cd = B // lcd - (A - 1) // lcd
print(num - div_c - div_d + div_cd)
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()

index = 0
counter = 0
while index < N:
    if S[index] == '#':
        offset = 1
        while index + offset < N and S[index + offset] == '#':
            offset += 1

        if index + offset >= N:
            break
        else:
            count = 1
            memory = offset
            offset += 1
            while index + offset < N and S[index + offset] == '.':
                count += 1
                offset += 1
            counter += count if count < memory else memory
            index += offset-1
    else:
        index += 1
print(str(counter))


Fixed submission:
A, B, C = map(int, input().strip().split(' '))
message = ''
if A > B:
  message = 'Yes' if A > C > B else 'No'
else:
  message = 'Yes' if B > C > A else 'No'
print(message)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
def calc(ln, i):
    ans = [0]*ln
    ans[i] += i // 2 + 1
    ans[i] += (ln - i - 1) // 2
    ans[i+1] += (i + 1) // 2
    ans[i+1] += (ln-i) // 2
    return ans

left = 0
right = 0
ans = []
while left < N:
    while S[right] == "R":
        right += 1
    ind = right - 1
    while right < N and S[right] == "L":
        right += 1
    ans = ans + calc(right - left, ind-left)
    left = right
print(" ".join(map(str, ans)))

Fixed submission:
S = input()
N = len(S)
def calc(ln, i):
    ans = [0]*ln
    ans[i] += i // 2 + 1
    ans[i] += (ln - i - 1) // 2
    ans[i+1] += (i + 1) // 2
    ans[i+1] += (ln-i) // 2
    print(" ".join(map(str, ans)), end=" ")

left = 0
right = 0
while left < N:
    while S[right] == "R":
        right += 1
    ind = right - 1
    while right < N and S[right] == "L":
        right += 1
    calc(right - left, ind-left)
    left = right
Buggy python submission (verdict: Runtime Error):
S = input()
dp = [[0]*13 for _ in range(len(S))]
mod = 10**9+7
for i in range(13):
    if S[0] == "?":
        dp[0][i] = 1 if i <= 9 else 0
    else:
        dp[0][i] = 1 if int(S[0]) == i else 0
for i in range(1,len(S)):
    if S[i] == "?":
        for j in range(10):
            for k in range(13):
                dp[i][(k*10+j) % 13] = (dp[i][(k*10+j) % 13]+dp[i-1][k])%mod
    else:
        j = int(S[i])
        for k in range(13):
            dp[i][(k*10+j)%13] = (dp[i][(k*10+j)%13]+dp[i-1][k])%mod
print(dp[len(S)-1][5])

Fixed submission:
A,B = map(int,input().split())
if A == B:
    print(0)
else:
    if (A+B) % 2 == 0:
        print((A+B)//2)
    else:
        print("IMPOSSIBLE")
Buggy python submission (verdict: Runtime Error):
import bisect as bs
N = int(input())
A = [int(input()) for _ in range(N)]
def LDS(a):
    N = len(a)
    INF = float("inf")
    dp = [-INF]*N
    dp[N-1] = a[0]
    for i in range(1, N):
        j = bs.bisect_left(dp, a[i])-1
        dp[j] = a[i]
    for i in range(N-1, -1, -1):
        if dp[i] == -INF:
            return N-i-1
    return N
print(LDS(A))

Fixed submission:
N = int(input())
a = list(map(int,input().split()))
b = [None for _ in range(N)]
for i in range(N):
    j = N-i
    if j > N//2:
        b[j-1] = a[j-1]
    else:
        s = 0
        for k in range(2*j-1, N, j):
            s = (s+b[k])%2
        b[j-1] = (a[j-1]-s) % 2
ans = []
for i in range(N):
    if b[i] == 1:
        ans.append(i+1)
if ans == []:
    print(0)
else:
    print(len(ans))
    print(" ".join(map(str, ans)))

Buggy python submission (verdict: Runtime Error):
import bisect as bs
N = int(input())
A = [int(input()) for _ in range(N)]
def LDS(a):
    N = len(a)
    INF = float("inf")
    dp = [-INF]*N
    dp[N-1] = a[0]
    for i in range(1, N):
        j = bs.bisect_left(dp, a[i])-1
        print(j)
        dp[j] = a[i]
    for i in range(N-1, -1, -1):
        if dp[i] == -INF:
            return N-i-1
    return N
print(LDS(A))

Fixed submission:
N = int(input())
a = list(map(int,input().split()))
b = [None for _ in range(N)]
for i in range(N):
    j = N-i
    if j > N//2:
        b[j-1] = a[j-1]
    else:
        s = 0
        for k in range(2*j-1, N, j):
            s = (s+b[k])%2
        b[j-1] = (a[j-1]-s) % 2
ans = []
for i in range(N):
    if b[i] == 1:
        ans.append(i+1)
if ans == []:
    print(0)
else:
    print(len(ans))
    print(" ".join(map(str, ans)))
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int,input().split())
edges = []

if K > (N-1)*(N-2)//2:
    print(-1)
else:
    edges.append((1,2))
    for i in range(3, N+1):
        edges.append((2, i))
    sa = (N-1)*(N-2)//2 - K
    for i in range(min(sa, N-2)):
        sa -= 1
        edges.append((1, i+3))
    if sa != 0:
        i = 3
        j = 4
        while sa > 0:
            while j < N and sa > 0:
                edges.append((i, j))
                j += 1
                sa -= 1
            i += 1
            j = i + 1
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
    else:
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
    

Fixed submission:
N, K = map(int,input().split())
edges = []

if K > (N-1)*(N-2)//2:
    print(-1)
else:
    edges.append((1,2))
    for i in range(3, N+1):
        edges.append((2, i))
    sa = (N-1)*(N-2)//2 - K
    for i in range(min(sa, N-2)):
        sa -= 1
        edges.append((1, i+3))
    if sa != 0:
        i = 3
        j = 4
        while sa > 0:
            while j <= N and sa > 0:
                edges.append((i, j))
                j += 1
                sa -= 1
            i += 1
            j = i + 1
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
    else:
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])

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

if K > (N-1)*(N-2)//2:
    print(-1)
else:
    edges.append((1,2))
    for i in range(3, N+1):
        edges.append((2, i))
    sa = (N-1)*(N-2)//2 - K
    for i in range(min(sa, N-2)):
        sa -= 1
        edges.append((1, i+3))
    if sa != 0:
        i = 3
        j = 4
        while sa > 0:
            while j < N and sa > 0:
                edges.append((i, j))
                j += 1
                sa -= 1
            i += 1
            j = i + 1
        print(len(M))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
    else:
        print(len(M))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
    

Fixed submission:
N, K = map(int,input().split())
edges = []

if K > (N-1)*(N-2)//2:
    print(-1)
else:
    edges.append((1,2))
    for i in range(3, N+1):
        edges.append((2, i))
    sa = (N-1)*(N-2)//2 - K
    for i in range(min(sa, N-2)):
        sa -= 1
        edges.append((1, i+3))
    if sa != 0:
        i = 3
        j = 4
        while sa > 0:
            while j <= N and sa > 0:
                edges.append((i, j))
                j += 1
                sa -= 1
            i += 1
            j = i + 1
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
    else:
        print(len(edges))
        for i in range(len(edges)):
            print(edges[i][0], edges[i][1])
Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
a = [int(input()) for _ in range(M)]
dp = [0]*(N+1)
j = 0
dp[0] = 1
mod = 10**9+7
for i in range(1, N+1):
    if i == a[j]:
        j += 1
        j = min(M-1, j)
    elif i == 1:
        dp[i] = 1
    else:
        dp[i] = (dp[i-1]+dp[i-2]) % mod
print(dp[N])

Fixed submission:
N,M=map(int,input().split())
a = [int(input()) for _ in range(M)]
dp = [0]*(N+1)
j = 0
dp[0] = 1
mod = 10**9+7
for i in range(1, N+1):
    if j < M and i == a[j]:
        j += 1
    elif i == 1:
        dp[i] = 1
    else:
        dp[i] = (dp[i-1]+dp[i-2]) % mod
print(dp[N])
Buggy python submission (verdict: Runtime Error):
N,K= map(int,input().split())
D = list(map(int,input().split()))
from itertools import accumulate
acc = [0]+list(accumulate(D, lambda x,y:x+y))
mx = 0
for k in range(K+1): # k 回とる
    rest = K-k # rest回まで捨てることができる
    for l in range(1, min(k+1, N)): # 左から何個とるか
        r = k-l
        left = acc[l]
        right = acc[N]-acc[N-r]
        tmp = left+right
        tmpl = D[:l]
        tmpr = D[N-r:N]
        minus = sorted([i for i in tmpl+tmpr if i < 0])
        tmp -= sum(minus[:rest])
        mx = max(mx, tmp)
print(mx)

Fixed submission:
N,K= map(int,input().split())
D = list(map(int,input().split()))
from itertools import accumulate
acc = [0]+list(accumulate(D, lambda x,y:x+y))
mx = 0
for k in range(min(K+1, N+1)): # k 回とる
    rest = K-k # rest回まで捨てることができる
    for l in range(min(k+1, N+1)): # 左から何個とるか
        r = k-l
        left = acc[l]
        right = acc[N]-acc[N-r]
        tmp = left+right
        tmpl = D[:l]
        tmpr = D[N-r:N]
        minus = sorted([i for i in tmpl+tmpr if i < 0])
        tmp -= sum(minus[:rest])
        mx = max(mx, tmp)
if K > N:
    tmp = acc[-1]
    rest = K - N
    minus = sorted([i for i in D if i < 0])
    tmp -= sum(minus[:rest])
    mx = max(mx, tmp)
print(mx)

Buggy python submission (verdict: Runtime Error):
N,K= map(int,input().split())
D = list(map(int,input().split()))
from itertools import accumulate
acc = [0]+list(accumulate(D, lambda x,y:x+y))
mx = 0
for k in range(K+1): # k 回とる
    rest = K-k # rest回まで捨てることができる
    for l in range(1, k+1): # 左から何個とるか
        r = k-l
        left = acc[l]
        right = acc[N]-acc[N-r]
        tmp = left+right
        tmpl = D[:l]
        tmpr = D[N-r:N]
        minus = sorted([i for i in tmpl+tmpr if i < 0])
        tmp -= sum(minus[:rest])
        mx = max(mx, tmp)
print(mx)

Fixed submission:
N,K= map(int,input().split())
D = list(map(int,input().split()))
from itertools import accumulate
acc = [0]+list(accumulate(D, lambda x,y:x+y))
mx = 0
for k in range(min(K+1, N+1)): # k 回とる
    rest = K-k # rest回まで捨てることができる
    for l in range(min(k+1, N+1)): # 左から何個とるか
        r = k-l
        left = acc[l]
        right = acc[N]-acc[N-r]
        tmp = left+right
        tmpl = D[:l]
        tmpr = D[N-r:N]
        minus = sorted([i for i in tmpl+tmpr if i < 0])
        tmp -= sum(minus[:rest])
        mx = max(mx, tmp)
if K > N:
    tmp = acc[-1]
    rest = K - N
    minus = sorted([i for i in D if i < 0])
    tmp -= sum(minus[:rest])
    mx = max(mx, tmp)
print(mx)
Buggy python submission (verdict: Runtime Error):
N = int(input())
headB=0
tailA=0 
both = 0
ans = 0
for j in range(N):
    t = input()
    if t[0] == "B":
        if t[-1] == "A":
            both += 1
        else:
            headB += 1
    elif t[-1] == "A":
        tailA += 1
    ans += t.count("AB")
if tailA > 0 or headB > 0:
    while both > 0:
        tmp = min(tailA+headB,both)
        ans += tmp
        both -= tmp
else:
    ans += both-1
ans += min(headB,tailA)
print(ans)

Fixed submission:
print((lambda x,y:x-y+1)(*map(int,input().split())))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
headB=0
tailA=0 
both = 0
ans = 0
for j in range(N):
    t = input()
    if t[0] == "B":
        if t[-1] == "A":
            both += 1
        else:
            headB += 1
    elif t[-1] == "A":
        tailA += 1
    ans += t.count("AB")
while both > 0:
    tmp = min(tailA,both)
    ans += tmp
    both -= tmp
    tmp = min(headB,both)
    ans += tmp
    both -= tmp
ans += min(headB,tailA)
print(ans)

Fixed submission:
N = int(input())
headB=0
tailA=0 
both = 0
ans = 0
for j in range(N):
    t = input()
    if t[0] == "B":
        if t[-1] == "A":
            both += 1
        else:
            headB += 1
    elif t[-1] == "A":
        tailA += 1
    ans += t.count("AB")
if both > 0:
    ans += both - 1
    if tailA > 0 or headB > 0:
        ans += 1
ans += min(tailA,headB)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

V = list(map(int,input().split()))
C = list(map(int,input().split()))

x = 0
y = 0
mx = 0
for i in range(2**N):
    x = 0
    y = 0
    for j in range(N):
        if (i >> j) & 1 == 1:
            x += V[j]
            y += C[j]
        mx = max(x-y,mx)
print(mx)

Fixed submission:
N = int(input())

V = list(map(int,input().split()))
C = list(map(int,input().split()))

x = 0
y = 0
mx = 0
l = [V[i]-C[i] for i in range(N)]
print(sum([ i for i in l if i>0 ]))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int,input().split()))
#ユークリッド互除法
def gcd(x,y):
    a = max(x,y)
    b = min(x,y)
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b

def gcdl(l):
    if len(l) == 1:
        return l[0]
    x = l[0]
    for i in l[1:]:
        x = gcd(i,x)
    return x

can = [1]*N
for i in range(N):
    can[i] = gcdl(A[0:i]+A[i+1:])
print(max(can))

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

#ユークリッド互除法
def gcd(x,y):
    a = max(x,y)
    b = min(x,y)
    r = a % b
    while r != 0:
        a = b
        b = r
        r = a % b
    return b

L = [A[0]]
R = [A[N-1]]*N
for i in range(N-1):
    L.append(gcd(L[i], A[i+1]))
    R[N-i-2]=gcd(R[N-i-1],A[N-i-2])
can = []
for i in range(N):
    if i == 0:
        can.append(R[1])
    elif i == N-1:
        can.append(L[N-2])
    else:
        can.append(gcd(L[i-1], R[i+1]))

print(max(can))