Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
def main():
    N = int(input())
    A = list(map(int, input().split()))

    c = [0] * (100000 + 1)
    for ai in A:
        c[ai+1] += 1
        c[ai] += 1
        c[ai+2] += 1

    print(max(c))

if __name__ == "__main__":
    main()

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

    c = [0] * (100000 + 1)
    for ai in A:
        c[ai] += 1

    ans = 0
    for i in range(len(c) - 2):
        ans = max(ans, c[i] + c[i+1] + c[i+2])
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    X = int(input())
    # A = list(map(int, input().split()))
    A = input().split()

    b = []
    for ai in A:
        b.append(ai)
        b = b[::-1]
        # print(b)

    print(" ".join(b))

if __name__ == "__main__":
    main()

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

    # b = []
    # for ai in A:
    #     b.append(ai)
    #     b = b[::-1]
    #     # print(b)
    # print(" ".join(b))

    b = []
    for i in range(N - 1, -1, -2):
        b.append(A[i])
    
    if N % 2 == 0:
        for i in range(0, N, 2):
            b.append(A[i])

    else:
        for i in range(1, N, 2):
            b.append(A[i])

    print(" ".join(b))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

ans = 1
for i in range(1, n + 1):
    ans = ans * i

ans = ans % (10 ** 9 + 7)
print(ans)


Fixed submission:
n = int(input())

ans = 1
for i in range(1, n + 1):
    ans = (ans * i) % (10 ** 9 + 7)

# ans = ans % (10 ** 9 + 7)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
K, S = map(int, input().split())

# a = []
ans = 0
for i in range(K + 1):
    for j in range(i + 1):
        for k in range(j + 1):
            # print(i, j, k)
            if i + j + k == S:
                # print(i, j, k)
                # tmp = [i, j, k]
                # a.append(tmp)
                if i == j and j == k:
                    ans += 1
                elif i == j or j == k:
                    ans += 3
                else:
                    ans += 6

print(ans)


Fixed submission:
K, S = map(int, input().split())

# a = []
ans = 0
for i in range(K + 1):
    for j in range(i + 1):
        k = S - i - j
        if k >= 0 and k <= j:
            # print(i, j, k)
            if i == j and j == k:
                ans += 1
            elif i == j or j == k:
                ans += 3
            else:
                ans += 6

print(ans)

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

a = []
ans = 0
for i in range(K + 1):
    for j in range(i + 1):
        for k in range(j + 1):
            # print(i, j, k)
            if i + j + k == S:
                tmp = [i, j, k]
                a.append(tmp)

                length = len(set(tmp))
                if length == 3:
                    ans += 6
                elif length == 2:
                    ans += 3
                else:
                    ans += 1

print(ans)


Fixed submission:
K, S = map(int, input().split())

# a = []
ans = 0
for i in range(K + 1):
    for j in range(i + 1):
        k = S - i - j
        if k >= 0 and k <= j:
            # print(i, j, k)
            if i == j and j == k:
                ans += 1
            elif i == j or j == k:
                ans += 3
            else:
                ans += 6

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
K, S = map(int, input().split())
# print(K, S)

# 三重ループ？
# x,y,z の最大値はK

ans = 0
for i in range(K + 1):
    for j in range(K + 1):
        for k in range(K + 1):
            if i + j + k == S:
                ans += 1

print(ans)


Fixed submission:
K, S = map(int, input().split())

# a = []
ans = 0
for i in range(K + 1):
    for j in range(i + 1):
        k = S - i - j
        if k >= 0 and k <= j:
            # print(i, j, k)
            if i == j and j == k:
                ans += 1
            elif i == j or j == k:
                ans += 3
            else:
                ans += 6

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def main():
    S = input()
    if len(set(S)) == 1:
        print(0)
    else:
        ans = 0
        while len(set(S)) != 1:
            if S[-1] == 'B':
                x = 'W'
            else:
                x = 'B'

            S = S + x
            S = S[::-1]
            new_s = x
            flag = False
            for si in S[1:]:
                if not flag and si != x:
                    new_s += x
                elif not flag and si == x:
                    new_s += x
                    flag = True
                else:
                    new_s += si
            S = new_s
            ans += 1
            # print(S)
        print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    S = input()
    if len(set(S)) == 1:
        print(0)
    else:
        s2 = ''
        prev = ''
        for si in S:
            if si == prev:
                pass
            else:
                s2 += si
            prev = si
        
        print(len(s2) - 1)


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
S = input()

c = []
f = []

for i, si in enumerate(S):
    if si == 'C':
        c.append(i)
    elif si == 'F':
        f.append(i)

if min(c) < max(f):
    print('Yes')
else:
    print('No')

Fixed submission:
S = input()

C = False
# F = False
ans = False
for si in S:
    if si == 'C':
        C = True
    if C and si == 'F':
        ans = True

if ans:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
def main():
    S = input()
    lens = len(S)

    patterns = []
    ans = 0
    for i in range(2 ** (lens-1)):
        x = format(i, 'b').zfill(lens-1)

        stmt = S[0]
        for i, xi in enumerate(x):
            if xi == '1':
                stmt += '+'
            stmt += S[i+1]
        ans += eval(stmt)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
from itertools import combinations

def main():
    S = input()
    n = len(S)
    a = [i for i in range(1, n)]
    ans = []
    for i in range(n):
        if i == 0:
            ans.append(int(S))
        else:
            k = list(combinations(a, i))
            for ki in k:  # ki: (1, 3)
                r = 0
                t = 0  # 一時記憶
                for j, sj in enumerate(S):
                    if j in ki:
                        r += t
                        t = int(sj)
                    else:
                        t = t * 10 + int(sj)
                r += t
                ans.append(r)

    # print(ans)
    print(sum(ans))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
from itertools import combinations

def main():
    S = input()
    lens = len(S)

    patterns = []
    ans = 0
    for i in range(2 ** (lens-1)):
        x = format(i, 'b').zfill(lens-1)

        stmt = S[0]
        for i, xi in enumerate(x):
            if xi == '1':
                stmt += '+'
            stmt += S[i+1]
        ans += eval(stmt)
    print(ans)



if __name__ == "__main__":
    main()

Fixed submission:
from itertools import combinations

def main():
    S = input()
    n = len(S)
    a = [i for i in range(1, n)]
    ans = []
    for i in range(n):
        if i == 0:
            ans.append(int(S))
        else:
            k = list(combinations(a, i))
            for ki in k:  # ki: (1, 3)
                r = 0
                t = 0  # 一時記憶
                for j, sj in enumerate(S):
                    if j in ki:
                        r += t
                        t = int(sj)
                    else:
                        t = t * 10 + int(sj)
                r += t
                ans.append(r)

    # print(ans)
    print(sum(ans))


if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
name = input()
print(name(:2))

Fixed submission:
name = input()
print(name[:3])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
mul = 1
for i in range(N):
  mul = mul * A[i]
if mul>10**18:
  print(-1)
else:
  print(mul)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
if 0 in A:
  mul = 0
else:
  mul = 1
  for i in range(N):
    mul = mul * A[i]
    if mul > 10**18:
      break
if mul>10**18:
  print(-1)
else:
  print(mul)
Buggy python submission (verdict: Runtime Error):
N = int(input())

ls = N%10

if ls == 2,4,5,7,9:
  print("hon")
  
elif ls == 0,1,6,8:
  print("pon")
  
else:
  print("bon")

Fixed submission:
N = int(input())
 
mod = N % 10
 
if mod in [2,4,5,7,9]:
  print("hon")
  
elif mod in [0,1,6,8]:
  print("pon")
  
else:
  print("bon")
Buggy python submission (verdict: Runtime Error):
K = int(input())
S = input()

if len(S) <= K:
  print(S)
  
else:
  print("{}...".format(S[0,K]))

Fixed submission:
K = int(input())
S = input()
 
if len(S) <= K:
  print(S)
  
else:
  print("{}...".format(S[:K]))
Buggy python submission (verdict: Runtime Error):
A = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
N = int(input())
print(A[N])

Fixed submission:
A = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
n = int(input())
print(A[n-1])
Buggy python submission (verdict: Runtime Error):
a,b,c=input().split()
d=a*b
if d<=c:
  print(d)
else:
  print(c)

Fixed submission:
a,b,c=input().split()
a1=int(a)
b1=int(b)
c1=int(c)
d=a1*b1
if d<=c1:
  print(d)
else:
  print(c1)
Buggy python submission (verdict: Runtime Error):
N = int(inut())
if N<105:
  print(0)
elif N<135:
  print(1)
elif N<165:
  print(2)
elif N<189:
  print(3)
elif N<195:
  print(4)
else:
  print(5)

Fixed submission:
N = int(input())
if N<105:
  print(0)
elif N<135:
  print(1)
elif N<165:
  print(2)
elif N<189:
  print(3)
elif N<195:
  print(4)
else:
  print(5)
Buggy python submission (verdict: Runtime Error):
nlist = list(map(int,input.split()))

if abs(nlist[0] - nlist[1]) < abs(nlist[0] - nlist[1]) :
	print("A")
else: 
	print("B")

Fixed submission:
def ABS(a,b):
  c = a - b
  if c < 0:
    c = c * -1
  return c

str = input()
nli = list(map(int,str.split()))

if ABS(nli[0],nli[1]) < ABS(nli[0],nli[2]):
  print("A")
else:
  print("B")
Buggy python submission (verdict: Time Limit Exceeded):
P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449,
     457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]


def prime_factors(N):
    ret = []
    for p in P:
        while N % p == 0:
            ret.append(p)
            N //= p
    if N > 1:
        ret.append(N)
    return ret


N = 10 ** 6  # int(input())
A = [10 ** 6] * N  # list(map(int, input().split()))
S = []
for p in P:
    S.append(sum([1 for a in A if a % p == 0]))
print(S)


Fixed submission:
import math


def primes(N):
    p = [False, False] + [True] * (N - 1)
    for i in range(2, int(N ** 0.5) + 1):
        if p[i]:
            for j in range(i * 2, N + 1, i):
                p[j] = False
    return [i for i in range(N + 1) if p[i]]


N = int(input())
A = list(map(int, input().split()))
maxA = max(A) + 1
g, Ac = A[0], [0] * maxA
P = primes(maxA)

for a in A:
    Ac[a] += 1

ans = "pairwise"
for p in P:
    if sum(Ac[p:maxA:p]) > 1:
        ans = "setwise"
for a in A:
    g = math.gcd(g, a)
print(ans if g == 1 else "not", "coprime")
Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())
if K % 2 == 0:
    print(-1)
else:
    mod, modt, ans = 7 % K, 7 % K, 1
    while mod != 0:
        modt = (modt * (10 % K)) % K
        mod, ans = (mod + modt) % K, ans + 1
    print(ans)


Fixed submission:
K = int(input())
if K % 2 == 0 or K % 5 == 0:
    print(-1)
else:
    mod, modt, ans = 7 % K, 7 % K, 1
    while mod != 0:
        modt = (modt * (10 % K)) % K
        mod, ans = (mod + modt) % K, ans + 1
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
K = int(input())
if K % 2 == 0:
    print(-1)
else:
    mod, modt, ans = 7 % K, 7 % K, 1
    while mod != 0:
        modt = (modt * (10 % K)) % K
        mod, ans = (mod + modt) % K, ans + 1
    print(ans)


Fixed submission:
K = int(input())
if K % 2 == 0 or K % 5 == 0:
    print(-1)
else:
    mod, modt, ans = 7 % K, 7 % K, 1
    while mod != 0:
        modt = (modt * (10 % K)) % K
        mod, ans = (mod + modt) % K, ans + 1
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 1
for a in A:
    ans *= a
print(-1 if ans > 10 ** 18 else ans)


Fixed submission:
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 1
for a in A:
    ans *= a
    if ans > 10 ** 18:
        print(-1)
        exit()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
ans = 1
for a in A:
    ans *= a
print(ans if ans <= 10 ** 18 else -1)


Fixed submission:
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 1
for a in A:
    ans *= a
    if ans > 10 ** 18:
        print(-1)
        exit()
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
C = [int(S[-(i + 1):]) % 2019 for i in range(len(S))]
Cd, C0 = {}, 0
for c in C:
    if c == 0:
        C0 += 1
    else:
        Cd[c] = Cd.get(c, 0) + 1
print(C0 + sum([v * (v - 1) // 2 for v in Cd.values()]))


Fixed submission:
def modpow(d, b, p, mod=2019):
    ret = 1
    while p > 0:
        if p % 2 == 1:
            ret = ret * b % mod
        b = b * b % mod
        p //= 2
    return (d * ret) % mod


S = input()
C = [0]
for i in range(len(S)):
    C.append((C[-1] + modpow(int(S[-(i + 1)]), 10, i)) % 2019)
Cd, C0 = {}, 0
for c in C[1:]:
    if c == 0:
        C0 += 1
    else:
        Cd[c] = Cd.get(c, 0) + 1
print(C0 + C0 * (max(0, C0 - 1)) // 2
      + sum([v * (v - 1) // 2 for v in Cd.values()]))
Buggy python submission (verdict: Time Limit Exceeded):
X, Y, A, B, C = map(int, input().split())
p, pi = sorted(list(map(int, input().split())), reverse=True)[:X] + [-1], 0
q, qi = sorted(list(map(int, input().split())), reverse=True)[:Y] + [-1], 0
r, ri = sorted(list(map(int, input().split())), reverse=True) + [-1], 0
ans, nX, nY = 0, 0, 0
while nX < X or nY < Y:
    if r[ri] >= p[pi] and r[ri] >= q[qi]:
        if nX < X and nY < Y:
            if p[pi] >= q[qi]:
                nX += 1
            else:
                nY += 1
        elif nX < X:
            nX += 1
        elif nY < Y:
            nY += 1
        ans += r[ri]
        ri += 1
    elif p[pi] >= q[qi] and p[pi] >= r[ri]:
        if nX < X:
            nX += 1
            ans += p[pi]
            pi += 1
    elif q[qi] >= r[ri] and q[qi] >= p[pi]:
        if nY < Y:
            nY += 1
            ans += q[qi]
            qi += 1

print(ans)


Fixed submission:
X, Y, A, B, C = map(int, input().split())
p = sorted(list(map(int, input().split())))[-X:]
q = sorted(list(map(int, input().split())))[-Y:]
r = list(map(int, input().split()))
print(sum(sorted(p + q + r)[-(X + Y):]))
Buggy python submission (verdict: Runtime Error):
def comb(n, k):
    nume, deno = 1, 1
    for i in range(k):
        deno *= n - i
        nume *= i + 1
    return deno // nume


def ans(N, K):
    ret = sum([9 ** K * comb(max(dig - 1, 1), K - 1)
               for dig in range(K, len(N))])
    ret += (int(N[0]) - 1) * 9 ** (K - 1) * comb(len(N) - 1, K - 1)
    if K == 1:
        return ret + 1
    elif K == 2:
        return ret + ans(N[1:], 1)
    elif K == 3:
        return ret + ans(N[1:], 2)


N = input()
K = int(input())
print(ans(N, K))


Fixed submission:
from math import factorial


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


N = input()
K = int(input())
ans = 0
if K == 1:
    for i in range(1, len(N)):
        ans += 9
    ans += int(N[0])
elif K == 2:
    for i in range(K, len(N)):
        ans += 81 * comb(i - 1, 1)
    for i1 in range(1, 10):
        for i2 in range(1, 10):
            for p in range(len(N) - 1):
                if str(i1) + "0" * (len(N) - p - 2) + str(i2) + "0" * p <= N:
                    ans += 1
elif K == 3:
    for i in range(K, len(N)):
        ans += 729 * comb(i - 1, 2)
    for i1 in range(1, 10):
        for i2 in range(1, 10):
            for i3 in range(1, 10):
                for p1 in range(1, len(N) - 1):
                    for p2 in range(p1):
                        st = str(i1) + "0" * (len(N) - p1 - 2) + str(i2) + \
                            "0" * (p1 - p2 - 1) + str(i3) + "0" * p2
                        if st <= N:
                            ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]

dp = [0] * (H + 1)
for h in range(1, H + 1):
    dp[h] = min([dp[max(h - a, 0)] + b for a, b in AB])

print(dp[-1])


Fixed submission:
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])

dp = [0] * (H + 1)
for h in range(1, H + 1):
    dp[h] = min([dp[max(h - a, 0)] + b for a, b in AB])

print(dp[-1])

Buggy python submission (verdict: Runtime Error):
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])

dp = [0] * (H + 1)
for h in range(1, H + 1):
    dp[h] = min([dp[h - a] + b for a, b in AB])

print(dp[-1])


Fixed submission:
H, N = map(int, input().split())
AB = [tuple(map(int, input().split())) for _ in range(N)]
AB.sort(key=lambda x: x[1])

dp = [0] * (H + 1)
for h in range(1, H + 1):
    dp[h] = min([dp[max(h - a, 0)] + b for a, b in AB])

print(dp[-1])
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)

nA, sA, i = [N, N], [0],  N - 1
for a in range(1, A[0] * 2):
    di = 0
    while A[i - di] == a:
        di += 1
    nA.append(nA[-1] - di)
    i -= di
for a in A:
    sA.append(sA[-1] + a)

L, R, ans = 0, A[0] * 2, 0
while R - L > 1:
    mid = L + (R - L) // 2
    mA = [nA[max(mid - A[i], 0)] for i in range(N)]
    sM = sum(mA)
    if M <= sM:
        L = mid
        ans = sum([A[i] * mA[i] + sA[mA[i]]
                   for i in range(N)]) - mid * (sM - M)
    else:
        R = mid

print(ans)


Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)

nA, sA, i = [N, N], [0],  N - 1
for a in range(1, A[0] * 2):
    di = 0
    while i - di >= 0 and A[i - di] == a:
        di += 1
    nA.append(nA[-1] - di)
    i -= di
for a in A:
    sA.append(sA[-1] + a)

L, R, ans = 0, A[0] * 2 + 1, 0
while R - L > 1:
    mid = L + (R - L) // 2
    mA = [nA[max(mid - A[i], 0)] for i in range(N)]
    sM = sum(mA)
    if M <= sM:
        L = mid
        ans = sum([A[i] * mA[i] + sA[mA[i]]
                   for i in range(N)]) - mid * (sM - M)
    else:
        R = mid

print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
if N % 2 == 1:
    print(0)
else:
    ans = 0
    for i in range(1, 30):
        ans += (N // 2) // (5 ** i)

print(ans)


Fixed submission:
N = int(input())
if N % 2 == 1:
    print(0)
else:
    ans = 0
    for i in range(1, 30):
        ans += (N // 2) // (5 ** i)
    print(ans)
Buggy python submission (verdict: Runtime Error):
import math

T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())

if (A1 - B1) * T1 + (A2 - B2) * T2 == 0:
    print("infinity")
else:
    D1, D2 = (A1 - B1) * T1, (A2 - B2) * T2
    if (D1 > 0) == (D1 + D2 > 0):
        print(0)
    else:
        print(1 + 2 * math.floor(abs(D1) / (abs(D1 + D2) - 1)) + (D1 % D2 == 0))


Fixed submission:
import math

T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())

if (A1 - B1) * T1 + (A2 - B2) * T2 == 0:
    print("infinity")
else:
    D, D1 = (A1 - B1) * T1 + (A2 - B2) * T2, (A1 - B1) * T1
    if (D1 > 0) == (D > 0):
        print(0)
    else:
        if D1 % D == 0:
            print(2 * math.floor(-D1 / D))
        else:
            print(2 * math.floor(-D1 / D) + 1)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
L, R, df = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if df < 0:
        L += 1,
        df += A[L]
    else:
        R -= 1
        df -= A[R]
print(abs(df))


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
L, R, sumL_R = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if sumL_R < 0:
        L += 1
        sumL_R += A[L]
    else:
        R -= 1
        sumL_R -= A[R]
print(abs(sumL_R))

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
L, R, df = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if df < 0:
        L += 1,
        df += A[L]
    else:
        R -= 1
        df -= A[R]
print(abs(sumL_R))


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
L, R, sumL_R = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if sumL_R < 0:
        L += 1
        sumL_R += A[L]
    else:
        R -= 1
        sumL_R -= A[R]
print(abs(sumL_R))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)

ans, sumA2 = abs(sum(A) - 0), 0
for i in range(N):
    sumA2 += A[i]
    ans = min(ans, abs(sum(A) - sumA2 * 2))

print(ans)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
L, R, sumL_R = 0, N - 1, A[0] - A[N - 1]

while R - L > 1:
    if sumL_R < 0:
        L += 1
        sumL_R += A[L]
    else:
        R -= 1
        sumL_R -= A[R]
print(abs(sumL_R))