Problem:
 Score: 200 points    Problem Statement  Takahashi, who works at DISCO, is standing before an iron bar.
The bar has N-1 notches, which divide the bar into N sections. The i -th section from the left has a length of A_i millimeters.  Takahashi wanted to choose a notch and cut the bar at that point into two parts with the same length.
However, this may not be possible as is, so he will do the following operations some number of times before he does the cut:   Choose one section and expand it, increasing its length by 1 millimeter. Doing this operation once costs 1 yen (the currency of Japan).  Choose one section of length at least 2 millimeters and shrink it, decreasing its length by 1 millimeter. Doing this operation once costs 1 yen.   Find the minimum amount of money needed before cutting the bar into two parts with the same length.      Constraints   2 \leq N \leq 200000  1 \leq A_i \leq 2020202020  All values in input are integers.         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  A_3  ...  A_N       Output  Print an integer representing the minimum amount of money needed before cutting the bar into two parts with the same length.        Sample Input 1 3
2 4 3      Sample Output 1 3  The initial lengths of the sections are [2, 4, 3] (in millimeters). Takahashi can cut the bar equally after doing the following operations for 3 yen:   Shrink the second section from the left. The lengths of the sections are now [2, 3, 3] .  Shrink the first section from the left. The lengths of the sections are now [1, 3, 3] .  Shrink the second section from the left. The lengths of the sections are now [1, 2, 3] , and we can cut the bar at the second notch from the left into two parts of length 3 each.        Sample Input 2 12
100 104 102 105 103 103 101 105 104 102 104 101      Sample Output 2 0

Solution (Python):
N = int(input())
length_list = [int(x) for x in input().split()]

all_length = sum(length_list)

half_length = all_length / 2
before_sum = 0
after_sum = 0
for ax in length_list:
    if before_sum+ax < half_length:
        before_sum += ax
        continue
    else:
        after_sum = before_sum + ax
        break
before_diff = all_length - before_sum 
after_diff = all_length - after_sum

if before_diff - before_sum < after_sum - after_diff:
    print(before_diff - before_sum)
else:
    print(after_sum - after_diff)

Solution (Python):
a=int(input())
b=list(map(int,input().split()))
money=0
kei=0
cnt1=0
cnt2=0
if a%2==1:
    for i in range(0,a,1):
        kei+=b[i]
    f=0
    while cnt1<=kei/2:
        cnt1+=b[f]
        f+=1
    cnt1-=b[f-1]
    g=a-1
    while cnt2<=kei/2:
        cnt2+=b[g]
        g-=1
    cnt2-=b[g+1]
else:
    for i in range(0,a,1):
        kei+=b[i]
    f=0
    while cnt1<=kei/2:
        cnt1+=b[f]
        f+=1
    cnt1-=b[f-1]
    g=a-1
    while cnt2<=kei/2:
        cnt2+=b[g]
        g-=1
    cnt2-=b[g+1]
if cnt1==kei/2:
    print(0)
else:
    print(abs(b[f-1]-abs(cnt1-cnt2)))

Solution (Python):
N = int(input())
length = [int(i) for i in input().split()]
pos = []
s1 = sum(length)
s2 = 0

for a in range(len(length)):
    pos.append(max(s1,s2) - min(s1,s2))
    s1 -= length[a]
    s2 += length[a]
    


print(min(pos))

Solution (Python):
n=int(input())

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

if n==2:
  print(abs(r[0]-r[1]))
  exit()
 



temp=sum(r)

goukei=0
for item in r:
  if goukei+item>=temp/2.0:
    
    if abs(temp-goukei-goukei)<=abs((temp-(goukei+item))-(goukei+item)):
      print(abs(temp-goukei-goukei))
    else:
      print(abs((temp-(goukei+item))-(goukei+item)))
    exit()
  goukei=goukei+item
  
goukei=goukei-item
  
print(abs(temp-(goukei-item)))

Solution (Python):
n = int(input())
list_a = list(map(int, input().split()))

sum_a = sum(list_a)
mid_sum_a = sum_a / 2

sum_first = 0
i = 0
result = 0

while sum_first < mid_sum_a:
    sum_first += list_a[i]
    i += 1
if sum_first == mid_sum_a:
    result = 0
if sum_first - mid_sum_a <= mid_sum_a - (sum_first - list_a[i - 1]):
    result = (sum_first - mid_sum_a) * 2
else:
    result = (mid_sum_a - (sum_first - list_a[i - 1])) * 2
print(int(result))

Solution (Python):
N=int(input())
A_list=[int(thing) for thing in input().split(" ")]

Suu=sum(A_list)
S=Suu*0.5
import sys

sys.setrecursionlimit(2000)
def my_bisect(l:list,left_true=False):
    def bis(begin:int,end:int):
        
        if end-begin<=1:
            if begin==0:
                if A_list[0]>S:
                    return 0
                else:return 1
            
            
            
            if begin==0:
                B=A_list[begin+1]
            else:
                B=sum(A_list[0:begin+1])
            if B>S==True:
                return begin
            else:
                return end

        v=int((end+begin)/2//1)
        
        res=sum(A_list[0:v+1])>S
        
        
        
        if not res:
            return bis(v,end)
        elif  res:
            return bis(begin,v)
        else:
            print("what")
    return bis(0,len(l)-1)

m=my_bisect(A_list)


if m==0:
    print(A_list[0])
else:
    if m==1:
        nagasa=A_list[0]
    else:
        nagasa=sum(A_list[:m])
    print(min(abs((nagasa+A_list[m])*2-Suu),abs(nagasa*2-Suu)))

Solution (Python):
n = int(input())
a = input().split()
a = [int(i) for i in a]

total = sum(a)
mid = int((total + 1) / 2)

temp_sum = 0
for i in range(len(a)):
    temp_sum += a[i]
    if temp_sum >= mid:
        over_idx = i
        break

diff1 = abs(sum(a[:over_idx]) - sum(a[over_idx:]))
diff2 = abs(sum(a[:over_idx+1]) - sum(a[over_idx+1:]))

ans = min(diff1, diff2)

print(ans)

Solution (Python):
import math

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors




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

total=sum(a)

mindif=1e30
leftbest=-1

l=0
r=total

for i in range(n):
    l+=a[i]
    r-=a[i]
    if(abs(l-r)<mindif):
        mindif=abs(l-r)
        
print(mindif)

Solution (Python):
n = int(input())
ls_bf = list(map(int,input().split()))
ls_af = []
ls_sum = 0
am = 0
for i in range(n):
    ls_af.append(ls_bf[i] + ls_sum)
    ls_sum += ls_bf[i]

for i in ls_af:
    if abs(2*i - ls_af[n-1]) < abs(2 * am - ls_af[n-1]):
        am = i


print(abs(2*am - ls_af[n-1]))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = sum(a)
c = 0
index = 0
for i in range(n):
    c += a[i]
    if c >= b / 2:
        index = i
        break
print(min(abs(sum(a[:index+1]) - sum(a[index+1:])), abs(sum(a[:index]) - sum(a[index:]))))

Solution (Python):

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

sum = 0
for i in range(len(A)):
    sum += A[i]

check_sum = 0
for i in range(len(A)):
    check_sum += A[i]
    if check_sum == sum/2:
        print(0)
        exit()
    if check_sum > sum / 2:
        if i+ 1 == len(A):
            print(A[i] - (check_sum - A[i]))
            exit()
        else:
            if (abs(2 * (check_sum - A[i]) - sum) < abs(2 * check_sum - sum)):
                print(abs(2 * (check_sum - A[i]) - sum))
                exit()
            
            else:
                print(abs(2 * check_sum - sum))
                exit()

Solution (Python):
n=int(input())
bar=list(map(int,input().split()))
hev=sum(bar)
hev_sum=[bar[0]]*n 
for i in range(1,len(bar)):
    hev_sum[i]=hev_sum[i-1]+bar[i]
if (hev/2) in hev_sum:
    print(0)
else:
    for i in range(0,len(bar)-1):
        if hev_sum[i]<(hev/2) and hev_sum[i+1]>(hev/2) :
            ans=i
    first=hev_sum[ans]
    second=hev-first-bar[ans+1]
    ab=abs(first-second)
    print(bar[ans+1]-ab)

Solution (Python):
import math

n = int(input())

a = input().split()


cnt = 2020202022

length_sum = 0
length_sum_start_left = 0

for i in range(n): 
  length_sum += int( a[i] )

for i in range(n-1): 
  length_sum_start_left += int( a[i] )
  if( abs( length_sum - length_sum_start_left - length_sum_start_left ) < cnt ):
    cnt = abs( length_sum - length_sum_start_left - length_sum_start_left )
  if( cnt == 0 ):
    break

print(cnt)

Solution (Python):
import math
n=int(input())
a=list(map(int,input().split()))

sum_a=sum(a)
left_a=0
right_a=sum_a
ans=200002*2020202020

for i in range(n):
    left_a+=a[i]
    right_a-=a[i]
    if(ans>abs(left_a-right_a)):
        ans=abs(left_a-right_a)
print(ans)

Solution (Python):
import bisect


def CumulativeSum(l):
    a = [0]
    for i in l:
        a.append(a[-1] + i)
    return a[1:]


def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    b = CumulativeSum(a)
    s = b[-1]
    h = s // 2
    x = bisect.bisect_left(b, h)
    try:
        m = (b[x], b[x + 1], b[x - 1])
    except:
        m = (b[x], b[x - 1])
    if s % 2 == 0 and h in m:
        print(0)
        return

    print(min([abs(x * 2 - s) for x in m]))


if __name__ == '__main__':
    resolve()

Solution (Python):
n = int(input())
li = list(map(int,input().split()))

s = sum(li)
size = s / 2
plus = 0

for i in li:
    plus += i
    if plus >= size:
        mid = i
        break

ans_x = plus * 2 - s
ans_y = s - (plus - mid) * 2

if size == plus:
    ans_x = 0 

print(min(ans_x,ans_y))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
sm = sum(a)
l = 0
r = 0
ans = 2020202020 * 200000
for x in a:
    l += x
    r = sm - l
    ans = min(ans, abs(l - r))
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split(" ")))

total = sum(a)
half = total / 2
current = 0


for i in range(n):
    current += a[i]
    if half < current:
        half_index = i
        break

pos_r = current - half
pos_l = a[half_index] - pos_r

print(int(min(pos_l, pos_r) / 0.5))

Solution (Python):

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

A_sum = sum(A)
a_half = 0
for ai in A:
    a_half_next = a_half + ai
    if a_half_next >= A_sum / 2:
        break
    a_half = a_half_next
    
a_half_dif =  abs((A_sum - a_half) - a_half)
a_half_next_dif = abs((A_sum - a_half_next) - a_half_next)
if a_half_next_dif <= a_half_dif:
    a_half_dif = a_half_next_dif

    
print(a_half_dif)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
anslist = []
lefts = 0
rights = sum(A)
for i in range(N):
    lefts += A[i]
    rights -= A[i]
    anslist.append(abs(lefts - rights))
print(min(anslist))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
sum = 0
c_1 = 0
c_2 = 0
for i in range(n):
    sum += a[i]

for i in range(n):
    c_1 += a[i]
    if c_1 * 2 >= sum:
        break
for i in range(n):
    c_2 += a[n - i - 1]
    if c_2 * 2 >= sum:
        break
    
if c_1 * 2 - sum >= c_2 * 2 - sum:
    print(c_2 * 2 - sum)
else:
    print(c_1 * 2 - sum)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))



BarLen = sum(A)
BarHalf = BarLen / 2



c_sum = 0
for a in A:
    if c_sum + a == BarHalf:
        n_point = 0
        break
    elif c_sum + a > BarHalf:
        d_minus = BarHalf - c_sum
        d_plus = c_sum + a - BarHalf
        n_point = min(d_minus, d_plus)
        break
    else:
        c_sum += a

print(int(n_point/0.5))

Solution (Python):
import sys
n = int(sys.stdin.readline())
a  = [int(n) for n in input().split()]
y = 0
summ = sum(a)
c = int(len(a) / 2)
t = False
while 1:
    lsum = sum(a[:c])
    rsum = sum(a[c:])
    if not t:
        if lsum > rsum:
            t = True
            c -= 1
        else:
            c += 1
    else:
        if lsum > rsum:
            c -= 1
        else:            
            h = sum(a[c:]) - sum(a[:c]) 
            hh = sum(a[:c + 1]) - sum(a[c + 1:])
            if h < hh:
                y += h
            else:
                y += hh
            break

print(y)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 1000000000000
sum_A = sum(A)
left = 0
for i in range(N - 1):
    left += A[i]
    ans = min(ans, abs(sum_A -  left - left))
print(ans)

Solution (Python):


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

L = sum(A)

ac = 0
for i in range(N):
  ac += A[i]
  if(ac >= L/2):
    key = i
    break

cost = 0
if(L%2 == 1):
  A[key] -= 1
  L -= 1
  cost = 1

ac = 0
for i in range(N):
  ac += A[i]
  if(ac >= int(L/2)):
    key = i
    break

if(ac == int(L/2)):
  print(cost)
else:
  right = ac - int(L/2)
  left = int(L/2) - (ac-A[key])
  cost += min(right, left)*2
  print(cost)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

mx = sum(A)

count_a = 0
count_b = 0
a_Flag = False
b_Flag = False
for i in range(len(A)):
    if not a_Flag:
        count_a += A[i]
    if not b_Flag:
        count_b += A[N - i - 1]
    if mx / 2 <= count_a:
        a_Flag = True
    if mx / 2 <= count_b:
        b_Flag = True
    if a_Flag and b_Flag:
        break

other_a = mx - count_a
other_b = mx - count_b
if count_a - other_a < count_b - other_b:
    print(abs(count_a - other_a))
else:
    print(abs(count_b - other_b))

Solution (Python):
import sys
input = sys.stdin.readline


n=int(input())
l=list(map(int,input().split()))
m=[l[0]]
for i in range(1,n):
    m.append(m[-1]+l[i])
check=0
if m[-1]%2==1:
    check=1
if m[-1]%2==0:
    sa=m[n-1]
    base=m[-1]/2
    for k in range(n):
        if abs(base-m[k])<sa:
            sa=abs(base-m[k])
    print(int(sa*2+check))
else:
    m1=m
    for _ in range(n):
        m1[_]+=1
    sa=m1[n-1]
    base=m1[-1]/2
    for k in range(n):
        if abs(base-m1[k])<sa:
            sa=abs(base-m1[k])
    ans1=sa*2+1
    for v in range(n-1):
        m[v]-=1
    m[-1]+=1
    sa=m[n-1]
    base=(m[n-1]/2)//1
    for k in range(n):
        if abs(base-m[k])<sa:
            sa=abs(base-m[k])
    ans2=sa*2+1
    print(int(min(ans1,ans2)))

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

t = [0] * n
t[0] = a[0]
for i in range(1, n):
    t[i] = a[i] + t[i-1]

h = sum(a) / 2
ans = 0
for i in range(n):
    if t[i] >= h and t[i] - h >= h - t[i-1]:
        print()
        ans = sum(a) - t[i-1] * 2
        break
    elif t[i] >= h and t[i] - h < h - t[i-1]:
        ans = t[i] * 2 - sum(a)
        break
    

print(ans)

Solution (Python):
N = int(input())
listA = list(map(int, input().split()))

cost = 0
length = 0
for a in listA:
    length += a

padding = 0
if (length % 2 != 0):
    cost += 1
    length += 1


half = int(length / 2)

i = 0
sum = padding
while sum < half:
    sum += listA[i]
    i += 1

if (sum != half):
    rightSum = 0
    for j in range(i - 1, N):
        rightSum += listA[j]

    if (sum > rightSum):
        sum -= listA[i - 1]
        print(int(rightSum - sum))
    else:
        rightSum -= listA[i - 1]
        print(int(sum - rightSum))
else:
    print(0)

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
mlmis = lambda: [-int(x) for x in input().split()]
INF = float('inf')


def main():
    N = ii()
    A = lmis()
    S = sum(A)
    ans = INF
    s = 0
    for a in A:
        s += a
        ans = min(ans, abs((S-s)-s))
    print(ans)


main()

Solution (Python):
import numpy as np
N = list(map(int, input().split()))[0]
A = np.array(list(map(int, input().split())))
def get_result(ary):
    cumsum_left = np.cumsum(ary)
    cumsum_right = np.cumsum(ary[::-1])
    d = np.abs(cumsum_left[:-1] - cumsum_right[::-1][1:])
    return np.min(d)
print(get_result(A))

Solution (Python):
N = int(input())
A = input().split()
A = [int(a) for a in A]
sum_ = sum(A)
sum_2 = sum_/2

X = 0
s = 0
for i in range(N):
    s += A[i]
    X += A[i]
    if(s>=sum_2):
        break

l1 = X-A[i]
l2 = sum_-X

data = [abs(l1+A[i]-l2),abs(l2+A[i]-l1)]
print(min(data))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = sum(a)
left = 0
right = sum(a)
for i in range(n):
  left += a[i]
  right -= a[i]
  if abs(left-right) < ans:
    ans = abs(left-right)
print(ans)

Solution (Python):
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    accum = [0] * (N-1)
    accum[0] = A[0]
    for i in range(1, N-1):
        accum[i] = A[i] + accum[i-1]
    mini = -1
    s = sum(A)
    for i in range(N-1):
        if mini == -1:
            mini = abs(s - 2 * accum[i])
        else:
            mini = min(mini, abs(s - 2 * accum[i]))
    print(mini)
main()

Solution (Python):
from collections import deque
n=int(input())
A=list(map(int,input().split()))
A=deque(A)
l,r=0,0
for i in range(n-1):
    num_o,num_t=A[0],A[-1]
    if l+num_o>=r+num_t:
        r +=num_t
        A.pop()
    else:
        l +=num_o
        A.popleft()
print(min(r+A[0]-l,l+A[0]-r))

Solution (Python):
N = int(input())
P = [int(_) for _ in input().split()]

d = sum(P)
L = sum(P)
S_left = 0
S_right= 0

for i in range(N):
    S_left += P[i]
    S_right = L - S_left
    d_temp = S_left - S_right
    
    if d >= abs(d_temp):
        d = abs(d_temp)
    
    else:
        break

    if d == 0:
        break

print(abs(d))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

S = sum(A)
i = 0
S1 = 0
while(S1 < S/2):
  S1 += A[i]
  i += 1
if i == N:
  ans = abs(sum(A[:i-1])-sum(A[i-1:]))
else:
  ans = min(abs(sum(A[:i-1])-sum(A[i-1:])),abs(sum(A[:i])-sum(A[i:])))
print(ans)

Solution (Python):
n = int(input())
num_list = list(map(int,input().split()))
min_diff = 10 ** 18
sum_list = sum(num_list)
count = 0
for i in range(n):
    count += num_list[i]
    diffs = abs(count*2 - sum_list)
    if min_diff < diffs:
        print(min_diff)
        exit()
    else:
        min_diff = diffs

Solution (Python):
import sys
input = sys.stdin.readline

_n = int(input())
_alist = list(map(int, input().split()))


def chker(n, alist):
    length = sum(alist)
    ls, rs = 0, length

    cost = length
    for index, ai in enumerate(alist):
        ls += ai
        rs -= ai
        precost = cost
        cost = min(abs(rs - ls), cost)

        if cost >= precost:
            print(precost)
            return


chker(_n, _alist)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
all_a = sum(a)
half_a = all_a / 2
a_sum = 0
for j,i in enumerate(a):
    if a_sum + i < half_a:
        a_sum += i
    else:
        if abs(a_sum -  half_a) >= abs((a_sum + i) -  half_a):
            print(abs((all_a - 2*(a_sum + i)) ))
            break
        else:
            print(abs(all_a - 2*(a_sum)))
            break

Solution (Python):
import numpy as np

n=int(input())
a=list(int(x) for x in input().split())
bou_length=0
l=[]
for i in range(len(a)):
  bou_length += a[i]
  l.append(bou_length)



def getNearestValue(list, num):
    """
    概要: リストからある値に最も近い値を返却する関数
    list: データ配列
    num: 対象値
    return 対象値に最も近い値
    """

    
    idx = np.abs(np.asarray(list) - num).argmin()
    return list[idx]

diff=getNearestValue(l, (bou_length/2))

print(abs(l[-1]-2*diff))

Solution (Python):
N = int(input())
a = list(map(int,input().split()))

if N == 2:
  print(abs(a[0]-a[1]))
else:
  s = sum(a)
  i = 0
  b = 0
  for i in range(N):
    if b<s/2:
      b += a[i]
    else:
      b = b-a[i-1]
      c = a[i-1]
      break
  h = s-b-c
  if h>b:
    print(b+c-h)
  elif b>h:
    print(h+c-b)
  else:
    if N%2==0:
      print(0)
    else:
      print(c)

Solution (Python):
n = int(input())
L = list(map(int, input().split()))

total = sum(L)
mid = total / 2
accum = 0
for i, elm in enumerate(L):
    accum += elm
    if accum == mid:
        print(0)
        break
    elif accum > mid:
        right = accum - mid
        left = elm - right
        print(int(min(left, right) / 0.5))
        break

Solution (Python):
N = int(input())

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

Asum = 0
A_sum = []

for i in range(N):
    Asum += A[i]
    A_sum.append(Asum)

for i in range(N):
    A_sum[i] = 2*A_sum[i] - Asum
    if A_sum[i] < 0:
        A_sum[i] = -1 * A_sum[i]

print(min(A_sum))

Solution (Python):
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))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

Asum = sum(A)
sumdiflist = []

temp = 0
for i in range(N-1):
  temp += A[i]
  sumdif = 2*temp - Asum
  if(sumdif<0):
    sumdif = -sumdif
  sumdiflist.append(sumdif)

print(min(sumdiflist))

Solution (Python):
n= int(input())
a = list(int(i) for i in input().split())
import math
import sys

sta=sum(a)/2
sum=0
ans=0
for i in range(0,n-1):
    if sum==sta:
        print(0)
        sys.exit()
    elif sum<sta and sum+a[i]>sta:
        ans=min(sta-sum,sum+a[i]-sta)/0.5
        print(int(ans))
        sys.exit()
    else:
        sum+=a[i]

ans=min(sta-sum,sum+a[n-1]-sta)/0.5
print(abs(int(ans)))

Solution (Python):
N = int(input())
a = [int(x) for x in input().split()]

sl = [0] * (N+1)
for i in range(N):
    sl[i+1] = a[i] + sl[i]

half = sl[-1]/2
diff = []
for i in range(N):
    diff.append(abs(half - sl[i]))
l = sl[diff.index(min(diff))]
r = sl[-1] - l
print(abs(l-r))

Solution (Python):
n=int(input())
a=[0]+list(map(int,input().split()))
ave=sum(a)/2
num=0
for i in a:
    num+=i
    if num>=ave:
        xx=min(abs(ave-num),abs(ave-(num-i)))
        print(int(xx/0.5))
        exit()

Solution (Python):
N = int(input())
A_list = [int(i) for i in list(input().split())]

cost = 0
right = 0
old_right = 0
length = 0
for i in range(N):
    length += A_list[i]
pivot = length/2
for i in range(N):
    right += A_list[i]
    if pivot == right:
        break
    if pivot < right:
        if pivot - old_right <= right - pivot:
            cost = pivot - old_right
            break
        else:
            cost = right - pivot
            break
    old_right = right

print(int(2*cost))

Solution (Python):
import itertools
import numpy as np

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

def getNearestValue(list, num):
    
    idx = np.abs(np.asarray(list) - num).argmin()
    return idx

sum = ac[-1]
half = sum/2
near = getNearestValue(ac,half)
dif = abs(ac[near]-half)
print(int(dif * 2))

Solution (Python):
import sys
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
totl=0
idx=0
dif1=0
for i in range(n):
  totl+=a[i]
  idx=i
  if (s/2)==totl:
    print(0)
    sys.exit()
  elif (s/2)<totl:
    dif1=totl-(s/2)
    break
dif2=a[idx]-dif1
if dif1-(dif1//1)==0:
  print(int(2*min(dif1,dif2)))
else:
  print(int(2*min(dif1,dif2)))

Solution (Python):
import sys
from itertools import accumulate
inputs = sys.stdin.readlines()
n = int(inputs[0])
A = list(map(int, inputs[1].split()))

total = sum(A)
B = list(accumulate(A))
C = [total - 2*b for b in B]
d = min((abs(c) for c in C))
print(d)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
l = A.pop(0)
idxl = 0
r = A.pop(-1)
idxr = -1

for i in range(n-2):
  if r<l:
    r+=A[idxr]
    idxr -= 1
  else:
    l+=A[idxl]
    idxl += 1
  
print(abs(r-l))

Solution (Python):
def A():
    temp = [int(i) for i in input().split()]
    ans = 0
    for n in temp:
        if n<4:
            ans += (4-n)*100000
    if temp[0] == 1 and temp[1] == 1:
        ans += 400000
    print(ans)

def B():
    N = int(input())
    lengths = [int(i) for i in input().split()]
    total = sum(lengths)
    temp_sum = lengths[0]
    ans = float('inf')
    for i in range(1, N):
        ans = min(ans, abs(total-2*temp_sum))
        
        temp_sum += lengths[i]
    
    print(ans)
        
B()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))



sumA = [0]
buf = 0
for i in A:
    buf += i
    sumA.append(buf)

ans = []
for i in sumA:
    if i >= sumA[-1] / 2:
        ans.append(temp)
        ans.append(i)
        break
    temp = i

print(int(min(abs(sumA[-1] / 2 - ans[0]), abs(sumA[-1] / 2 - ans[1])) * 2))

Solution (Python):
from itertools import accumulate
 
n = int(input())
A = list(map(int, input().split()))
 
prefix = list(accumulate(A))
total = prefix[-1]
seq = min(range(n), key=lambda x: abs(prefix[x]-total/2))
alt = prefix[seq]
print(abs(total - 2*alt))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
l = sum(a)
h = l/2
b, c = 0, 0
ans = 0
for i in a:
    b = c
    c += i
    if c == h:
        print(0)
        break
    if h < c:
        ans = c-l+c if c-h<h-b else l-b-b
        print(ans)
        break

Solution (Python):
import bisect 
n = int(input())
a = list(map(int, input().split()))
if n == 2:
  print(abs(a[0]-a[1]))
  exit()
com_a = [0 for _ in range(n)]
com_a[0] = a[0]
for i in range(1, n):
  com_a[i] = a[i] + com_a[i-1]

sum_a = sum(a)
middle  = 0
left = 0
for i in range(n):
  if com_a[i] > sum_a/2:
    if com_a[i] - sum_a/2 > sum_a/2 - com_a[i-1]:
      middle = i-1 
    else:
      middle = i
      left += a[i]
    break
  left += a[i]

right = sum_a - left
print(abs(left-right))

Solution (Python):
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()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = sum(a)
c = 0
for i in range(len(a)):
  c += a[i]
  if c >= b/2:
    d = i
    sum_b = c
    sumsum_b = c - a[i]
    break
if d == 0:
  ans = 2*a[0] - b
elif d == n-1:
  ans = b - 2*sumsum_b
else:
  e = sum_b
  f = sumsum_b
  if e + f >= b:
    ans = b - 2*f
  else:
    if sum_b - d - 1 < 2*e - b:
      ans = b - 2*f
    else:
      ans = 2*e - b
print(ans)

Solution (Python):
import bisect

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

acc_A = [0]
for i in range(N):
    acc_A.append(acc_A[-1] + A[i])
acc_A.append(acc_A[-1])
half = acc_A[-1] / 2
mid = bisect.bisect_left(acc_A, half)

if acc_A[mid] == half:
    ans = 0
else:
    ans = min(abs((acc_A[mid] - acc_A[0]) - (acc_A[-1] - acc_A[mid])),
              abs((acc_A[mid - 1] - acc_A[0]) - (acc_A[-1] - acc_A[mid - 1])))
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
suma=sum(a)
cost=float('inf')
tmp=0
ret = 0
for i in range(n):
    tmp+=a[i]
    ret=abs(tmp-(suma-tmp))
    cost=min(cost,ret)

print(cost)

Solution (Python):
N=int(input())

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

arr = []

a = 0
total = sum(List)

for i in range(N):
  a = a + List[i]
  b = total - a
  
  if a == b:
    ans = 0
  elif a > b:
    ans = a - b
  elif b > a:
    ans = b - a
  
  arr.append(ans)


print(min(arr))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
costs = [0 for x in range(N-1)]
mem = [0 for x in range(N-1)]

for i in range(1, N):
  if i == 1:
    mem[i-1] = A[i-1] * 2
  else:
    mem[i-1] = mem[i-2] + A[i-1] * 2
  costs[i-1] = abs(total - mem[i-1])
  
print(min(costs))

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

bar_len = sum(A)

target = bar_len / 2

value = 0
index = 0
flag = 0
for i in range(len(A)):
    value += A[i]
    if value >= target:
        error1 = value - target
        error2 = target + A[i] - value
        if error1 < error2:
            top_list = A[: i+1]
            back_list = A[i+1 :]
            break
        else:
            top_list = A[: i]
            back_list = A[i :]
            break

top = sum(top_list)
back = sum(back_list)

result = max(top - back, back - top)
print(result)

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


sum = 0
for i in range(n):
    sum += a[i]

sum2 = 0
for i in range(n):
    sum2 += a[i]
    x = sum2*2 - sum
    if x >= 0:
        y = sum - (sum2-a[i])*2
        if x <= y:
            print(x)
            break
        else:
            print(y)
            break

Solution (Python):
import sys
from pprint import pprint

def solve(n, a):
    all_s = sum(a)
    half = all_s / 2
    s = 0
    for a_i in a:
        s += a_i
        if s >= half:
            if s - half >= half - (s - a_i):
                print(all_s - 2*s + 2*a_i)
            else:
                print(2*s - all_s)
            return

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    a = list(map(int, sys.stdin.readline().strip().split(" ")))
    solve(n, a)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

total_length = sum(A)
left_length = 0
for i in range(N):
    left_length += A[i]
    if left_length == total_length / 2:
        print(0)
        break
    elif left_length > total_length / 2:
        right_length = total_length - left_length
        left_length -= A[i]
        if left_length < right_length:
            print(left_length + A[i] - right_length)
        else:
            print(right_length + A[i] - left_length)
        break

Solution (Python):



def solve(*args: str) -> str:
    n = int(args[0])
    A = tuple(map(int, args[1].split()))

    left = 0
    right = sum(A)

    ret = 0
    for i in range(0, n):
        r_g = right - left
        left += A[i]
        right -= A[i]
        if left == right:
            break
        elif left > right:
            l_g = left - right
            ret = min(r_g, l_g)
            break

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
n = int(input())
ls_a = list(map(int, input().split()))

s = 0
S = 0
half = sum(ls_a)/2

for i in range(n):
    s = S
    S += ls_a[i]
    if S > half:
        break

print(int(min(abs(half - s)*2, abs(half - S)*2)))

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

l = sum(a)

sumation = 0
for i in range(n):
    sumation += a[i]
    if sumation >= l/2:
        print(min(int((sumation - l/2) * 2), int((l/2 - sumation + a[i]) * 2)))
        break

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

a_sum = sum(A)
left = A[0]
right = sum(A) - A[0]
ans = abs(left - right)
for i in range(1, N):
    left += A[i]
    right -= A[i]
    ans = min(abs(left - right), ans)
print(ans)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
total = sum(A_list)
half = total // 2
l = 0
r = len(A_list) - 1
l_len = 0
r_len = 0
while True:
	new_len = l_len + A_list[l]
	if new_len <= half:
		l_len = new_len
		l += 1
	else:
		break

while True:
	new_len = r_len + A_list[r]
	if new_len <= half:
		r_len = new_len
		r -= 1
	else:
		break

if l == r:
	cut_l = r_len + A_list[r] - l_len
	cut_r = l_len + A_list[l] - r_len
	print(min(cut_l, cut_r))
else:
	print(0)

Solution (Python):
N=int(input())
List=[int(i) for i in input().split()]

bou_half = sum(List)/2

bou_sum = 0
for i in List:
    bou_sum += i
    if bou_sum == bou_half:
        print(0)
        exit()
    if bou_sum > bou_half:
        if bou_sum - bou_half < bou_half - bou_sum + i: 
            print(int((bou_sum - bou_half) // 0.5))
            exit()
        else: 
            print(int((bou_half - bou_sum +i) // 0.5))
            exit()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
t = sum(a)
tmp = 0
m = 0
for item in a:
    if (tmp + item) >= t // 2:
        m = item
        break
    tmp += item

ans1 = abs(t-2*tmp)
tmp += m
ans2 = abs(t-2*tmp)
print(min(ans1,ans2))

Solution (Python):
n = int(input())
arr = [int(j) for j in input().split()]

su1 = 0
su2 = sum(arr)
abc = []
for i in range(n):
	su1 += arr[i]
	su2 -= arr[i]
	abc += [abs(su1-su2)]

print(min(abc))