qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import numpy as np

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = np.array(a)
    sa = sum(a)
    half = int(sa/2)
    tmp = 0
    ans = 0
    flag = 0

    for i in range(n):
        tmp = tmp + a[i]
        if tmp >= half:
            if 2*tmp - sa < sa - 2*(tmp-a[i]):
                break
            else:
                tmp = tmp - a[i]
                flag = 1
                break
    
    if flag == 0:
        if sa % 2 == 0:
            ans = tmp - (sa/2)
        else:
            ans = tmp - int(sa/2) + 1
    else:
        if sa % 2 == 0:
            ans = (sa/2) - tmp
        else:
            ans = int(sa/2) - tmp + 1
    print(ans)



if __name__ == "__main__":
    main()

Fixed code:
import numpy as np

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = np.array(a)
    sa = sum(a)
    half = int(sa/2)
    tmp = 0
    ans = 0
    flag = 0

    for i in range(n):
        tmp = tmp + a[i]
        if tmp >= half:
            if 2*tmp - sa < sa - 2*(tmp-a[i]):
                break
            else:
                tmp = tmp - a[i]
                flag = 1
                break
    
    if flag == 0:
        ans = 2*tmp - sa
    else:
        ans = sa - 2*tmp
        
    print(int(ans))



if __name__ == "__main__":
    main()
Buggy code:
import sys

def main():
    n, *a = map(int, sys.stdin.readline().split())

    l = 0
    r = sum(a)
    m = sum(a)
    for i in range(n):
        l += a[i]
        r -= a[i]
        m = min(m, abs(l - r))
    
    print(m)


if __name__ == '__main__':
    main()

Fixed code:
import sys

def main():
    n, *a = map(int, sys.stdin.read().split())

    l = 0
    r = sum(a)
    m = sum(a)
    for i in range(n):
        l += a[i]
        r -= a[i]
        m = min(m, abs(l - r))
    
    print(m)


if __name__ == '__main__':
    main()
Buggy code:
N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
 
ans, sumA2 = abs(sumA - 0), 0
for i in range(N):
    sumA2 += A[i]
    ans = min(ans, abs(sumA - sumA2))
 
print(ans)

Fixed code:
N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
 
ans, sumA2 = abs(sumA - 0), 0
for i in range(N):
    sumA2 += A[i]
    ans = min(ans, abs(sumA - sumA2 * 2))
 
print(ans)
Buggy code:
import math
n=int(input())
a=list(map(int,input().split()))
ans=9999999999
hidari=sum(a)
migi=0
for i in range(n):
    hidari-=a[i]
    migi+=a[i]
    sa=math.abs(hidari-migi)
    if sa<ans:
        ans=sa
print(ans)


Fixed code:
import math
n=int(input())
a=list(map(int,input().split()))
ans=9999999999
hidari=sum(a)
migi=0
for i in range(n):
    hidari-=a[i]
    migi+=a[i]
    sa=math.fabs(hidari-migi)
    if sa<ans:
        ans=sa
print(int(ans))
Buggy code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

A_cs = A.cumsum()
A_cs = A_cs - A_cs[-1]/2
div = np.abs(A_cs).argmin()

print(abs(A[:div+1].sum()A - [div+1:].sum()))

Fixed code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))

A_cs = A.cumsum()
A_cs = A_cs - A_cs[-1]/2
div = np.abs(A_cs).argmin()

print(abs(A[:div+1].sum() - A[div+1:].sum()))
Buggy code:
# https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_b

import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    al = list(map(int, input().split()))

    cum_len = [0] * len(al)
    cum_len[0] = al[0]
    for i in range(1, len(al)):
        cum_len[i + 1] = cum_len[i] + al[i]

    ans = 1000000
    for i in range(len(al)-1):
        left = cum_len[i]
        right = cum_len[-1] - cum_len[i]
        ans = min(ans, abs(left - right))

    print(ans)


if __name__ == '__main__':
    main()


Fixed code:
# https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_b

import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    al = list(map(int, input().split()))

    cum_len = [0] * len(al)
    cum_len[0] = al[0]
    for i in range(1, len(al)):
        cum_len[i] = cum_len[i-1] + al[i]

    # print(sum(al))
    ans = 10**10
    for i in range(len(al)):
        left = cum_len[i]
        right = cum_len[-1] - cum_len[i]
        # print(left, right)
        ans = min(ans, abs(left - right))

    print(ans)


if __name__ == '__main__':
    main()
Buggy code:
import numpy as np
import bisect
n = int(input())
A = list(map(int,input().split()))

cs = np.cumsum(A).tolist()
mid = sum(A)//2
idx = bisect.bisect_left(cs, mid)

ans = float('inf')
for i in range(max(0, idx-1), idx+1):
    x = A[:i]
    y = A[i:]
    sum_x = sum(x)
    sum_y = sum(y)
    ans = min(ans, abs(sum_x - sum_y))
print(ans)

Fixed code:
import numpy as np
import bisect
n = int(input())
A = list(map(int,input().split()))
cs = np.cumsum(A).tolist()
mid = sum(A)//2
idx = bisect.bisect_left(cs, mid)
ans = float('inf')
for i in range(max(1, idx-1), min(idx+2, n)):
    x = A[:i]
    y = A[i:]
    sum_x = sum(x)
    sum_y = sum(y)
    ans = min(ans, abs(sum_x - sum_y))
print(ans)
Buggy code:
n=int(input())
A=list(map(int,input().split()))
m=float('INF')
sa=[]
for i,a in enumerate(A):
    sa.append(sa[i]+ai)
for i in range(n+1):
    ans=min(ans,abs(sa[i]-(sa[-1]-sa[i])))
print(ans)
    

Fixed code:
n=int(input())
A=list(map(int,input().split()))
ans=float('INF')
sa=[0]
for i,a in enumerate(A):
    sa.append(sa[i]+a)
for i in range(n+1):
    ans=min(ans,abs(sa[i]-(sa[-1]-sa[i])))
print(ans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
sumFrontHalf=0
halfIndex=0
sumA=aum(a)
for i in range(n):
  sumFrontHalf+=a[i]
  if sumFrontHalf>=sumA/2:
    halfIndex=i
    break
sumBackHalf=sumA-sumFrontHalf+a[halfIndex]
if sumFrontHalf<sumBackHalf:
  print(sumFrontHalf-sumBackHalf+a[halfIndex])
else:
  print(sumBackHalf-sumFrontHalf+a[halfIndex])

Fixed code:
n=int(input())
a=list(map(int,input().split()))
sumFrontHalf=0
halfIndex=0
sumA=sum(a)
for i in range(n):
  sumFrontHalf+=a[i]
  if sumFrontHalf>=sumA/2:
    halfIndex=i
    break
sumBackHalf=sumA-sumFrontHalf+a[halfIndex]
if sumFrontHalf<sumBackHalf:
  print(sumFrontHalf-sumBackHalf+a[halfIndex])
else:
  print(sumBackHalf-sumFrontHalf+a[halfIndex])
Buggy code:
def main():
    import sys
    import bisect


    n = int(input())
    a = list(map(int, sys.stdin.readline().split()))

    b = list(accumulate(a))
    l = b[-1]
    i = bisect.bisect(b, l // 2)
    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i]))
    print(ans)


if __name__ == '__main__':
    main()


Fixed code:
def main():
    import sys
    import bisect
    from itertools import accumulate

    n = int(input())
    a = list(map(int, sys.stdin.readline().split()))

    b = list(accumulate(a))
    l = b[-1]
    i = bisect.bisect(b, l // 2)
    ans = min(abs(l - 2 * b[i - 1]), abs(l - 2 * b[i]))
    print(ans)


if __name__ == '__main__':
    main()
Buggy code:
N = int(input())
li = list(map(int,input().split()))
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
for i in range(N):
    sum1 += li[i]
    sum4 += li[i]
sum1 //= 2
for i in range(N):
    sum2 += li[i]
    if sum2 >= sum1:
        ans = (sum1-sum3) + abs((sum4-sum2) - sum1)
        break
    sum3 += li[i]
print(ans)

Fixed code:
N = int(input())
li = list(map(int,input().split()))
sum1 = 0
sum2 = 0
sum3 = 0
sum4 = 0
for i in range(N):
    sum1 += li[i]
    sum4 += li[i]
sum1 //= 2
for i in range(N):
    sum2 += li[i]
    if sum2 >= sum1:
        ans = min((sum2 - sum1 + abs(sum4-sum2-sum1)),((sum1-sum3)+sum4-sum3-sum1))
        break
    sum3 += li[i]
print(ans)
Buggy code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
cn=0
buf=s
for i in range(n):
  cn+=a[i]
  buf=min(bug,abs(s-2+cn))
print(buf)


Fixed code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
cn=0
buf=s
for i in range(n):
  cn+=a[i]
  buf=min(buf,abs(s-2*cn))
print(buf)
Buggy code:
N = int(input())
A = list(map(int, input().split()))

lst = []
for i in range(0, N-1):
    a = abs(sum(A[:i]) - sum(A[i:]))
    lst.append(a)
#print(lst)
print(min(lst))

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

cumulative = [0]
for i in range(N):
    cumulative.append(cumulative[i] + A[i])
lst = []
for i in range(1, N):
    a = abs(cumulative[i] - (cumulative[-1] - cumulative[i]))
    lst.append(a)
#print(lst)
print(min(lst))
Buggy code:
N = int(input())
A = [int(i) for i in input().split()]

half_len = sum(A) / 2
now_len = 0
most_near_len = abs(half_len - A[0])
for i in A:
    now_len += i
    if abs(half_len - now_len) <= abs(half_len - most_near_len):
        most_near_len = now_len

cnt = abs(most_near_len - half_len) / 2
print(int(cnt))
    

Fixed code:
N = int(input())
A = [int(i) for i in input().split()]

half_len = sum(A) / 2.0
now_len = 0
most_near_len = sum(A)+1
for i in A:
    now_len += i
    if abs(half_len - now_len) <= abs(half_len - most_near_len):
        most_near_len = now_len

cnt = abs(most_near_len - half_len) / 0.5
print(int(cnt))
Buggy code:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
ans = float("INF")
LS = [0]*(N+1)
LS[0] = 0
for i in range(N-1):
  LS[i+1] = A[i] + LS[i]
  ans = min(ans, abs(S - LS[i+1]))
print(ans)

Fixed code:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
ans = float("INF")
LS = [0]*(N+1)
LS[0] = 0
for i in range(N-1):
  LS[i+1] = A[i] + LS[i]
  ans = min(ans, abs(LS[i+1] - (S - LS[i+1])))
print(ans)
Buggy code:
N = int(input())
a = list(map(int, input().split()))
r = sum(a)
ra = 0
result = 1e30
for i in tqdm(range(0,N)):
    ra += a[i]
    r -= a[i]
    result = min(result, abs(ra-r))
print(result)

Fixed code:
N = int(input())
a = list(map(int, input().split()))
r = sum(a)
ra = 0
result = 1e30
for i in range(0,N):
    ra += a[i]
    r -= a[i]
    result = min(result, abs(ra-r))
print(int(result))
Buggy code:
# -*- coding: utf-8 -*-

import sys
from bisect import bisect_left
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = [0] + LIST()

acc = list(accumulate(A))
mid = sum(A) / 2
l = bisect_left(acc, mid) - 1
r = bisect_left(acc, mid)
l = acc[l]
r = acc[r]
print(min(mid-l, r-mid)*2)


Fixed code:
# -*- coding: utf-8 -*-

import sys
from bisect import bisect_left
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = [0] + LIST()

acc = list(accumulate(A))
mid = sum(A) / 2
l = bisect_left(acc, mid) - 1
r = bisect_left(acc, mid)
l = acc[l]
r = acc[r]
print(int(min(mid-l, r-mid)*2))
Buggy code:
from itertools import accumulate


N = int(input())
A = [0] + list(accumulate(map(int, input().split())))
print(A)
min_diff = float("inf")
for i in range(N - 1):
    if abs(A[N] - 2 * A[i + 1]) < min_diff:
        min_diff = abs(A[N] - 2 * A[i + 1])
print(abs(min_diff))


Fixed code:
from itertools import accumulate


N = int(input())
A = [0] + list(accumulate(map(int, input().split())))
min_diff = float("inf")
for i in range(N - 1):
    if abs(A[N] - 2 * A[i + 1]) < min_diff:
        min_diff = abs(A[N] - 2 * A[i + 1])
print(min_diff)
Buggy code:
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
    a[i] += a[i-1]
c = a[-1]
for i in range(n):
   p = abs(a[-1]-a[i]*2)
      if c > p:
        c = p   
print(c) 

Fixed code:
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
    a[i] += a[i-1]
c = a[-1]
for i in range(n):
    p = abs(a[-1]-a[i]*2)
    if c > p:
        c = p   
print(c)
Buggy code:
N = int(input())
A = list(map(int, input().split()))

sep = N//2
for i in range(N):
  if sum(A[:sep]) >= sum(A[sep:]):
    sep -= N//((i+2)**2)
  else:
    sep += N//((i+2)**2)
  if N//((i+2)**2) == 1:
    break
#print(sep)

a = abs(sum(A[:sep])-sum(A[sep:]))
b = abs(sum(A[:sep+1])-sum(A[sep+1:]))
c = abs(sum(A[:sep-1])-sum(A[sep-1:]))
print(min(a,b,c))

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

sep = N//2
for i in range(N):
  if sum(A[:sep]) >= sum(A[sep:]):
    sep -= N//((i+2)**2)
  else:
    sep += N//((i+2)**2)
  if N//((i+2)**2) == 1:
    break
#print(sep)


a = abs(sum(A[:sep])-sum(A[sep:]))
b = abs(sum(A[:sep+1])-sum(A[sep+1:]))
c = abs(sum(A[:sep-1])-sum(A[sep-1:]))
d = abs(sum(A[:sep+2])-sum(A[sep+2:]))
e = abs(sum(A[:sep-2])-sum(A[sep-2:]))
print(min(a,b,c,d,e))
Buggy code:
N = int(input())
A = list(map(int,input().split()))

def func(A):
    A_inf = 0
    A_sup = 0
    N_inf = 0
    N_sup = 0
    L = sum(A)

    for a in A:
        if A_inf + a <= L/2:
            A_inf += a
            A_sup += a
            N_inf += 1
            N_sup += 1

        if A_sup <= L/2 and A_sup + a > L/2:
            A_sup += a
            N_sup += 1

    if A_inf == A_sup:
        return 0

    A_mid = A_sup - A_inf
    A_left = L/2 - A_inf
    A_right = A_sup - L/2
    A_lrmin = min(A_left, A_right)
    if A_mid-1 >= 2*A_lrmin:
        return 2*A_lrmin
    else:
        return 2*A_lrmin+1

print(func(A))

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

def func(A):
    A_inf = 0
    A_sup = 0
    N_inf = 0
    N_sup = 0
    L = sum(A)

    for a in A:
        if A_inf <= L/2 - a:
            A_inf += a
            A_sup += a
            N_inf += 1
            N_sup += 1
            continue
        else:
            if A_sup <= L/2 and A_sup + a > L/2:
                A_sup += a
                N_sup += 1
            break

    if A_inf == A_sup:
        return 0

    A_mid = A_sup - A_inf
    A_left = L/2 - A_inf
    A_right = A_sup - L/2
    A_lrmin = min(A_left, A_right)
    if A_mid-1 >= 2*A_lrmin:
        return int(2*A_lrmin)
    else:
        return int(A_mid)

print(func(A))
Buggy code:
# B - Iron Bar Cutting
N = int(input())
A = [map(int, input().split())]

left = 0
right = sum(A)
ans = right

for a in A:
	left += a
	right -= a
	
	diff = abs(right - left)
	ans = min(ans, diff)
	
	if left > right:
		break

print(ans)

Fixed code:
# B - Iron Bar Cutting
N = int(input())
A = list(map(int, input().split()))

left = 0
right = sum(A)
ans = right

for a in A:
	left += a
	right -= a
	
	diff = abs(right - left)
	ans = min(ans, diff)
	
	if left > right:
		break

print(ans)
Buggy code:
n = int(input())
a = [int(i) for i in input().split()]

len_f = sum(a)
sum_a = 0
min = 2020202021

for i in range(1,len(a)):
    sum_a += a[i]
    len_f -= a[i]
    if min > abs(sum_a - len_f):
        min = abs(sum_a - len_f)

print(min)

Fixed code:
n = int(input())
a = [int(i) for i in input().split()]

len_f = sum(a)
sum_a = 0
min = 2020202021

for i in range(n):
    sum_a += a[i]
    len_f -= a[i]
    if min > abs(sum_a - len_f):
        min = abs(sum_a - len_f)

print(min)
Buggy code:
import math
from collections import deque
def main():
    n = int(input())
    a = [int(t)for t in input().split()]
    a_rev = a[::-1]

    cusum = [a[0]]
    rcusum = [a_rev[0]]

    for i in range(1,n):
        cusum.append(cusum[i-1]+a[i])
        rcusum.append(rcusum[i-1]+a[i])
    
    rcusum = rcusum[::-1]

    result = []

    for i in range(n):
        result.append(abs(cusum[i]-rcusum[i]))

    print(min(result))
    
if __name__ == "__main__":
    main()

Fixed code:
import math
from collections import deque
def main():
    n = int(input())
    a = [int(t)for t in input().split()]
    a_rev = a[::-1]

    cusum = [a[0]]
    rcusum = [a_rev[0]]

    for i in range(1,n):
        cusum.append(cusum[i-1]+a[i])
        rcusum.append(rcusum[i-1]+a_rev[i])
    
    rcusum = rcusum[::-1]

    result = []

    for i in range(1,n):
        result.append(abs(cusum[i-1]-rcusum[i]))

    print(min(result))

if __name__ == "__main__":
    main()
Buggy code:
from itertools import accumulate
import math

N =int(input())
list_aaa = list(map(int,input().split()))

ans = 10**10

sum_list_aaa =list(accumulate(list_aaa))

for i in range(N):
    zen = sum_list_aaa[i]
    go = sum_list_aaa[-1]-sum_list_aaa[i]
    ans = min(ans,abs(zen-go)

print(ans)


Fixed code:
from itertools import accumulate
import math

N =int(input())
list_aaa = list(map(int,input().split()))

ans = 10**10

sum_list_aaa =list(accumulate(list_aaa))

for i in range(N):
    zen = sum_list_aaa[i]
    go = sum_list_aaa[-1]-sum_list_aaa[i]
    ans = min(ans,abs(zen-go))

print(ans)
Buggy code:
n = int(input())
a = [int(s) for s in input().split()]

sum1 = 0
count = 0
total_length = sum(a)

for i in range(len(a)):
    sum1 += a[i]
    if sum1 >= total_length / 2:
        count1 = sum1 - sum(a[i + 1:])
        count2 = sum1 - a[i] - sum(a[i:])
        if count1 <= count2:
            count = abs(count1)
        else:
            count = abs(count2)
        break
print(count)


Fixed code:
n = int(input())
a = [int(s) for s in input().split()]

sum1 = 0
count = 0
total_length = sum(a)

for i in range(len(a)):
    sum1 += a[i]
    if sum1 >= total_length / 2:
        count1 = abs(sum1 - sum(a[i + 1:]))
        count2 = abs(sum1 - a[i] - sum(a[i:]))
        if count1 <= count2:
            count = count1
        else:
            count = count2
        break
print(count)
Buggy code:

n = int(input())
a = list(map(int,input().split()))
d = [0]

for i in range(n):
    d.append(d[i] ;a[i])

ans = d[-1]

for i in range(n):
    ans =min(ans,abs(d[-1] -2*d[i]))
print(ans)


Fixed code:

n = int(input())
a = list(map(int,input().split()))
d = [0]

for i in range(n):
    d.append(d[i] + a[i])

ans = d[-1]

for i in range(n):
    ans =min(ans,abs(d[-1] -2*d[i]))
print(ans)
Buggy code:
from itertools import accumulate

n = int(input())
a = list(map(int, input().split()))

sum_l = [0]+list(accumulate(a))
sum_r = [0]+list(accumulate(a[:-1]))

cost = sum_r[-1]
for i in range(n+1):
    cost = min(cost, abs(sum_l[i]-sum_r[n-i]))
print(cost)

Fixed code:
from itertools import accumulate

n = int(input())
a = list(map(int, input().split()))

sum_l = [0]+list(accumulate(a))
sum_r = [0]+list(accumulate(a[-1::-1]))

cost = sum_r[-1]
for i in range(n+1):
    cost = min(cost, abs(sum_l[i]-sum_r[n-i]))
print(cost)
Buggy code:
n=int(input())
a=[int(i) for i in input().split()]
asum2=sum(a)//2
j,k=0,0
for i in range(n):
  j+=a[i]
  if j>=asum2:
    break
for i in range(n):
  k+=a[n-i-1]
  if k>=asum2:
    break
if j-asum2>k-asum2:
  if sum(a)%2==0:
    print(2*j-2*asum2)
  else:
    print(2*j-2*asum2+1)
else:
  if sum(a)%2==0:
    print(2*k-2*asum2)
  else:
    print(2*k-2*asum2+1)
  
 

Fixed code:
n=int(input())
a=[int(i) for i in input().split()]
asum2=sum(a)/2
j,k=0,0
for i in range(n):
  j+=a[i]
  if j>=asum2:
    break
for i in range(n):
  k+=a[n-i-1]
  if k>=asum2:
    break
if j-asum2<k-asum2:
  print(int(2*j-2*asum2))
else:
  print(int(2*k-2*asum2))
Buggy code:
n=int(input())
a=list(map(int,input().split()))
temp=sum(a)
ans=0
t=999999999
tsum=0
for i in range(n):
  tsum=tsum+a[i]
  if abs(tsum-temp/2)<t:
    t=abs(tsum-temp/2)
print(int(t)*2-temp)

Fixed code:
n=int(input())
a=list(map(int,input().split()))
temp=sum(a)
ans=0
t=9999999999999999
tsum=0
ta=0
for i in range(n):
  tsum=tsum+a[i]
  if abs(tsum-temp/2)<t:
    t=abs(tsum-temp/2)
    ta=tsum
print(abs(int(ta)*2-temp))
Buggy code:
N = int(input())
A = list(map(int, input().split()))

tmp = [0 for i in range(N+1)]
length = 0
for i in range(N):
  tmp[i+1] = tmp[i] + A[i]
  length += A[i]
  
ans = 0
for i in range(1, N+1):
 ans = min(ans, abs(length - tmp[i]))

print(ans)

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

tmp = [0 for i in range(N+1)]
length = 0
for i in range(N):
  tmp[i+1] = tmp[i] + A[i]
  length += A[i]
  
ans = 10 ** 10
for i in range(1, N+1):
 ans = min(ans, abs((length - tmp[i]) - tmp[i]))

print(ans)
Buggy code:
N = int(input())
A = [0]
Sum = 0
for s in input().split():
    s = int(s)
    Sum += s
    A.append(Sum)
mid = A[-1]//2
ans = 2*min(abs(A[i]-mid) for i in range(N+1))
print(int(ans))

Fixed code:
N = int(input())
A = [0]
Sum = 0
for s in input().split():
    s = int(s)
    Sum += s
    A.append(Sum)
mid = A[-1]/2
ans = 2*min(abs(A[i]-mid) for i in range(N+1))
print(int(ans))
Buggy code:
n=int(input())
a=list(map(int,input().split()))
su=0
mi=2000000
for i in range(n):
  su+=a[i]
  left=n-su
  mi=min(mi,max((su-left),(left-su)))

print(mi)

Fixed code:
n=int(input())
a=list(map(int,input().split()))
su=0
mi=20000000000000000000
for i in range(n):
  su+=a[i]
  
c=0  
for i in range(n):
  c+=a[i]
  left=su-c
  mi=min(mi,max((c-left),(left-c)))

print(mi)
Buggy code:
N = int(input())

An = list(map(int, input().split()))

sum_of_an = sum(An)

if sum_of_an % 2 == 0:
    half_of_an = sum_of_an / 2
    sum_left_len = 0
    for l in An:
        sum_left_len += l
        if sum_left_len == half_of_an:
            print(0)
            exit(0)
        elif sum_left_len > half_of_an:
            break
min_yen = None
left_len = 0
for (i, l) in zip(range(0, N - 1), An):
    left_len += l
    if min_yen is None:
        min_yen = abs(sum_of_an - left_len)
    else:
        min_yen = min(min_yen, abs(sum_of_an, left_len))

print(min_yen)

Fixed code:
N = int(input())

An = list(map(int, input().split()))

sum_of_an = sum(An)

if sum_of_an % 2 == 0:
    half_of_an = sum_of_an / 2
    sum_left_len = 0
    for l in An:
        sum_left_len += l
        if sum_left_len == half_of_an:
            print(0)
            exit(0)
        elif sum_left_len > half_of_an:
            break
min_yen = None
left_len = 0
for (i, l) in zip(range(0, N), An):
    if i == N - 1:
        break

    left_len += l
    if min_yen is None:
        min_yen = abs(sum_of_an - left_len * 2)
    else:
        min_yen = min(min_yen, abs(sum_of_an - left_len * 2))

print(min_yen)
Buggy code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
s = sum(a)
cnt = 0
res = INF
b = [0] * n
for i in range(n-1):
    a[i+1] = a[i+1] + a[i]

for i in range(n):
    res = min(res,s-a[i])
print(res)

Fixed code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
s = sum(a)
cnt = 0
res = INF
b = [0] * n
for i in range(n-1):
    a[i+1] = a[i+1] + a[i]
for i in range(n):
    res = min(res,abs(s-a[i]-a[i]))
print(res)