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):
import math
import random
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
N = int(input())
A = [int(i) for i in input().split()]
B =sum(A)
count =0
num = 0
while count < B//2:
    count +=A[num]
    num += 1
count_alt = count - A[num-1]
print(min(abs(B-count*2),abs(B-count_alt*2)))

Solution (Python):
N, *A = map(int, open(0).read().split())
cnt = 0
tgt = sum(A) // 2
for i in range(N):
    cnt += A[i]
    if cnt >= tgt:
        break
        
a1 = abs(cnt - sum(A[i + 1:]))
a2 = abs((cnt - A[i]) - sum(A[i:]))
print(min(a1, a2))

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

total_length = sum(A)
left_length = 0
for i in range(len(A)):
    left_length += A[i]
    if left_length * 2 >= total_length:
        break
cost_from_left = left_length * 2 - total_length

right_length = 0
for j in reversed(range(len(A))):
    right_length += A[j]
    if right_length * 2 >= total_length:
        break
cost_from_right = right_length * 2 - total_length

print(min(cost_from_left, cost_from_right))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
m = S
s = 0
a = 10**100
for i in range(N):
    m = m - A[i]
    s += A[i]
    if s > m:
        break
    a = m - s
b = s - m
ans = min(a,b)
print(ans)

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

left_side = 0
right_side = 0
ans = 1e12

left_wa = [0, A[0]]
right_wa = [A[-1]]

for i in range(1, N):
    left_wa.append(left_wa[-1] + A[i])
for i in range(2, N+1):
    right_wa.append(right_wa[-1] + A[-i])





for i in range(1, N):
    left_side = left_wa[i]
    right_side = right_wa[-(i+1)]
    
    ans = min(ans, abs(left_side-right_side))
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
all_val = sum(A)
left = all_val // 2
right = (all_val+1) // 2
sub_sum = 0
for val in A:
    old = sub_sum
    sub_sum += val
    if sub_sum >= left:
        ans = 2 * min(abs(sub_sum - left),abs(left - old) , abs(sub_sum - right), abs(right - old))
        if ans % 2 != all_val % 2:
                ans += 1
        print(ans)
        break

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = 0
sum = 0
l = []
for i in a:
    sum += i
    l.append(sum)
l2 = [sum - x for x in l]
num = 0
loc = 0
for i in range(n):
    if i == 0:
        num = abs(l[i] - l2[i])
    else:
        if num > abs(l[i] - l2[i]):
            num = abs(l[i] - l2[i])
            loc = i


print(num)

Solution (Python):
import bisect
N=int(input())
As=list(map(int,input().split()))
S=sum(As)
l=[]
x=0
for i in range(N):
  x+=As[i]
  l.append(x)
ha=bisect.bisect_left(l,S/2)
sa1=abs(l[ha]-(S-l[ha]))
if ha>0:
  sa2=abs(l[ha-1]-(S-l[ha-1]))
else:
  sa2=sa1+1
print(min([sa1,sa2]))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
total=sum(a)
ideal=sum(a)/2
tmp=10**20
goukei=0
for i in range(n):
  goukei+=a[i]
  if tmp>abs(ideal-goukei):
    tmp=min(tmp,abs(ideal-goukei))
  else:
    break
print(int(tmp*2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
k = 0
f = 0
for i in range(n):
  k += a[i]
  if k >= s//2:
    f = i
    break
k = max(k, s - k)
k2 = k - a[f]
k2 = max(k2, s - k2)
print(min(2*k - s, 2*k2 - s))

Solution (Python):

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

total = sum(A)
half = total/2

aa = 0

for i in range(N):
    aa += A[i]
    if aa > half:
        check_num = i
        break

cost = 0 
OK = False
        
if sum(A[:check_num+1]) < sum(A[check_num:]):
    B = A[:check_num+1]
    C = A[check_num+1:]
else:
    B = A[:check_num]
    C = A[check_num:]


print(abs(sum(B)-sum(C)))

Solution (Python):
N = int(input())
A = [int(x) for x in input().split(' ')]
assert(len(A) == N)
s = sum(A)
Ls = 0
ans = -1

for i in range(N-1):
    Ls += A[i]
    Rs = s - Ls
    diff = abs(Rs - Ls)
    if ans == -1 or diff < ans:
        ans = diff
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
alen = len(A)

r = 0
l = 0
p1, p2 = 0, 1
for i in range(N):
    if p1 + p2 > alen:
        break
    if r >= l:
        l += A[p1]
        p1 += 1
    else:
        r += A[-p2]
        p2 += 1 
print(abs(l- r))

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

h = sum(a)/2
s = 0
s2 = 0
for i in a:
    s += i
    if s >= h:
        break
a.reverse()
for i in a:
    s2 += i
    if s2 >= h:
        break

s = min([s,s2])
print(2*s-sum(a))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
d = sum(a)
L = d/2
s = 0

for i in range(n):
    s += a[i]
    new_d = abs(L-s)
    if new_d < d:
        d = new_d
    else:
        break

print(int(2*d))

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

sum_a = 0
for a in a_list:
    sum_a += a

sub_sum_a = 0

for index in range(len(a_list)):
    middle_index_big = index
    sub_sum_a += a_list[index]
    if sub_sum_a >= (sum_a // 2):
        break

diff_big_index = abs(sub_sum_a - (sum_a - sub_sum_a))
sub_sum_a_small = sub_sum_a - a_list[middle_index_big]
diff_small_index = abs(sub_sum_a_small - (sum_a - sub_sum_a_small))

diff = min(diff_big_index, diff_small_index)
print(diff)

Solution (Python):

n = int(input())
a = list(map(int, input().split()))
ls = [0 for _ in range(len(a))]

tmp = 0
for i, v in enumerate(a):
    tmp += v
    ls[i] = tmp

length = ls[-1]
half = length // 2

cand = 2020202020
for l in ls[:-1]:
    cand = min(cand, abs(l * 2 - length))
print(cand)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
l = sum(a)
ls = 0
md = 100000000000
for aa in a:
    ls += aa
    md = min(md, abs(l-2*ls))

print(md)

Solution (Python):
"""
NTC here
"""
from sys import stdin, setrecursionlimit
setrecursionlimit(10**7)

def iin(): return int(stdin.readline())
 
 
def lin(): return list(map(int, stdin.readline().split()))



def main():
    n=iin()
    a=lin()
    s2=sum(a)
    s1=0
    mn=abs(s2-s1)
    for i in range(n-1):
        s1+=a[i]
        s2-=a[i]
        mn=min(mn,abs(s1-s2))
    print(mn)

"""
1 2 5 8 15 20 23 11 45
"""
        


    
    
            



    
                











main()

Solution (Python):
N = int(input())
import numpy as np
A = np.array(list(map(int,input().split())))
cumsum = np.cumsum(A)

ans = float('inf')
for i in range(N-1):
    if abs(cumsum[i] - (cumsum[-1] - cumsum[i])) < ans:
        ans = abs(cumsum[i] - (cumsum[-1] - cumsum[i]))
print(ans)

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7


n = INT()
l = MAP()

s = sum(l)
tmp=0
for i in range(n):
    tmp+=l[i]
    if tmp >= s/2:
        p1 = tmp - (s/2)
        idx1 = i
        break

tmp=0
for i in range(n-1,-1,-1):
    tmp+=l[i]
    if tmp >= s/2:
        p2 = tmp - (s/2)
        idx2 = i
        break

if p1 <= p2:
    left = sum(l[:idx1+1])
    right = sum(l[idx1+1:])
else:
    left = sum(l[:idx2])
    right = sum(l[idx2:])
print(abs(right-left))

Solution (Python):
def answer():
    N = int(input())
    As = list(map(int, input().split()))

    total = sum(As)
    half = total//2

    sum_left = 0
    sum_left2 = 0
    idx = 0

    for i, l in enumerate(As[:-1]):
        sum_left += l
        if sum_left >= half:
            idx = i
            sum_left2 = sum_left - l
            break

    sum_right = sum(As[idx+1:])
    sum_right2 = sum(As[idx:])

    ans1 = abs(sum_right - sum_left)
    ans2 = abs(sum_right2 - sum_left2)

    print(min(ans1, ans2))


if __name__ == '__main__':
    answer()

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline






















def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	A = inputList()

	to = 0
	for i in A:
		to += i

	to = to // 2
	manaka = 0
	tmp = 0
	for i,val in enumerate(A):
		tmp += val
		if tmp >= to:
			manaka = i
			break

	
	

	to1 = 0
	to11 = 0
	to2 = 0
	to22 = 0

	to111 = 0
	to222 = 0

	for i,val in enumerate(A):
		if i >= manaka+1:
			to2 += val
		else:
			to1 += val

		if i >= manaka-1:
			to22 += val
		else:
			to11 += val

		if i >= manaka:
			to222 += val
		else:
			to111 += val

	ans = min(abs(to1-to2),abs(to11-to22),abs(to111-to222))
	print(ans)


if __name__ == "__main__":
	main()

Solution (Python):


def readInts():
  return [int(s) for s in input().split(" ")]

def solve(N, A):
  s = sum(A)

  sp = 0
  last = A[0]
  for a in A:
    sp += a
    if sp >= s / 2:
      last = a
      break

  half = sp
  if sp - (s / 2) > abs(sp - last - (s / 2)):
    half = sp - last

  return abs(2 * half - s)

def main():
  N = readInts()
  A = readInts()

  print(solve(N, A))

if __name__ == "__main__":
  main()

Solution (Python):
from itertools import accumulate
n = int(input())
a = [int(i) for i in input().split()]
aa = list(accumulate(a))
mind = aa[-1]
for ai in aa:
  d = abs(aa[-1] - ai - ai)
  if d < mind:
    mind = d
  else:
    break
print(mind)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
s0 = sum(a)
s = s0//2 + 1 
y = 0
x = 0
for i in range(n):
    if x >= s:
        y = i-1
        break
    else:
        x += a[i]
a11 = a[:y]
a12 = a[y:]
a21 = a[:y+1]
a22 = a[y+1:]
a11s = sum(a11)
a12s = sum(a12)
a21s = sum(a21)
a22s = sum(a22)
ans = min(abs(a11s-a12s), abs(a21s-a22s))
print(ans)

Solution (Python):
N = int(input())
A = [int(_)for _ in input().split()]
sumA = sum(A)
halfsumA = sumA / 2
pos = 0
for n, a in enumerate(A):
    pos += a
    if pos > halfsumA:
        l = pos - a
        r = pos
        
        if abs(l - halfsumA) < abs(r - halfsumA):
            ans = abs((sumA - l) - l)
        else:
            ans = abs((sumA - r) - r)

        break

print(ans)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline






















def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

def main():
	N = inputInt()
	A = inputList()

	to = 0
	for i in A:
		to += i

	to = to // 2
	manaka = 0
	tmp = 0
	for i,val in enumerate(A):
		tmp += val
		if tmp >= to:
			manaka = i
			break

	
	

	to1 = 0
	to11 = 0
	to2 = 0
	to22 = 0

	to111 = 0
	to222 = 0

	for i,val in enumerate(A):
		if i >= manaka+1:
			to2 += val
		else:
			to1 += val

		if i >= manaka-1:
			to22 += val
		else:
			to11 += val

		if i >= manaka:
			to222 += val
		else:
			to111 += val

	ans = min(abs(to1-to2),abs(to11-to22),abs(to111-to222))
	print(ans)


if __name__ == "__main__":
	main()

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

mean_a = sum(a)/2

mean_a_disc = 0
ans = 1000000
candidate = 0

for i in range(N):
    if mean_a_disc + a[i] < mean_a:
        mean_a_disc += a[i]
    elif mean_a_disc + a[i] > mean_a:
        candidate = min(mean_a_disc + a[i] -mean_a,mean_a- mean_a_disc)
        break
    else:
        ans = 0
        break
if candidate > 0:
    ans = int(candidate * 2)

print(ans)

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

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

len = sum(list)
nlen = 0
d = []
for i in list:
    nlen += i
    d.append(abs(nlen - (len - nlen)))

print(min(d))

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

sum_left, sum_right = 0, sum(A)
min_diff = sum_right

for i in range(N):
    sum_left += A[i]
    sum_right -= A[i]
    
    if min_diff < abs(sum_right - sum_left):
        break
    min_diff = abs(sum_right - sum_left)
print(min_diff)

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


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    total = sum(A)
    right = [0] * N
    left = [0] * N

    left[0] = A[0]
    for i in range(N - 1):
        left[i + 1] = A[i + 1] + left[i]

    right[N - 1] = A[N - 1]
    for i in range(N - 1, 0, -1):
        right[i - 1] += A[i - 1] + right[i]
    
    mi = float("inf")
    for i in range(N - 1):
        mi = min(mi, abs(left[i] - right[i + 1]))
    
    print(mi)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
a = na()
asum = sum(a)
s1 = 0
for ai in a:
    s0 = s1
    s1 += ai
    if s1 >= asum//2:
        print(min(asum-2*s0, 2*s1 - asum))

        exit()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
l=[0]*n
l[0]=a[0]
for i in range(1,n):
  l[i]=l[i-1]+a[i]
import bisect as bi
x=bi.bisect_left(l,s/2)
if l[x]==s/2:
  print(0)
else:
  print(min(s-l[x-1]*2,l[x]*2-s))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = sum(a)
al = ans
mod = 0
for i in range(n):
  if mod + a[i] == al - a[i]:
    ans = 0
    break
  elif ans > abs(mod - al + 2*a[i]):
    ans = abs(mod - al + 2*a[i])
    mod += a[i]
    al -= a[i]
  else:
    mod += a[i]
    al -= a[i]
print(ans)

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

accumulated_sums = []
tmp = 0
for a in A:
    tmp += a
    accumulated_sums.append(tmp)

min_cost = 2020202020
for ac_s in accumulated_sums:
    cur_cost = abs(accumulated_sums[-1] - 2 * ac_s)
    min_cost = min(min_cost, cur_cost)

print(min_cost)

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

L = sum(A)

sum_1 = 0

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

sum_2 = 0

for i in reversed(range(N)):
    sum_2 += A[i]
    if sum_2 >= L / 2:
        break

print(min(2 * sum_1 - L, 2 * sum_2 - L))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
total = 0
min_diff = 10**10
min_idx = 0
total_tmp = 0
for i in range(n):
    total += a[i]
    diff = abs(total - sum_a/2)
    if diff < min_diff:
        min_diff = diff
        min_idx = i
        total_tmp = total

print(abs(total_tmp - sum(a[min_idx+1:])))

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

bar = [0]
bar_tmp = 0
for Ai in A:
    bar_tmp += Ai
    bar.append(bar_tmp)

A_sum = sum(A)
targ = A_sum / 2

for i_r in range(N):
    if bar[i_r] > targ: break
i_l = i_r - 1

pen_l = abs(A_sum - 2 * bar[i_l])
pen_r = abs(2 * bar[i_r] - A_sum)

print(min(pen_l, pen_r))

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

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

N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
sum_A = np.sum(A)
res = np.cumsum(A)
value = getNearestValue(res, sum_A/2)
print(abs(value - (sum_A - value)))

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

accumulated_sums = []
tmp = 0
for a in A:
    tmp += a
    accumulated_sums.append(tmp)

min_cost = 2020202020
for ac_s in accumulated_sums:
    cur_cost = abs(accumulated_sums[-1] - 2 * ac_s)
    min_cost = min(min_cost, cur_cost)

print(min_cost)

Solution (Python):
def main():
    n = int(input())
    arr = list(map(int,input().split()))
    dp = []
    for i in arr:
        if not dp:
            dp.append(i)
        else:
            dp.append(dp[-1]+i)

    ans = float('inf')

    for i in range(n-1):
        left = dp[i]
        right = dp[-1]-dp[i]

        ans = min(ans,abs(left-right))

    print(ans)

main()

Solution (Python):



def f(A, k):
    s = 0
    for p, a in enumerate(A):
        if s+a > k:
            return (s, p)
        s += a


def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    S = sum(A)
    k = S/2
    (s, p) = f(A, k)
    
    x = int(min(k-s, s+A[p]-k)*2 + 0.5)
    print(x)



if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a_ls = list(map(int, input().split()))
sum_a_ls = sum(a_ls)

i = 0
short_bou = 0
half = sum_a_ls / 2
while True:
    if short_bou + a_ls[i] > half:
        break
    else:
        short_bou += a_ls[i]
        i+=1

if half == short_bou:
    print(0)
else:
    sub_diff = min(half - short_bou, short_bou + a_ls[i] - half)
    print(int(sub_diff * 2))

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

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

print(ans)

Solution (Python):


n = int(input())
a = list(map(int, input().split()))
x = 0
y = 0
be = 0
af = -1
for i in range(0, n):
    if x <= y:
        x += a[be]
        be += 1
    else:
        y += a[af]
        af -= 1
print(abs(x - y))

Solution (Python):
n = int(input())
a_s = list(map(int, input().split()))
sum_a_s = sum(a_s)

tmp_sum = 0
for i in range(n - 1):
	tmp_sum += a_s[i]
	if tmp_sum > sum_a_s - tmp_sum:
		break

tmp_res1 = abs(sum(a_s[:i]) * 2 - sum_a_s)
tmp_res2 = abs(sum(a_s[:i + 1]) * 2 - sum_a_s)
res = min(tmp_res1, tmp_res2)

print(res)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from bisect import bisect_left
from itertools import accumulate


n = ni()
a = list(li())

suma = sum(a)
cuma = [0] + list(accumulate(a))
half = suma / 2
halfidx = bisect_left(cuma, half) - 1
if halfidx == -1:
    print(cuma[1] - (cuma[n] - cuma[1]))
else:
    first = cuma[n] - cuma[halfidx] - cuma[halfidx]
    latter = cuma[halfidx+1] - (cuma[n] - cuma[halfidx+1])
    print(min(first, latter))

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

s=sum(l)
m=0
i=-1
while m<s//2:
    i+=1
    m+=l[i]
    


print(min(2*m-s,s-2*(m-l[i])))

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
DBG = True  and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
acc = [0]*(n+1)
for i in range(n):
    acc[i+1] = acc[i]+a[i]
h = acc[n]/2
j = -1
for i in range(n):
    if acc[i]<=h and h<acc[i+1]:
        j = i
        break
ddprint(acc)
ddprint(j)
if acc[j]==h:
    print(0)
    exit()
x = min(h-acc[j], acc[j+1]-h)
print(int(2*x))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
s = [0] * n
t = [0] * n
s[0] = a[0]
t[-1] = a[-1]
for i in range(1,n):
    s[i] = s[i-1] + a[i]
    t[-i-1] = t[-i] + a[-i-1]



m = min([abs(s[i]-t[i+1]) for i in range(n-1)])

print(m)

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

l = sum(a)
cur_l = 0
pre_l = 0
for i, ai in enumerate(a):
    cur_l += ai
    if l - 2*cur_l < 0:
        res = min(abs(l-2*cur_l), abs(l-2*pre_l))
        break
    pre_l = cur_l
print(res)

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

ans = 217937901270479417
_sum = sum(a)
sum_1 = 0
for i in range(0,len(a)):
    sum_1 += a[i]
    sum_2 = _sum - sum_1
    ans = min(ans, abs(sum_1 - sum_2))
print(ans)

Solution (Python):
n = int(input())
alist = list(map(int,input().split()))
length = 0
temp = 0
keypoint = 0

for a in alist:
    length += a

halflength = length/2
    
for i in range(len(alist)):
    temp += alist[i]
    if temp > halflength:
        keypoint = i
        break
        
beforeamari = abs(halflength-temp+alist[i])
afteramari = abs(halflength-temp)

if beforeamari>=afteramari:
    print(int(afteramari*2))
else:
    print(int(beforeamari*2))

Solution (Python):
from itertools import accumulate
import bisect

N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))
idx = bisect.bisect(acmA, acmA[-1]/2)
print(min(abs(acmA[idx-1]-(acmA[-1]-acmA[idx-1])),abs(acmA[idx]-(acmA[-1]-acmA[idx]))))

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

sum_all = sum(A)
sum_front = 0
ans = float('inf')
for a in A:
    sum_front += a
    ans = min(abs(2 * sum_front - sum_all), ans)
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = 0
s = sum(a)
if n==2:
    print(a[1]-a[0])
    exit()
i = 0
if s%2==0:
    while b<s//2:
        b+=a[i]
        i+=1
else:
    while b<=s//2:
        b+=a[i]
        i+=1
c =s-b
e = b-c
ans = min(abs(e-2*a[i-1]),abs(e))
print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
s = sum(a) / 2
i = 0
j=0
while i < n and j < s:
    j += a[i]
    i += 1
ans1 = abs(sum(a[:i]) - sum(a[i:n]))
ans2 = abs(sum(a[:i-1]) - sum(a[i-1:n]))
print(min(ans1, ans2))

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

a = sum(list1)/2
length = 0
index = 0


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

for i in range(n):
    if a <= length:
        index = i
        break
    length += list1[i]

o = sum(list1[:index])
p = sum(list1[index:])

q = sum(list1[:index-1])
r = sum(list1[index-1:])

print(min(abs(o-p),abs(q-r)))

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

minimum = sum(A)
a = 0
b = minimum
for i, l in enumerate(A):
  a += l
  b -= l
  minimum = min(minimum, abs(a-b))
print(minimum)

Solution (Python):
from itertools import accumulate
import numpy as np

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

acc = np.array(list(accumulate(A)))
l = -(acc - sum(A))

print(min(abs(acc-l)))

Solution (Python):
a=[]
b=int(input())
n=0
nn=0
nnn=0
mnn=0
hnn=0
hmnn=0
h=0
m=0
for x in input().split(" "):
    a.append(int(x))
for x in a:
    n+=x
for x in a:
    mnn=nn
    nn+=x
    if nn>=n/2:
        break
hnn=nn-n/2
hmnn=n/2-mnn
if hmnn>=hnn:
    h=hnn
else:
    h=hmnn
print(int(h*2))

Solution (Python):
from itertools import accumulate
import bisect
n = int(input())
als = list(map(int, input().split()))

ac = list(accumulate(als))

asum = sum(als)

trg = asum/2
idx = bisect.bisect_left(ac, asum/2)
if ac[idx] == trg:
    ans = 0
else:
    ans = min(trg - ac[idx-1], ac[idx] - trg)/0.5
print(int(ans))

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

S = [0] * (N+1)
for i in range(1,N+1):
    S[i] = S[i-1]+A[i-1]

L = S[-1]

pre_s = 0
for s in S:
    if s > L/2:
        break
    pre_s = s

l = L - 2*pre_s
r = 2*s-L
print(min(l,r))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=float('inf')
s=sum(a)
s1 = a[0]
s2 = s-s1
ans = min(ans, abs(s1 - s2))
for i in range(1,n):
    s1+=a[i]
    s2-=a[i]
    ans = min(ans, abs(s1 - s2))

print(ans)

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

cumsum = 0
pos=[]
total = sum(A)
for a in A:
    cumsum+=a
    pos.append(abs(total/2-cumsum))
print(int(min(pos)*2))

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

n = int(input())
a_list = list(map(int, input().split()))
a_half = sum(a_list) / 2

for i in range(n):
  a_half -= a_list[i]
  if a_half <= 0:
    
    row_answer = a_half
    high_answer = a_half+a_list[i]
    break

print(int(min(-1 * row_answer, high_answer) * 2))

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

d = sum(A)
mi = d
for a in A:
    d -= 2*a
    mi = min(mi, abs(d))

print(mi)

Solution (Python):
from sys import stdin
from itertools import repeat
def main(ab=abs):
    n = int(stdin.readline())
    a = map(int, stdin.readline().split(), repeat(10, n))
    s = sum(a)
    t = 0
    ans = s
    for i, x in enumerate(a):
        t += x
        s -= x
        c = ab(s - t)
        if ans > c:
            ans = c
    print ans
main()

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

sum_A = sum(A_list)
acc_A = [0] * N
acc_A[0] = A_list[0]
border = sum_A / 2
for i in range(1, N):
    acc_A[i] += A_list[i] + acc_A[i-1]

for i in range(N):
    if acc_A[i] > border:
        border_i = i
        break

left = abs(2 * acc_A[border_i - 1] - sum_A)
right = abs(2 * acc_A[border_i] - sum_A)

if left > right:
    ans = right
else:
    ans = left

print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
all_sum = sum(A)
left_sum = 0
ans = 1<<40
for a in A:
	left_sum += a
	right_sum = all_sum - left_sum
	ans = min(ans, abs(right_sum - left_sum))
print(ans)

Solution (Python):
from collections import deque
N = int(input())
A = deque(list(map(int,input().split())))
L = 0
R = 0
while len(A) != 0:
    if L < R:
        L += A.popleft()
    else:
        R += A.pop()
print(abs(R-L))

Solution (Python):
from itertools import accumulate

N = int(input())
A = list(map(int, input().split()))
B = list(accumulate(A))
ans = float('inf')
for i in range(N-1):
    ans = min(ans, abs(B[i]-(B[-1]-B[i])))

print(ans)

Solution (Python):


n=int(input())
a=list(map(int,input().split()))
l=[]
c=sum(a)/2

mi=10**12
tmp=0

for i in range(n):
    tmp+=a[i]
    l.append(tmp)
    if mi > abs((c-l[i])):
        mi=abs(c-l[i])
        k=i

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

Solution (Python):
def main():
    
    ''' Cook your dish here! '''
    n = get_int()
    li  = get_list()
    full_sm = sum(li)
    sm = 0
    res = full_sm
    for i in li:
        sm += i
        right = full_sm - sm
        res = min(res, abs(sm-right))
    print(res)



''' Coded with love at India by Satyam Kumar '''
import sys

import math

out = []
get_int = lambda: int(input())
get_list = lambda: list(map(int, input().split()))
main()

print(*out, sep='\n')

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

r = [0 for i in range(N)]
r[0] = A[0]
sumA = sum(A)
for i in range(1, N):
    r[i] = r[i-1] + A[i]

ans = abs(2 * r[0] - sumA)
for i in range(N):
   ans = min(abs(2 * r[i] - sumA), ans)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=sum(a)
d=c/2
cnt=0
count=0
for i in range(n):
    cnt+=a[i]
    if cnt>=d:
        if abs(cnt-a[i]-d)<abs(cnt-d):
            cnt-=a[i]
            count=i
        else:
            count=i
        break
print(abs(2*cnt-c))

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

b=itertools.accumulate(a)
b=list(b)
x=float('inf')
for i in range(n):
    x=min(abs(b[i]-(b[-1]-b[i])),x)

print(x)

Solution (Python):
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    m = sum(A) // 2

    s = 0
    c = 0
    while s < m:
        s += A[c]
        c += 1

    d1 = abs(sum(A[:c]) - sum(A[c:]))
    d2 = abs(sum(A[:c-1]) - sum(A[c-1:]))

    if d1 < d2:
        print(d1)
    else:
        print(d2)


main()

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

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

tot = sum(ls)

p = 0
for i in range(n):
  p += ls[i]
  if p>= tot/2:
    break
tmp = 2*p-tot

p = 0
for i in range(n-1,-1,-1):
  p += ls[i]
  if p>= tot/2:
    break
print(min(tmp,2*p-tot))

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
c = 0
k = sum(a)
for i in range(n):
    if (c + a[i] >= k/2):
        d = i
        break
    c += a[i]
print(min(k-2*c,2*(c+a[d])-k))

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

acum = np.cumsum(A)

print(int(min(abs(acum-(sum(A)/2)))*2))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
t = 0
i = 0
while True:
  t += a[i]
  if 2*t >= s:
    break
  i += 1
print(min(2*t-s,s-2*t+2*a[i]))

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
first = 0
goukei = sum(li)
ans = goukei

for i in range(n):
    first += li[i]
    rest = goukei - first
    if abs(first-rest) < ans:
        ans =  abs(first-rest)
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()
acc = [0]
for x in ls:
    acc.append(acc[-1] + x)
res = float('inf')
for x in acc:
    res = min(res, abs(acc[-1] - 2 * x))
print(res)

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

B = set()
x = 0
L = sum(A)
for i in A:
    x += i
    B.add(abs(abs(L-x)-x))

print(min(B))

Solution (Python):




def main():
  input()
  v = list(map(int, input().split()))
  prev_value, post_value = 0, 0
  prev_index, post_index = 0, len(v) - 1

  while prev_index <= post_index:
    if prev_value < post_value:
      prev_value += v[prev_index]
      prev_index += 1
    else:
      post_value += v[post_index]
      post_index -= 1

  print(abs(prev_value - post_value))


if __name__ == '__main__':
  main()