ZhenlongDai/ACPR huggingface dataset:
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):






from collections import deque





n=int(input())
s=[input() for _ in range(n)]
ans=0
bn=an=0
d=0
for i in range(n):
    ans+=s[i].count("AB")
    if s[i][0]=="B":
        an+=1
        if s[i][-1]=="A":
            d+=1
    if s[i][-1]=="A":
        bn+=1
if bn==an==d:
    print(ans+d-1)
else:
    print(ans+min(an,bn))

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):






from collections import deque





n=int(input())
s=[input() for _ in range(n)]
ans=0
bn=an=0
d=0
for i in range(n):
    ans+=s[i].count("AB")
    if s[i][0]=="B":
        an+=1
        if s[i][-1]=="A":
            d+=1
    if s[i][-1]=="A":
        bn+=1
if an==0 or bn==0:
  print(ans)
  exit()
if bn==an==d:
    print(ans+d-1)
else:
    print(ans+min(an,bn))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S =[0]*N
for i in range(N):
    S[i] = str(input())

ans = int(0)
a=int(0)
b=int(0)
c=int(0)
R=int(0)

for i in range(N):
    AB = S[i].split("AB")
    R = len(AB)-1

    ans += R
    R = 0

for i in range(N):
    if S[i].endswith('A') : a +=1
    if S[i].startswith('B') : b +=1
    if S[i].endswith('A') and S[i].startswith('B'): c +=1

if c==0: R=0
elif c==a and c==b:
    R = min(a, b)-1
else : R=min(a, b)

ans += R

print(ans)

Failing test case input:
7
RABZBBF
JOX
AVUQHMB
BPB
IVR
MCMABAOBHZ
RZMDJMA

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
S =[0]*N
for i in range(N):
    S[i] = str(input())

ans = int(0)
a=int(0)
b=int(0)
c=int(0)
R=int(0)

for i in range(N):
    AB = S[i].split("AB")
    R = len(AB)-1

    ans += R
    R = 0

for i in range(N):
    if S[i].endswith('A') : a +=1
    if S[i].startswith('B') : b +=1
    if S[i].endswith('A') and S[i].startswith('B'): c +=1

if c==0 and c==a and c==b: R=0
elif c==a and c==b:
    R = min(a, b)-1
else : R=min(a, b)

ans += R

print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())

A = []
B = []

ans = 0
for i in range(N):
    s = input()
    for i in range(len(s)-1):
        if s[i:i+2] == "AB":
            ans += 1
    if s[0] == "B":
        B.append(i)
    if s[-1] == "A":
        A.append(i)

A = set(A)
B = set(B)

if len(A) == N and len(B)==N:
    ans -= 1
elif len(A)> len(B):
    ans += len(B)
else:
    ans += len(A)
print(ans)


        


Failing test case input:
9
BEWPVCRWH
ZQNZYIJX
AERVAB
PA
HJMYITEOX
BCJHMRMNK
PB
QVFABZ
PRGKSPUNA

Expected output:
4


Actual output:
3


Fixed submission (verdict: Accepted):
N = int(input())

A = []
B = []

ans = 0
for i in range(N):
    s = input()
    for j in range(len(s)-1):
        if s[j:j+2] == "AB":
            ans += 1
    if s[0] == "B":
        B.append(i)
    if s[-1] == "A":
        A.append(i)

A = set(A)
B = set(B)

if len(A) == len(B):
    ans += len(A)
    if len(A&B) == len(A) and len(A)>0:
        ans -= 1
elif len(A)> len(B):
    ans += len(B)
else:
    ans += len(A)
print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def check(S):
    ans = 0
    for i in range(1,len(S)):
        if S[i-1] == 'A' and S[i] == 'B':
            ans += 1
    return ans

N = int(input())
s = []
a = 0
b = 0
ab = 0

ans = 0

for i in range(N):
    S = input()
    ans += check(S)
    if S[0] == 'B' or S[-1] == 'A':
        s.append(S)
        if S[0] == 'B' and S[-1] != 'A':
            b += 1
        elif S[0] != 'B' and S[-1] == 'A':
            a += 1
        else:
            ab += 1

if ab >= 2:
    ans += ab - 1
    ab = 1

ans += min(a,b)
abc = abs(a-b)
if abc >= 1:
    ans += 1

print(ans)

Failing test case input:
3
AD@B
\XBA
CBD

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
def check(S):
    ans = 0
    for i in range(1,len(S)):
        if S[i-1] == 'A' and S[i] == 'B':
            ans += 1
    return ans

N = int(input())
s = []
a = 0
b = 0
ab = 0

ans = 0

for i in range(N):
    S = input()
    ans += check(S)
    if S[0] == 'B' or S[-1] == 'A':
        s.append(S)
        if S[0] == 'B' and S[-1] != 'A':
            b += 1
        elif S[0] != 'B' and S[-1] == 'A':
            a += 1
        else:
            ab += 1

if ab == 0:
    ans += min(a,b)
else:
    if a == 0 and b == 0:
        ans += ab - 1
    else:
        ans += ab + min(a,b)

print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
s = []
for i in range(N):
    s.append((input()))

count = 0
count_BA = 0
count_A = 0
count_B = 0
for i in range(N):
    count += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        count_BA += 1
    elif s[i][0] == 'B':
        count_B += 1
    elif s[i][-1] == 'A':
        count_A += 1

if count_B != 0 and count_A != 0:
    print(count + count_BA - 1 + 1 + 1 + min(count_A, count_B) - 1)
elif count_B != 0 or count_A != 0:
    print(count + count_BA - 1 + 1)
else:
    print(count + count_BA - 1)


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
s = []
for i in range(N):
    s.append((input()))

count = 0
count_BA = 0
count_A = 0
count_B = 0
for i in range(N):
    count += s[i].count('AB')
    if s[i][0] == 'B' and s[i][-1] == 'A':
        count_BA += 1
    elif s[i][0] == 'B':
        count_B += 1
    elif s[i][-1] == 'A':
        count_A += 1

if count_B != 0 and count_A != 0:
    print(count + count_BA - 1 + 1 + 1 + min(count_A, count_B) - 1)
elif count_B != 0 or count_A != 0:
    print(count + count_BA - 1 + 1)
elif count_BA != 0:
    print(count + count_BA - 1)
else:
    print(count)
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n=int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ans += s.count('AB')
    s0 = s[0]
    s1 = s[-1]
    b += 1 if s0 == 'B' else 0
    a += 1 if s1 == 'A' else 0
    ba += 1 if s0 == 'B' and s1 == 'A' else 0

print(ans + min(a,b) if a!=ba else ans + min(a,b)-1)


Failing test case input:
3
BD?B
\XB@
DAC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
n=int(input())
ans = 0
a = 0
b = 0
ba = 0
for i in range(n):
    s = input()
    ans += s.count('AB')
    s0 = s[0]
    s1 = s[-1]
    b += 1 if s0 == 'B' else 0
    a += 1 if s1 == 'A' else 0
    ba += 1 if s0 == 'B' and s1 == 'A' else 0

print(ans + min(a,b)-1 if (ba > 0 and ba == a and ba == b) else ans + min(a,b))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0


for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[len(li)-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[len(li)-1] != 'A':
        firstb += 1
    if li[len(li)-1] == 'A' and li[0] == 'B':
        lasta += 1
        firstb += 1
        ab += 1


if lasta == firstb:
    cnt += lasta - ab
else:
    cnt += min(lasta, firstb)

print(cnt)


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):

import itertools

N = int(input())
li = []
lasta = 0
firstb = 0
ab = 0
cnt = 0



for i in range(N):
    s = input()
    cnt += s.count('AB')
    li = list(s)
    if li[-1] == 'A' and li[0] != 'B':
        lasta += 1
    if li[0] == 'B' and li[-1] != 'A':
        firstb += 1
    if li[-1] == 'A' and li[0] == 'B':
        ab += 1



if ab == 0:
    cnt += min(lasta,firstb)
elif ab != 0 and lasta + firstb > 0:
    cnt += ab + min(lasta, firstb)
elif ab != 0 and lasta + firstb == 0:
    cnt += ab - 1

print(cnt)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from re import findall


def main():
    N = int(input())
    num_full = 0
    begin_b = 0
    end_a = 0
    both = 0
    for i in range(N):
        s_i = input()
        num_full += len(findall('AB', s_i))
        if s_i[0] == 'B' and s_i[-1] == 'A':
            both += 1
        if s_i[0] == 'B':
            begin_b += 1
        if s_i[-1] == 'A':
            end_a += 1
    ans = num_full
    if begin_b == end_a:
        if begin_b == both:
            ans += begin_b - 1
        else:
            ans += begin_b
    else:
        ans += min(begin_b, end_a)
    print(ans)


if __name__ == '__main__':
    main()


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
from re import findall


def main():
    N = int(input())
    num_full = 0
    begin_b = 0
    end_a = 0
    both = 0
    for i in range(N):
        s_i = input()
        num_full += len(findall('AB', s_i))
        if s_i[0] == 'B' and s_i[-1] == 'A':
            both += 1
        if s_i[0] == 'B':
            begin_b += 1
        if s_i[-1] == 'A':
            end_a += 1
    ans = num_full
    if begin_b == end_a:
        if begin_b == both:
            ans += max(begin_b - 1, 0)
        else:
            ans += begin_b
    else:
        ans += min(begin_b, end_a)
    print(ans)


if __name__ == '__main__':
    main()

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
s = [input() for i in range(n)]

b = 0
a = 0
ba = 0

cnt = 0
for x in s:
    if x[0] == "B" and x[-1] == "A":
        ba += 1
    if x[0] == "B":
        b+=1
    if x[-1] == "A":
        a+=1
    cnt += x.count("AB")

cnt2 = 0
if ba == b and ba==a:
    cnt2 = ba-1
else:
    cnt2 = min(a,b)

print(cnt+cnt2)

Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
n = int(input())
s = [input() for i in range(n)]

b = 0
a = 0
ba = 0

cnt = 0
for x in s:
    if x[0] == "B" and x[-1] == "A":
        ba+=1
    if x[0] == "B":
        b+=1
    if x[-1] == "A":
        a+=1
    cnt += x.count("AB")

cnt2 = 0
if ba==b and ba==a and ba!=0:
    cnt2 = ba-1
else:
    cnt2 = min(a,b)

print(cnt+cnt2)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
b_start=0
a_last=0
both=0
ans=0
for i in range(n):
    s=input()
    ans+=s.count("AB")
    if s[0]=="B":
        if s[len(s)-1]=="A":
            both+=1
        else:
            b_start+=1
    elif s[len(s)-1]=="A":
        a_last+=1
diff=abs(b_start-a_last)
print(a_last, b_start, both, ans, diff)
ans+=both-1
if a_last>=1:
    ans+=1
    a_last-=1
if b_start>=1:
    ans+=1
    b_start-=1
ans+=min(a_last,b_start)
print(ans)











Failing test case input:
7
FBBZBAR
JOZ
AVUQHMB
BPB
ISV
MCMABAOBHZ
SZMEJMA

Expected output:
2


Actual output:
1 1 0 1 0
2


Fixed submission (verdict: Accepted):
n=int(input())
b_start=0
a_last=0
both=0
ans=0
for i in range(n):
    s=input()
    ans+=s.count("AB")
    if s[0]=="B":
        if s[len(s)-1]=="A":
            both+=1
        else:
            b_start+=1
    elif s[len(s)-1]=="A":
        a_last+=1
diff=abs(b_start-a_last)

if both>=1:
    ans+=both-1
    if a_last>=1:
        ans+=1
        a_last-=1
    if b_start>=1:
        ans+=1
        b_start-=1
ans+=min(a_last,b_start)
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
 
b = 0
a = 0
c = 0
count = 0
for i in range(N):
    s = input()
    count += len([1 for j in range(len(s)) if s[j:j + 2] == "AB"])
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[0] == "B" and s[-1] == "A":
        c += 1

if a == b and b == c:
    print(count+c-1)
else:
    print(count + min([a, b, N-1]))

Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
 
a = 0
b = 0
c = 0
count = 0
for i in range(N):
    s = input()
    count += len([1 for j in range(len(s)) if s[j:j+2] == "AB"])
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[0] == "B" and s[-1] == "A":
        c += 1

if a == b and b == c and c != 0:
    print(count + c - 1)
else:
    print(count + min([a, b, N-1]))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = [input() for i in range(N)]
ABcnt = 0
Acnt = 0
Bcnt = 0
for s in S:
    if s[0] == 'B':
        Bcnt += 1
    if s[-1] == 'A':
        Acnt += 1
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ABcnt += 1
ans = ABcnt + min(Acnt,Bcnt)
print(ans)


Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
S = [input() for i in range(N)]
ABcnt = 0
Acnt = 0
Bcnt = 0
BAcnt = 0
for s in S:
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'B':
            ABcnt += 1
    if s[0] == 'B' and s[-1] == 'A':
        BAcnt += 1
    elif s[0] == 'B':
        Bcnt += 1
    elif s[-1] == 'A':
        Acnt += 1

if Acnt == 0 and Bcnt == 0:
    ans = ABcnt + max(BAcnt-1,0)
else:
    ans = ABcnt + BAcnt + min(Acnt,Bcnt)

print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
c = 0
xa = 0
bx = 0
ba = 0
for _ in range(n):
  si = str(input())
  l = len(si)
  s1 = si[0]
  s2 = si[-1]
  if s1=="B":
    if s2=="A":
      ba += 1
    else:
      bx += 1
  elif s2 == "A":
    xa += 1
  for i in range(l-1):
    if si[i]=="A" and si[i+1]=="B":
      c += 1
print(c,ba,bx,xa)
if ba == 0:
  print(c+min(bx,xa))
  exit()
if bx == 0:
  if  xa == 0:
    print(ba-1+c)
    exit()
  else:
    print(ba+c)
    exit()
if xa == 0:
  print(ba+c)
  exit()
print(min(bx,xa)+ba+c)

Failing test case input:
3
ABCA
ZABX
BEA

Expected output:
3


Actual output:
2 1 0 1
3


Fixed submission (verdict: Accepted):
n = int(input())
c = 0
xa = 0
bx = 0
ba = 0
for _ in range(n):
  si = str(input())
  l = len(si)
  s1 = si[0]
  s2 = si[-1]
  if s1=="B":
    if s2=="A":
      ba += 1
    else:
      bx += 1
  elif s2 == "A":
    xa += 1
  for i in range(l-1):
    if si[i]=="A" and si[i+1]=="B":
      c += 1
if ba == 0:
  print(c+min(bx,xa))
  exit()
if bx == 0:
  if  xa == 0:
    print(ba-1+c)
    exit()
  else:
    print(ba+c)
    exit()
if xa == 0:
  print(ba+c)
  exit()
print(min(bx,xa)+ba+c)

Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n = int(input())

ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(a,b)
if min(a,b) == c:
    ans = ab + min(a,b) - 1
print(ans)


Failing test case input:
7
FBBYBAR
JOZ
BMHQUVA
BPA
SIV
MCMABAOBHZ
SZMEIMA

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):
n = int(input())
ab = 0
a = 0
b = 0
c = 0
for i in range(n):
    s = input()
    if s[-1] == "A":
        a += 1
    if s[0] == "B":
        b += 1
    if s[-1] == "A" and s[0] == "B":
        c += 1
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "B":
            ab += 1
ans = ab + min(a,b)
if a == b == c != 0:
    ans = ab + min(a,b) - 1
print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
s = []

result = 0
ABAB = 0
A = 0
B = 0

amari = 0
for i in range(N):
    s.append(input())

for S in s:
    AB = S.count("AB")
    result += AB
    if S[0] == "B" and S[-1] == "A":
        ABAB += 1
    elif S[0] == "B":
        B += 1
    elif S[-1] == "A":
        A += 1


if A >= B:
    onon = B
    amari = A - B
else:
    onon = A
    amari = B - A

if ABAB >= 1:
    onon = onon + ABAB - 1

if amari >= 1:
    onon = onon + 1

print(result + onon)


Failing test case input:
9
BWEPVCRWH
ZQNZYIJX
AVRAEB
Q@
TYMJIHDOX
KNMRMHKCA
AP
ZBAGVQ
PRGKSPUNA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
N = int(input())
s = []

result = 0
ABAB = 0
A = 0
B = 0
onon = 0

amari = 0
for i in range(N):
    s.append(input())

for S in s:
    AB = S.count("AB")
    result += AB
    if S[0] == "B" and S[-1] == "A":
        ABAB += 1
    elif S[0] == "B":
        B += 1
    elif S[-1] == "A":
        A += 1

if ABAB >= 1:
    onon = onon + ABAB - 1

if A >= 1 and ABAB >= 1 :
    onon += 1
    A -= 1
if B >= 1 and ABAB >= 1 :
    onon += 1
    B -= 1

if A >= 1 and B >= 1:
    if A >= B:
        onon += B
    else:
        onon += A


print(result + onon)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
c=0
d=0
a=0
k=0
for i in range(n):
    s=input()
    if "AB" in s:
        c+=s.count("AB")
    if s[0]=="B" and s[len(s)-1]=="A":
        d+=1
    if s[0]=="B" and s[len(s)-1]!="A":
        a+=1
    if s[len(s)-1]=="A" and s[0]!="B":
        k+=1
if a==0 and k==0:
    print(c+d-1)
elif a+k>0:
    print(c+min(d+a,d+k))
else:
    print(c+min(a,k))


Failing test case input:
3
AD@B
\XB@
DBC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
n=int(input())
c=0
d=0
a=0
k=0
for i in range(n):
    s=input()
    if "AB" in s:
        c+=s.count("AB")
    if s[0]=="B" and s[len(s)-1]=="A":
        d+=1
    if s[0]=="B" and s[len(s)-1]!="A":
        a+=1
    if s[len(s)-1]=="A" and s[0]!="B":
        k+=1
if a+k==0:
    print(max(c+d-1,c))
elif a+k>0:
    print(c+min(d+a,d+k))
else:
    print(c+min(a,k))
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    last_a = 0
    first_b = 0
    for _ in range(N):
        s = str(input().strip())
        for i in range(len(s)-1):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and last_a > 0:
            ans += 1
            last_a -= 1
            s = 'b' + s

        if s[-1] == 'A' and first_b > 0:
            ans += 1
            first_b -= 1
            s = s + 'a'

        if s[0] == 'B':
            first_b += 1

        if s[-1] == 'A':
            last_a += 1

    return ans


if __name__ == '__main__':
    print(main())


Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    ans = 0
    ba = 0  
    xa = 0  
    bx = 0  
    for _ in range(N):
        s = str(input().strip())

        
        for i in range(len(s)):
            if s[i:i+2] == 'AB':
                ans += 1

        if s[0] == 'B' and s[-1] == 'A':
            ba += 1
        elif s[-1] == 'A':
            xa += 1
        elif s[0] == 'B':
            bx += 1

    if ba == 0:
        
        
        ans += min(xa, bx)

    elif max(xa, bx) == 0:
        
        
        ans += ba - 1

    else:  
        
        
        
        
        
        
        ans += ba + min(xa, bx)

    return ans


if __name__ == '__main__':
    print(main())

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
s=[]
ans=0
a=0
b=0
ba=0
for i in range(n):
    s.append(input())
    
for i in range(n):
    flg=0
    if s[i][0]=="B":
        b+=1
    if s[i][-1]=="A":
        a+=1
        if s[i][0]=="B":
            ba+=1
            
    for j in range(len(s[i])):
        if s[i][j]=="B" and flg==1:
            ans+=1
            
        if s[i][j]=="A":
            flg=1
        else:
            flg=0
if ba==a and ba==b:
    ans+=(ba-1)
else:
    ans+=min(a,b)
print(ans)

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n=int(input())
s=[]
ans=0
a=0
b=0
ba=0
for i in range(n):
    s.append(input())
    
for i in range(n):
    flg=0
    if s[i][0]=="B":
        b+=1
    if s[i][-1]=="A":
        a+=1
        if s[i][0]=="B":
            ba+=1
            
    for j in range(len(s[i])):
        if s[i][j]=="B" and flg==1:
            ans+=1
            
        if s[i][j]=="A":
            flg=1
        else:
            flg=0
if ba==a and ba==b and ba!=0:
    ans+=(ba-1)
else:
    ans+=min(a,b)
print(ans)
Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n=int(input())
lb=0
ra=0
ba=0
ans=0

import re

for _ in range(n):
    s=input()

    ans+=s.count('AB')

    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        lb+=1
    elif s[-1]=='A':
        ra+=1

tmp=min(lb,ra,ba)
ans += 2*tmp

lb-=tmp
ra-=tmp
ba-=tmp
if lb<0:
    lb=0
if ra<0:
    ra=0
if ba<0:
    ba=0

if lb==0 and ra==0 and ba>0:
    ans+=ba-1
elif lb!=0 or ra!=0:
    ans+=min(lb,ra)
    ans+=min(lb,ba)
    ans+=min(ra,ba)


print(ans)

Failing test case input:
7
EBBYBAR
JOZ
BMHQUVA
BPA
SIV
MBMABAOBHZ
SZMEHMA

Expected output:
3


Actual output:
2


Fixed submission (verdict: Accepted):
n=int(input())
lb=0
ra=0
ba=0
ans=0

import re

for _ in range(n):
    s=input()

    ans+=s.count('AB')

    if s[0]=='B' and s[-1]=='A':
        ba+=1
    elif s[0]=='B':
        lb+=1
    elif s[-1]=='A':
        ra+=1

if ba>0:
    ans+=ba-1
    ba=1

tmp=min(lb,ra,ba)
ans += 2*tmp

lb-=tmp
ra-=tmp
ba-=tmp

if lb<0:
    lb=0
if ra<0:
    ra=0
if ba<0:
    ba=0

if lb!=0 or ra!=0:
    ans+=min(lb,ra)
    ans+=min(lb,ba)
    ans+=min(ra,ba)


print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
string=[input() for i in range(n)]

base_ab=0
last_a=0
first_b=0
all_ba=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
    if s[0]=="B" and s[-1]=="A":
        all_ba+=1
if min(last_a,first_b) == all_ba:
    print(base_ab+min(last_a,first_b)-1)
else:    
    print(base_ab+min(last_a,first_b))


Failing test case input:
7
FBBYBAR
JOZ
AVUQHMB
BPA
ISV
MCMABAOBHZ
SZMEIMA

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n=int(input())
string=[input() for i in range(n)]

base_ab=0
last_a=0
first_b=0
all_ba=0
for s in string:
    base_ab+=s.count("AB")
    if s[-1]=="A":
        last_a+=1
    if s[0]=="B":
        first_b+=1
    if s[0]=="B" and s[-1]=="A":
        all_ba+=1
if last_a-all_ba==0 and first_b-all_ba==0 and all_ba>0:
    print(base_ab+min(last_a,first_b)-1)
else:    
    print(base_ab+min(last_a,first_b))
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
s = [0]*n

ans = 0
initb = 0
lasta = 0
add = 0

for i in range(n):
    s[i] = input()
    st = s[i]

    if st[0] == "B":
        initb += 1

    if st[len(st)-1] == "A":
        lasta += 1

    for j in range(len(st)):

        if st[j] == "A":

            if j == len(st) - 1:
                break
            if st[j+1] == "B":
                ans += 1

if initb <= lasta:
    add = initb
else:
    add = lasta

ans = ans + add

print(ans)

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n = int(input())
s = [0]*n

ans = 0
initb = 0
lasta = 0
add = 0
juuhuku = 0
flag = 0

for i in range(n):
    s[i] = input()
    st = s[i]

    if st[0] == "B":
        initb += 1

        if st[len(st)-1] == "A":
            lasta += 1
            juuhuku += 1
        else:
            flag += 1

    elif st[len(st)-1] == "A":
        lasta += 1
        flag += 1

    for j in range(len(st)):

        if st[j] == "A":

            if j == len(st) - 1:
                break
            if st[j+1] == "B":
                ans += 1

if initb <= lasta:
    add = initb
else:
    add = lasta

if juuhuku > 0:

    if flag > 0:
        ans = ans + add 
    else:
        ans = ans + add -1

else:
    ans = ans + add

print(ans)

Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys,math,collections,itertools
input = sys.stdin.readline

N = int(input())
Aend = []
Bstart = []
Ae_Bs = []
cnt = 0

for i in range(N):
    S = input().rstrip()
    if S[0]=='B' and S[-1] =='A':
        Ae_Bs.append(S)
    elif S[0] == 'B':
        print(S)
        Bstart.append(S)
    elif S[-1] == 'A':
        Aend.append(S)
    cnt += S.count('AB')

lAe_Bs = len(Ae_Bs)
lBstart = len(Bstart)
lAend = len(Aend)

if lAe_Bs >0:
    cnt += lAe_Bs -1
    
cnt += min(lBstart,lAend)
cnt += (abs(lBstart-lAend)>0 and lAe_Bs >0)

print(cnt)


Failing test case input:
9
BWEPVCRWH
ZQOZYIJX
AVRAEB
Q@
TYMJIHDOX
JNMRMHKCA
AP
ZBAGVQ
PRGKSPUNA

Expected output:
1


Actual output:
BWEPVCRWH
1


Fixed submission (verdict: Accepted):
import sys,math,collections,itertools
input = sys.stdin.readline

N = int(input())
Aend = []
Bstart = []
Ae_Bs = []
cnt = 0

for i in range(N):
    S = input().rstrip()
    if S[0]=='B' and S[-1] =='A':
        Ae_Bs.append(S)
    elif S[0] == 'B':
        Bstart.append(S)
    elif S[-1] == 'A':
        Aend.append(S)
    cnt += S.count('AB')

lAe_Bs = len(Ae_Bs)
lBstart = len(Bstart)
lAend = len(Aend)

if lAe_Bs >0:
    cnt += lAe_Bs -1
    
if lAe_Bs > 0 and lAend >0:
    cnt += 1
    lAend -=1
if lAe_Bs > 0 and lBstart>0:
    cnt +=1
    lBstart -=1

cnt += min(lAend,lBstart)

print(cnt)
Problem: p03049
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
n=int(input())
a,b,ans=0,0,0
for _ in range(n):
    s=input()
    for i in range(len(s)-1):
        if s[i]=="A" and s[i+1]=="B": ans+=1
    if s[0]=="B": b+=1
    if s[-1]=="A": a+=1
print(ans+min(a,b))

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n=int(input())
a,b,ba,ans=0,0,0,0
for _ in range(n):
    s=input()
    for i in range(len(s)-1):
        if s[i]=="A" and s[i+1]=="B": ans+=1
    if s[0]=="B": b+=1
    if s[-1]=="A": a+=1
    if s[0]=="B" and s[-1]=="A": ba+=1
if ba==b==a!=0: ans-=1 
print(ans+min(a,b))

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward
MAX = N-1
if dbl>0:
    ans += dbl-1
    MAX -= (dbl-1)
if top >0:
    ans +=1
    top -=1
    MAX -=1
if fin >0:
    ans +=1
    fin -=1
    MAX -=1
ans += min(top,fin,MAX)
print(ans)

Failing test case input:
3
ABCA
XBAZ
BAE

Expected output:
2


Actual output:
3


Fixed submission (verdict: Accepted):
N = int(input())
inward = 0
top = 0
fin = 0
dbl = 0

for _ in range(N):
    s = input()
    n = len(s)
    if s[0] == 'B' and s[n-1] == 'A':
        dbl += 1
    elif s[0] == 'B' and s[n-1] != 'A':
        top += 1
    elif s[0] != 'B' and s[n-1] == 'A':
        fin += 1
    for i in range(n-1):
        if s[i] == 'A' and s[i+1] == 'B':
            inward += 1

ans = inward
MAX = N-1
if dbl>0:
    ans += dbl-1
    MAX -= (dbl-1)
    if top >0:
        ans +=1
        top -=1
        MAX -=1
    if fin >0:
        ans +=1
        fin -=1
        MAX -=1
ans += min(top,fin,MAX)
print(ans)

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

n = int(input())
s = []
a = 0
b = 0
for i in range(n):
    s.append(input())
    if s[i][0] == 'B':
        b += 1
    if s[i][-1] == 'A':
        a += 1

ans = 0
for i in range(n):
    tmp = list(s[i])
    for j in range(len(tmp)-1):
        if tmp[j] == 'A' and tmp[j+1] == 'B':
            ans += 1

ans += min(a, b)
print(ans)

Failing test case input:
3
BDAA
AAX[
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):

n = int(input())
s = []
a = 0
b = 0
c = 0
for i in range(n):
    s.append(input())
    if s[i][0] == 'B' and s[i][-1] != 'A':
        b += 1
    if s[i][-1] == 'A' and s[i][0] != 'B':
        a += 1
    if s[i][0] == 'B' and s[i][-1] =='A':
        c += 1

ans = 0
for i in range(n):
    tmp = list(s[i])
    for j in range(len(tmp)-1):
        if tmp[j] == 'A' and tmp[j+1] == 'B':
            ans += 1

if c == 0:
    ans += min(a, b)
elif c != 0 and a+b != 0:
    ans += c + min(a, b)
else:
    ans += c - 1 
print(ans)
Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
S = [input() for _ in range(N)]

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    elif s[0] == 'B':
        headB += 1
    elif s[-1] == 'A':
        tailA += 1

ans = min(tailA, headB)
tailA -= ans
headB -= ans
K = min(max(tailA, headB), same)
ans += K
same -= K
ans += max(same - 1, 0)

if tailA >= 1:
    ans += 1

print(ans + inside)

Failing test case input:
3
@DBA
ZAAX
AEB

Expected output:
0


Actual output:
1


Fixed submission (verdict: Accepted):
N = int(input())
S = [input() for _ in range(N)]

tailA = 0
headB = 0
same = 0
inside = 0
for s in S:
    inside += s.count('AB')
    if s[0] == 'B' and s[-1] == 'A':
        same += 1
    else:
        if s[0] == 'B':
            headB += 1
        if s[-1] == 'A':
            tailA += 1

ans = 0

if same >= 1:
    if tailA >= 1:
        ans += same
        tailA -= 1
    else:
        ans += same - 1

    if headB >= 1:
        ans += 1
        headB -= 1

ans += max(min(tailA, headB), 0)
print(ans + inside)

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
S = [input() for i in range(n)]

ans = 0
a = 0
b = 0
c = 0
for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j:j+2] == "AB":
            ans += 1
    if S[i][-1] == "A":
        a += 1
    if S[i][0] == "B":
        b += 1
    if S[i][-1] == "A" and S[i][0] == "B":
        c += 1

ans += min(a, b)
if a == c and a == b:
    ans -= 1

print(ans)

Failing test case input:
3
AABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n = int(input())
S = [input() for i in range(n)]

ans = 0
a = 0
b = 0
c = 0
for i in range(n):
    for j in range(len(S[i]) - 1):
        if S[i][j:j+2] == "AB":
            ans += 1
    if S[i][-1] == "A":
        a += 1
    if S[i][0] == "B":
        b += 1
    if S[i][-1] == "A" and S[i][0] == "B":
        c += 1

ans += min(a, b)
if a == c and a == b and a > 0:
    ans -= 1

print(ans)

Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n=int(input())
s=[input() for _ in range(n)]
ans=sum([si.count('AB') for si in s])
endA=len([si for si in s if si[-1]=='A'])
startB=len([si for si in s if si[0]=='B'])
AB=len([si for si in s if si[0]=='B' and si[-1]=='A'])
if AB==endA and AB==startB:
      print(max(0,ans+AB-1))
else:
      print(ans+min(endA,startB))


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):
n=int(input())
s=[input() for _ in range(n)]
ans=sum([si.count('AB') for si in s])
endA=len([si for si in s if si[-1]=='A'])
startB=len([si for si in s if si[0]=='B'])
AB=len([si for si in s if si[0]=='B' and si[-1]=='A'])
if endA*startB==0:
      print(ans)
elif AB==endA and AB==startB:
      print(ans+AB-1)
else:
      print(ans+min(endA,startB))
Problem: p03049
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n = int(input())
s = [input() for _ in range(n)]
ab = 0
last_a = 0
first_b = 0
for i in range(n):
  if s[i][0] == "B":
    first_b += 1
  if s[i][-1] == "A":
    last_a += 1
  ab += s[i].count("AB")
if last_a == n and first_b == n:
  print(ab+n-1)
else:
  print(ab+min(last_a,first_b))

Failing test case input:
3
BDAA
[AAX
BDA

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
n = int(input())
s = [input() for _ in range(n)]
ab = 0
ba = 0
last_a = 0
first_b = 0
for i in range(n):
  if s[i][0] == "B" and s[i][-1] == "A":
    ba += 1
  elif s[i][-1] == "A":
    last_a += 1
  elif s[i][0] == "B":
    first_b += 1
  ab += s[i].count("AB")
if last_a == 0 and first_b == 0:
  print(ab+ba-1) if ba != 0 else print(ab)
else:
  print(ab+ba+min(last_a,first_b))

Problem: p03049
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


N = int(input())
S = [input() for i in range(N)]

ans = sum([s.count('AB') for s in S])
count_A = 0
count_B = 0
count_AB = 0
for s in S:
    if s[0] == 'B':
        count_B += 1
        if s[-1] == 'A':
            count_AB += 1
    if s[-1] == 'A':
        count_A += 1

if max(count_A, count_B) == count_AB:
    ans += count_AB-1
else:
    ans += min(count_A, count_B)

print(ans)


Failing test case input:
3
@ABD
ZABX
AEB

Expected output:
2


Actual output:
1


Fixed submission (verdict: Accepted):


N = int(input())
S = [input() for i in range(N)]

ans = sum([s.count('AB') for s in S])
count_A = 0
count_B = 0
count_AB = 0
for s in S:
    if s[0] == 'B':
        count_B += 1
        if s[-1] == 'A':
            count_AB += 1
    if s[-1] == 'A':
        count_A += 1

if max(count_A, count_B) != 0 and max(count_A, count_B) == count_AB:
    ans += count_AB-1
else:
    ans += min(count_A, count_B)

print(ans)
Problem: p03891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
a = int(input())
b = int(input())
c = int(input())

l = [
    [a, b, -a-b],
    [-2*a-b-2*c, c, 2*a+b+c],
    [a+b+2*c, -b-c, -a-c]
]

for i in l:
    print(*i)


Failing test case input:
8
-10
5

Expected output:
8 -10 17
14 5 -4
-7 20 2


Actual output:
8 -10 2
-16 5 11
8 5 -13


Fixed submission (verdict: Accepted):
a = int(input())
b = int(input())
c = int(input())
s = 3 * c

l = [
    [a, b, s-a-b],
    [2*s-2*a-b-2*c, c, 2*a+b+c-s],
    [a+b+2*c-s, s-b-c, s-a-c]
]

for i in l:
    print(*i)

Problem: p03891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
a=int(input())
b=int(input())
c=int(input())

li1=[a,b,3*c-a-b]
li2=[4*c-2*a-b,2*c-b,2*a+b-2*c]
li3=[a+b-c,2*c-b,2*c-a]
print(*li1)
print(*li2)
print(*li3)

Failing test case input:
1
-7
5

Expected output:
1 -7 21
25 5 -15
-11 17 9


Actual output:
1 -7 21
25 17 -15
-11 17 9


Fixed submission (verdict: Accepted):
a=int(input())
b=int(input())
c=int(input())

li1=[a,b,3*c-a-b]
li2=[4*c-2*a-b,c,2*a+b-2*c]
li3=[a+b-c,2*c-b,2*c-a]
print(*li1)
print(*li2)
print(*li3)

Problem: p03891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

import itertools
a = int(input())
b = int(input())
c = int(input())
for k in itertools.count():
    f = [
            [             a,      b,     k-a-b ],
            [ 2*k-2*a-b-2*c,      c, 2*a+b+c-k ],
            [     a+b+2*c-k,  k-b-c,     k-a-c ] ]
    print(*f[0])
    print(*f[1])
    print(*f[2])
    break


Failing test case input:
1
4
3

Expected output:
1 4 4
6 3 0
2 2 5


Actual output:
1 4 -5
-12 3 9
11 -7 -4


Fixed submission (verdict: Accepted):

import itertools
a = int(input())
b = int(input())
c = int(input())
k = 3*c
f = [
        [             a,      b,     k-a-b ],
        [ 2*k-2*a-b-2*c,      c, 2*a+b+c-k ],
        [     a+b+2*c-k,  k-b-c,     k-a-c ] ]
assert f[0][2] + f[1][1] + f[2][0] == k
for y in range(3):
    print(*f[y])

Problem: p03891
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
a=int(input())
b=int(input())
c=int(input())
print(a,b,c)
print(a,b,-a-b+3*c)
print(-2*a-b+4*c,c,2*a+b-2*c)
print(a+b-c,-b+2*c,-a+2*c)

Failing test case input:
8
-3
5

Expected output:
8 -3 10
7 5 3
0 13 2


Actual output:
8 -3 5
8 -3 10
7 5 3
0 13 2


Fixed submission (verdict: Accepted):
a=int(input())
b=int(input())
c=int(input())
print(a,b,-a-b+3*c)
print(-2*a-b+4*c,c,2*a+b-2*c)
print(a+b-c,-b+2*c,-a+2*c)
Problem: p03229
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from collections import deque
from copy import copy
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
A2 = copy(A)
d = deque()
d.append(A.popleft())
while True:
    try:
        d.append(A.pop())
        d.appendleft(A.pop())
        d.append(A.popleft())
        d.appendleft(A.popleft())
    except IndexError:
        break
ans = 0
for i in range(N-1):
    ans += abs(d[i]-d[i+1])
A = A2
d = deque()
d.append(A.pop())
while True:
    try:
        d.append(A.popleft())
        d.appendleft(A.popleft())
        d.append(A.pop())
        d.appendleft(A.pop())
    except IndexError:
        break
n = 0
for i in range(N-1):
    n += abs(d[i]-d[i-1])
print(max(ans, n))


Failing test case input:
3
-9
27
-3

Expected output:
66


Actual output:
42


Fixed submission (verdict: Accepted):
from collections import deque
from copy import copy
N = int(input())
A = deque(sorted([int(input()) for _ in range(N)]))
A2 = copy(A)
d = deque()
d.append(A.popleft())
while True:
    try:
        d.append(A.pop())
        d.appendleft(A.pop())
        d.append(A.popleft())
        d.appendleft(A.popleft())
    except IndexError:
        break
ans = 0
for i in range(N-1):
    ans += abs(d[i]-d[i+1])
A = A2
d = deque()
d.append(A.pop())
while True:
    try:
        d.append(A.popleft())
        d.appendleft(A.popleft())
        d.append(A.pop())
        d.appendleft(A.pop())
    except IndexError:
        break
n = 0
for i in range(N-1):
    n += abs(d[i]-d[i+1])
print(max(ans, n))
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def resolve():
    
    import collections
    N = int(input())
    A_list = [int(input()) for _ in range(N)]
    A_odered = collections.deque(sorted(A_list))
    res_list = collections.deque([A_odered.popleft()])

    while len(A_odered) >= 1:
        res_list.append(A_odered.pop())
        if len(A_odered) >=1:
            res_list.appendleft(A_odered.pop())
        if len(A_odered) >=1:
            res_list.append(A_odered.popleft())
        if len(A_odered) >=1:
            res_list.appendleft(A_odered.popleft())

    res = 0
    prev = 0
    for i in res_list:
        if prev == 0:
            pass
        else:
            res += abs(i - prev)

        prev = i
        
    print(res)
if __name__ == "__main__":
    resolve()


Failing test case input:
6
6
1
7
0
1
9

Expected output:
35


Actual output:
26


Fixed submission (verdict: Accepted):
def resolve():
    
    import collections
    N = int(input())
    A_list = [int(input()) for _ in range(N)]
    A_odered = collections.deque(sorted(A_list))
    res_list = collections.deque([A_odered.popleft()])
    res_list.appendleft(A_odered.pop())
    res = abs(res_list[0] - res_list[1])

    while len(A_odered) >= 1:
        temp_def1 = abs(res_list[0] - A_odered[-1])
        temp_def2 = abs(res_list[-1] - A_odered[-1])
        temp_def3 = abs(res_list[0] - A_odered[0])
        temp_def4 = abs(res_list[-1] - A_odered[0])

        max_deff = max(temp_def1, temp_def2, temp_def3, temp_def4)
        if temp_def1 == max_deff:
            res += temp_def1
            res_list.appendleft(A_odered.pop())
        elif temp_def2 == max_deff:

            res += temp_def2
            res_list.append(A_odered.pop())
        elif temp_def3 == max_deff:
            res += temp_def3
            res_list.appendleft(A_odered.popleft())
        else:
            res += temp_def4
            res_list.append(A_odered.popleft())
    
    print(res)
if __name__ == "__main__":
    resolve()

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import collections
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A.sort()

s = collections.deque([A[0]])
ans = 0
i = 1
j = N-1
while i <= j:
    if len(s) % 2:
        x = A[j]
        j -= 1
    else:
        x = A[i]
        i += 1
    if abs(s[0]-x) > abs(s[-1]-x):
        s.appendleft(x)
    else:
        s.append(x)
assert len(s) == N
ans = 0
for i in range(1, N):
    ans += abs(s[i] - s[i-1])
print(ans)


Failing test case input:
6
6
1
4
1
5
9

Expected output:
27


Actual output:
25


Fixed submission (verdict: Accepted):
import collections
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
A.sort()

s = collections.deque([A[0]])
i = 1
j = N-1
while i <= j:
    xl = max(abs(s[0]-A[j]), abs(s[-1]-A[j]))
    xs = max(abs(s[0]-A[i]), abs(s[-1]-A[i]))
    if xl > xs:
        x = A[j]
        j -= 1
    else:
        x = A[i]
        i += 1
    if abs(s[0]-x) > abs(s[-1]-x):
        s.appendleft(x)
    else:
        s.append(x)
assert len(s) == N
ans = 0
for i in range(1, N):
    ans += abs(s[i] - s[i-1])
print(ans)
Problem: p03229
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(int(readline()) for _ in range(N))
    A.sort(reverse=True)
    B = []
    l = 0
    r = -1
    for i in range(N):
        if i % 2 == 0:
            B.append(A[l])
            l += 1
        else:
            B.append(A[r])
            r -= 1
    ans = 0
    for i in range(N - 2):
        ans += abs(B[i] - B[i + 1])
    ans += max(abs(B[0] - B[-1]), abs(B[-2] - B[-1]))
    print(ans)


if __name__ == '__main__':
    main()


Failing test case input:
5
156
-1
0
-1
0

Expected output:
316


Actual output:
315


Fixed submission (verdict: Accepted):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(int(readline()) for _ in range(N))
    A.sort(reverse=True)
    B = []
    C = []
    lb = 0
    rb = -1
    lc = 0
    rc = -1
    for i in range(N):
        if i % 2 == 0:
            B.append(A[lb])
            lb += 1
            C.append(A[rc])
            rc -= 1
        else:
            B.append(A[rb])
            rb -= 1
            C.append(A[lc])
            lc += 1
    ansb = 0
    for i in range(N - 2):
        ansb += abs(B[i] - B[i + 1])
    ansb += max(abs(B[0] - B[-1]), abs(B[-2] - B[-1]))

    ansc = 0
    for i in range(N - 2):
        ansc += abs(C[i] - C[i + 1])
    ansc += max(abs(C[0] - C[-1]), abs(C[-2] - C[-1]))


    print(max(ansb,ansc))


if __name__ == '__main__':
    main()
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def resolve():
    
    import collections
    N = int(input())
    A_list = [int(input()) for _ in range(N)]
    A_odered = collections.deque(sorted(A_list))
    res_list = collections.deque([A_odered.popleft()])
    res_list.appendleft(A_odered.pop())
    res = abs(res_list[0] - res_list[1])

    while len(A_odered) >= 1:
        temp_def1 = abs(res_list[0] - A_odered[0])
        temp_def2 = abs(res_list[-1] - A_odered[-1])

        if temp_def1 >= temp_def2:
            add_num = A_odered.popleft()
            res += temp_def1
            res_list.appendleft(add_num)
        else:
            add_num = A_odered.pop()
            res +=temp_def2
            res_list.append(add_num)
    
    print(res)
if __name__ == "__main__":
    resolve()


Failing test case input:
6
6
1
7
1
1
9

Expected output:
33


Actual output:
28


Fixed submission (verdict: Accepted):
def resolve():
    
    import collections
    N = int(input())
    A_list = [int(input()) for _ in range(N)]
    A_odered = collections.deque(sorted(A_list))
    res_list = collections.deque([A_odered.popleft()])
    res_list.appendleft(A_odered.pop())
    res = abs(res_list[0] - res_list[1])

    while len(A_odered) >= 1:
        temp_def1 = abs(res_list[0] - A_odered[-1])
        temp_def2 = abs(res_list[-1] - A_odered[-1])
        temp_def3 = abs(res_list[0] - A_odered[0])
        temp_def4 = abs(res_list[-1] - A_odered[0])

        max_deff = max(temp_def1, temp_def2, temp_def3, temp_def4)
        if temp_def1 == max_deff:
            res += temp_def1
            res_list.appendleft(A_odered.pop())
        elif temp_def2 == max_deff:

            res += temp_def2
            res_list.append(A_odered.pop())
        elif temp_def3 == max_deff:
            res += temp_def3
            res_list.appendleft(A_odered.popleft())
        else:
            res += temp_def4
            res_list.append(A_odered.popleft())
    
    print(res)
if __name__ == "__main__":
    resolve()
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Failing test case input:
5
46
-1
-2
-2
-1

Expected output:
98


Actual output:
97


Fixed submission (verdict: Accepted):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

if not side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

answer = 0
a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque

N = int(input())
a = [int(input()) for _ in range(N)]

a.sort()
dqa = deque(a[1:])

dq = deque([a[0]])
cnt = 0
while len(dq) < N:
    if cnt == 0:
        q = dqa.pop()
        dq.append(q)
    elif cnt == 1:
        q = dqa.pop()
        dq.appendleft(q)
    elif cnt == 2:
        q = dqa.popleft()
        dq.append(q)
    else:
        q = dqa.popleft()
        dq.appendleft(q)
        cnt = -1
    cnt += 1

ans = 0
for i in range(1, N):
    ans += abs(dq[i] - dq[i-1])
print(ans)

Failing test case input:
5
75
-2
1
-2
0

Expected output:
159


Actual output:
158


Fixed submission (verdict: Accepted):
from collections import deque

N = int(input())
a = [int(input()) for _ in range(N)]

a.sort()
def calc(m):
    if m == 1:
        dqa = deque(a[1:])
        dq = deque([a[0]])
        cnt = 0
    else: 
        dqa = deque(a[:N-1])
        dq = deque([a[-1]])
        cnt = 2

    while len(dq) < N:
        if cnt == 0:
            q = dqa.pop()
            dq.append(q)
        elif cnt == 1:
            q = dqa.pop()
            dq.appendleft(q)
        elif cnt == 2:
            q = dqa.popleft()
            dq.append(q)
        else:
            q = dqa.popleft()
            dq.appendleft(q)
            cnt = -1
        cnt += 1

    ans = 0
    for i in range(1, N):
        ans += abs(dq[i] - dq[i-1])
    return ans
        
print(max(calc(0), calc(1)))