Problem:


Score : 300 points

Problem Statement

Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer a_i written on it.

They will share these cards.
First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards.
Here, both Snuke and Raccoon have to take at least one card.

Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively.
They would like to minimize |x-y|.
Find the minimum possible value of |x-y|.

Constraints

2 \leq N \leq 2 \times 10^5

-10^{9} \leq a_i \leq 10^{9}

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x-y|=1. This is the minimum possible value.

Sample Input 2

2
10 -10

Sample Output 2

20

Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=-10, and thus |x-y|=20.



Sample input:
6
1 2 3 4 5 6


Sample output:
1

Submission status: Accepted

Solution code:
def solution(a):
    if len(a) == 2:
        return abs(a[0] - a[1])

    total =sum(a)
    min_diff = 999999999999999
    part_of_sum = 0
    for i in range(len(a)-1):
        part_of_sum += a[i]
        total -= a[i]
        if abs(part_of_sum - total) < min_diff:
            min_diff = abs(part_of_sum - total)

    return min_diff


N = int(input())
a = [int(value) for value in input().split(' ')]
print(solution(a))

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))

a_sum = sum(a_list)
l_a_sum = a_list[0]
min_diff = abs(a_sum - 2 * l_a_sum)
for i in range(1, N-1):
    l_a_sum += a_list[i]
    min_diff = min(min_diff, abs(a_sum - 2 * l_a_sum))
print(min_diff)

Submission status: Accepted

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
total = sum(num_list)
dis = []
a=0

for i in range(n-1):
    a += num_list[i]
    b = total - a
    dis.append(abs(a-b))

print(min(dis))

Submission status: Accepted

Solution code:
def read(): return list(map(int, input().split()))


n = int(input())
a = read()

tot = sum(a)
for i in range(n - 1):
    a[i + 1] += a[i]

ans = 1145141919810893
for i in range(n-1):
    x = a[i]
    y = tot - x
    ans = min(ans, abs(x - y))

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
an=list(map(int,input().split()))
s=sum(an)
b = an[0]

l=[]
a = abs(s-2*b)
for i in range(1,N-1):
    b = b + an[i]
    a=min(a,abs(s-2*b))
print(a)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

l = 0
r = sum(a)

d = []

for i in range(n - 1):
    l += a[i]
    r -= a[i]
    d.append(abs(l - r))

print(min(d))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

left = 0
right = sum(a)

diff = []

for i in range(n - 1):
    left += a[i]
    right -= a[i]
    diff.append(abs(left - right))

print(min(diff))

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
sum_arr = sum(arr)
ans=[]
x = 0
for i in range(n-1):
    x += arr[i]
    y = sum_arr - x
    ans.append(abs(x - y))
print(min(ans))

Submission status: Accepted

Solution code:
# encoding:utf-8

def main():
	N = int(input())
	cards = list(map(int, input().split()))

	x = cards[0]
	y = sum(cards[1:])
	i = 1
	val = abs(x - y)

	while i < len(cards) - 1:
		x += cards[i]
		y += -cards[i]
		temp = abs(x - y)
		
		if temp == 0:
			val = 0
			break
		if temp < val:
			val = temp

		i += 1

	print(val)

if __name__ == '__main__':
	main()

Submission status: Accepted

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

sunuke = 0
araiguma = sum(a)

min = 10000000000
for i in range(n-1):
    araiguma -= a[i]
    sunuke += a[i]
    if min > abs(sunuke - araiguma):
        min = abs(sunuke - araiguma)

print(min)

Submission status: Accepted

Solution code:
if(__name__ == "__main__"):
    N = int(input())
    a = [int(i) for i in input().split()]
    #print(a,b)
    
    #print(maxlist)
    sumsunuke = a[0]
    arai = a[1:]
    sumarai = sum(arai)
    #darai = deque(arai)
    #print(sumsunuke)
    #print(sumarai)
    distmin = abs(sumsunuke - sumarai)
    
    for i in range(1,N-1):
        sumsunuke += a[i]
        sumarai -= a[i]
        #print(sunuke)
        #print(darai)
        dist = abs(sumsunuke - sumarai)
        if(distmin > dist):
            distmin = dist
    
    print(distmin)

Submission status: Accepted

Solution code:
# Splitting Pile
N = int(input())
a = list(map(int, input().split()))
 
s = [sum(a[1:]) - a[0]]
for i in range(N-2):
    s.append( s[i]- 2*a[i+1] )
a = list(map(abs, s))
print(min(a))

Submission status: Accepted

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

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

SUM_CARD = sum(a)
i=0

sum_before = a[i]
diff_sum = abs(sum_before*2-SUM_CARD)
i+=1

while(i<N-1):
    sum_before = sum_before + a[i]
    diff_sum_new = abs(sum_before*2-SUM_CARD)
    if diff_sum_new<diff_sum:
        diff_sum = diff_sum_new
    i += 1

print(diff_sum)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(item) for item in input().split()]

sunuke = sum(a[1:])
raccoon = a[0]
minimum = abs(sunuke - raccoon)
for i in range(1, N):
    if(abs(sunuke - raccoon) < minimum):
        minimum = abs(sunuke - raccoon)
    sunuke -= a[i]
    raccoon += a[i]
       
print(minimum)

Submission status: Accepted

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())

sum1 = a[0]
sum2 = sum(a[1:])
diff = [abs(sum1-sum2)]
for i in range(1,N-1):
  sum1 += a[i]
  sum2 -= a[i]
  diff.append(abs(sum1-sum2))

# diff = [abs(sum(a[select:])-sum(a[:select])) for select in range(1,N)]
print min(diff)

Submission status: Accepted

Solution code:
N = int(input())
src = list(map(int, input().split()))

s = [0]
for i in range(N):
    s.append(s[-1] + src[i])

ans = float('inf')
for i in range(N-1):
    a = s[i+1]
    b = s[-1] - s[i+1]
    df = abs(a-b)
    ans = min(ans, df)
print(ans)

Submission status: Accepted

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

total=sum(a)

ans=[]
x=0
for i in range(n-1):
    x+=a[i]
    y=total-x
    ans.append(abs(x-y))

print(min(ans))

Submission status: Accepted

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

total = sum(a)

acc_sum = [0] * N
base = 0
for i in range(N):
    base += a[i]
    acc_sum[i] = base

diff = [abs(acc * 2 - total) for acc in acc_sum]
print(min(diff[:-1]))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

N = np.int64(input())
a = np.array([int(x) for x in input().split()])

min = int(1e9 + 7)
temp = int(1e9 + 7)

a_cumsum = a.cumsum()
a_sum = a.sum()

score = []

for i in range(0, N-1):
    sunuke = a_cumsum[i]
    kuma = a_sum - sunuke
    score.append(np.abs(sunuke - kuma))

print(np.array(score).min())

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
sunuke = []
diff = []
total = sum(a)
for i, x in enumerate(a[0:-1]):
    if(i==0):
        sunuke.append(x)
    else:
        sunuke.append(x + sunuke[i-1])
for i in sunuke:
    diff.append(abs((i * 2) - total))
print(min(diff))

Submission status: Accepted

Solution code:
n= int(input())
card = input().split(' ')
card = [int(s) for s in card]
min_s = 20000000000
sum1 = sum(card[:1])
sum2 = sum(card[1:])
for i in range(n - 1):
    dif  = abs(sum1-sum2)

    if dif  < min_s:
        min_s = dif
    sum1 += card[i+1]
    sum2 = sum2 - card[i+1]
print(min_s)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

ls = []
A = sum(ax[:1])
B = sum(ax[1:])

for i in range(1, N):
    sub = abs(A-B)
    ls.append(sub)
    if(sub is 0):
        break
    A += ax[i]
    B -= ax[i]

print(sorted(ls)[0])

Submission status: Accepted

Solution code:
import sys

N = int(input())
Cards = list(map(int, input().split()))
if N==2:
    print(abs(Cards[0] - Cards[1]))
    sys.exit()
B = sum(Cards) - Cards[0]
A = Cards[0]

del Cards[0]
del Cards[-1]
answer = 100000000

if N==2:
    print(abs(Cards[0] - Cards[1]))
    sys.exit()
for number in Cards:
    A += number
    B -= number
    if abs(A-B) < answer:
        answer = abs(A-B)

print(answer)

Submission status: Accepted

Solution code:
N = map(int, raw_input().split())
a = map(int, raw_input().split())
xsum = 0
for i in range(len(a)-1):
    xsum += a[i]
dif = xsum - a[-1]
m = abs(dif)
for i in range(1,len(a)-1)[::-1]:
    dif -= 2*a[i]
    if m > abs(dif):
        m = abs(dif)
print(abs(m))

Submission status: Accepted

Solution code:
def calc(x,y):
    z = x+y
    b.append(z)
    return z
from functools import reduce
import numpy as np
N = int(input())
a = list(map(int, input().split()))
s = sum(a)
b = [a[0]]
reduce(calc ,a)
print(min(abs(2 * np.array(b[0:N-1]) -s )))

Submission status: Accepted

Solution code:
nb = int(input())
a = list(map(int,input().split()))
t = sum(a)
g = nb * 1000000000000
q = 0
for i in range(nb - 1):
   q += a[i]
   p = t - q
   diff = abs(q-p)
   if diff < g:
      g = diff
print(g)

Submission status: Accepted

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

c = [2*a[0]-sum(a)]
d = [abs(c[0])]
for i in range(N-2):
	c.append(c[i]+2*a[i+1])
	d.append(abs(c[i+1]))

print(min(d))

Submission status: Accepted

Solution code:

s = input().split()
s = [int(i) for i in s]
N = s[0]

s = input().split()
a = [int(i) for i in s]

L = [0] * N
L[0] = a[0] - sum(a[1 : ])

if N == 2 : 
    print(abs(a[0] - a[1]))
else : 
    MIN = float("inf")
    for i in range(1 , N - 1 ) : 
        L[i] = L[i - 1] + 2 * a[i]
        if MIN > min( abs(L[i]) , abs(L[i - 1])) : MIN = min( abs(L[i]) , abs(L[i - 1]))
    print(MIN)

Submission status: Accepted

Solution code:
#!/usr/bin/python

import sys

def main(argv):
    N = int(sys.stdin.readline())
    while N:
        A = map(lambda x: int(x), raw_input().split(" ", N))
        snuke = A[0]
        raccon = sum(A[1:])
        difference = abs(snuke - raccon)
        for i in xrange(1, len(A) - 1):
            snuke += A[i]
            raccon -= A[i]
            d = abs(snuke - raccon)
            if d < difference:
                difference = d
        print(difference)

        N = sys.stdin.readline()
        if N:
            N = int(N)

if __name__ == "__main__":
    main(sys.argv)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(1, N):
    res += a[i]
diff = a[0]-res
abs_ = abs(diff)
for i in range(1, N-1):
    diff += 2*a[i]
    abs_ = min(abs_, abs(diff))

print(abs_)

Submission status: Accepted

Solution code:
# import numpy as np
N = input()
# a =  np.araray((map(int, raw_input().split())))
# sum_a = np.sum(a)
a =  list(map(int, raw_input().split()))
sum_a = sum(a)

sum_top = a[0] + 0
sum_bottom = a[-1] + 0
cur_sum = sum_top

for i in range(1, N - 1):
    sum_top += a[i]
    if abs(sum_a / 2. - sum_top) <= abs(sum_a / 2. - cur_sum):
        cur_sum = sum_top

print abs(2 * cur_sum - sum_a)

Submission status: Accepted

Solution code:
n= int(input())

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

m = None
suke_sum = 0
araiguma_sum = sum(a)

for suke_n in range(1, n):
    # araiguma_n = n - suke_n
    # suke_sum = sum(a[0:suke_n])
    suke_sum += a[suke_n - 1]
    # araiguma_sum = sum(a[suke_n:n])
    araiguma_sum -= a[suke_n - 1]
    ans = abs(suke_sum - araiguma_sum)
    if m is None or m > ans:
        m = ans

print(m)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    sum_ = sum(A)
    min_ = float('inf')
    x = y = 0
    for i in range(N - 1):
        x += A[i]
        y = sum_ - x
        if abs(x - y) < min_:
            min_ = abs(x - y)

    print(min_)

main()

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())

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

x = a[0]
y = sum(a[1:])

m = abs(x - y)
for i in range(1, N - 1):
    x += a[i]
    y -= a[i]
    m = min(abs(x - y), m)

print(m)

Submission status: Accepted

Solution code:
n = int(input())
a=[int(i) for i in input().split()]
sum=0
for i in range(0,n):
    sum += a[i]
x = [a[0]]*n
xy = [None]*(n-1)
for i in range(1,n):
    x[i] = x[i-1]+a[i]
for i in range(0,n-1):
    xy[i] = abs(sum - 2*x[i])
print(min(xy))

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split(" ")]
a_sum = sum(a)
ans = 100000000000
x = a[0]
y = a_sum - x
if N == 2:
    print(abs(x-y))
else:
    ans = abs(x-y)
    for i in range(1,N-1):
        x += a[i]
        y -= a[i]
        tmp = abs(x-y)
        if ans >= tmp :
            ans = tmp
    print(ans)

Submission status: Accepted

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

x, y = a[0], sum(a[1:])
ans = abs(x-y)

for i in a[1:-1]:
    x += i
    y -= i
    ans = min(ans, abs(x-y))

print(ans)

Submission status: Accepted

Solution code:
x = input()
y = map(int,raw_input().split())
total = sum(y)
minn = 2147483647
flag1 = y[0]
total -= flag1
for i in range(1,len(y)):
    minn = min( minn, abs(flag1-total))
    flag1 += y[i]
    total -= y[i]
print minn

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
x = 0
total = sum(a)
ans = []
for i in range(n):
    x += a[i]
    ans.append(abs(x - (total-x)))
ans.pop(-1)
print(min(ans))

Submission status: Accepted

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

sum1 = [0] * n
sum2 = [0] * n
sum1[0] = a[0]
sum2[-1] = a[n - 1]

for i in range(1, n):
    sum1[i] = sum1[i - 1] + a[i]
    sum2[-i - 1] = sum2[-i] + a[-i - 1]

d = 10 ** 10
index = 0

for i in range(n - 1):
    if abs(sum1[i] - sum2[i + 1]) < d:
        d = abs(sum1[i] - sum2[i + 1])

print(d)

Submission status: Accepted

Solution code:
import math

n=int(input())
s=0
l=[]

x=input()
x=x.split(' ')


for i in range(n):
  s=s+int(x[i])
  l.append(s)


m = abs(l[0]-(s-l[0]))

for i in range(1,n-1):
  if m > abs(l[i]-(s-l[i])):
    m=abs(l[i]-(s-l[i]))
    
print(m)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import re
import math
import collections
import itertools
import functools

DEBUG = True
DEBUG = False

def dbg(*args):
    if DEBUG:
        print("DBG: ", file=sys.stderr, end="")
        print(*args, file=sys.stderr)



def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    x = A[0]
    y = sum(A[1:])
    diff_min = abs(x-y)

    for i in range(1, N-1):
        x += A[i]
        y -= A[i]
        diff_min = min(diff_min, abs(x-y))

    print(diff_min)

if __name__ == "__main__": main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    S = sum(A)

    MIN = float('inf')
    P = 0
    Q = S
    for a in range(len(A)-1):
        P += A[a]
        Q -= A[a]
        dif = abs(P-Q)
        MIN = min(MIN, dif)

    print(MIN)


if __name__ == "__main__":
    # global stime
    # stime = time.clock()
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
def get_ln_inputs():
    return input().split()


def get_ln_int_inputs():
    return list(map(int, get_ln_inputs()))


def is_closer(s, orig, insp):
    return abs(s - orig * 2) > abs(s - insp * 2)

def main():
    N = get_ln_int_inputs()[0]
    a = get_ln_int_inputs()

    s = sum(a)

    minimum_v = a[0]
    p_s = minimum_v
    for i in range(1, N - 2):
        p_s += a[i]
        if is_closer(s, minimum_v, p_s):
            minimum_v = p_s

    print(abs(s - minimum_v * 2))


main()

Submission status: Accepted

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

dp1 = [0]*(n-1)
dp2 = [0]*(n-1)
dp1[0]= a[0]
dp2[0]= sum(a[1:])
m = abs(dp1[0]-dp2[0])


for i in range(1,n-1):
    dp1[i] = dp1[i-1]+a[i]
    dp2[i] = dp2[i-1]-a[i]
    tm = abs(dp1[i]-dp2[i])
    if tm < m:
        m = tm

print(m)

Submission status: Accepted

Solution code:
import sys

def main():
    line = sys.stdin.readline()
    n = int(line)
    line = sys.stdin.readline()
    lst = line.split()
    a_lst = [int(a) for a in lst]
    length = len(a_lst)

    sunuke_sum = a_lst[0]
    arai_sum = sum(a_lst[1:])
    min_score = abs(sunuke_sum - arai_sum)

    for k in range(1,length-1):
        sunuke_sum += a_lst[k]
        arai_sum -= a_lst[k]
        score = abs(sunuke_sum - arai_sum)
        if score < min_score:
            min_score = score

    print(min_score)
    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

Solution code:
import sys
n = int (sys.stdin.readline())
a = map(int, sys.stdin.readline().split())

cur1 = a[0]
cur2 = sum(a) - cur1
res = 10 ** 15
for i in a[1:]:
  res = min (res, abs (cur1 - cur2))
  cur1 += i
  cur2 -= i
print res

Submission status: Accepted

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

tmp=0
sum_list = [0]
for i in range(N):
    tmp +=A[i]
    sum_list.append(tmp)

#print(sum_list)

diff =9999999999999
for i in range(1,N):
    diff = min(diff , abs( sum_list[-1]-2*sum_list[i]  ))

print(diff)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [0]+list(map(int,input().split()))
for i in range(1,N+1):
  A[i] = A[i]+A[i-1]
ans = float('inf')
for i in range(1,N):
  ans = min(ans, ans(A[N]-2*A[i]))
print(ans)

Submission status: Runtime Error

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

total = sum(a)
s = 0
i = 0
ans = total + 1
while True:
    s += a[i]
    total -= a[i]
    ans = min(ans, abs(s - total))
    i += 1
print(ans)

Submission status: Runtime Error

Solution code:
n = raw_input()
ais = map(int, raw_input())
c = 0
s = sum(ais)
m = +float('inf')
for a in ais:
	c += a
	m = min(m, abs(c - s + c))
print m

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
a_sum = a.sum()
ans = 10**15
for i in range(1,n):
    x = a[:i].sum()
    aa = np.abs(2 * x - a_sum)
    ans = min(ans,aa)
print(ans)

Submission status: Runtime Error

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

b = list(accumulate(a))
ma = b[-1]
mi = 10**10
for sunuke in b:
    arai = ma - sunuke
    if abs(sunuke - arai) < mi:
        mi = abs(sunuke - arai)
print(mi)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt = n//2
print(a[:cnt],a[cnt:])
ans=abs(sum(a[:cnt]) - sum(a[cnt:]))

for i in range(1,cnt):
    
    print(a[:cnt+i],a[cnt+i:])
    if ans > abs(sum(a[:cnt+i]) - sum(a[cnt+i:])):
        ans = abs(sum(a[:cnt+i]) - sum(a[cnt+i:]))
for i in range(1,cnt):
    print(a[:cnt-i],a[cnt-i:])
    if ans > abs(sum(a[:cnt-i]) - sum(a[cnt-i:])):
        ans = abs(sum(a[:cnt-i]) - sum(a[cnt-i:]))

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
l = [0]*n
l[0] = a[0]

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

ans = 10**9+1
for i in range(len(l)-1):
    x = l[i]
    y = sum - l[i]
    tmp = abs(x-y)
    ans = min(ans,tmp)

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
from numpy import cumsum
left_cumsum=cumsum(a)
a.reverse()
right_cumsum=cumsum(a)
ans=10000000000
for i in range(n-1):
    ans=min(ans,abs(left_cumsum[i]-right_cumsum[-i-2]))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
sum_a = sum(a)
b = []
c = 0
for i in range(N-1):
    c += a[i]
    b.append(abs(2*b-sum_a))
print(min(b))

Submission status: Runtime Error

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

ans = 10000000000
aa = sum(a)

for i in range(N-1):
    sum_a = sum(a[:i+1])
    sum_b = a-sum_a
    sa = abs(sum_a-sum_b)
    ans = min(ans, sa)

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
z = sum(a)
ln = []
x = 0
for i in range(1, n):
    x += a[i]
    ln.append(abs(z - 2 * a)
print(min(ln))

Submission status: Runtime Error

Solution code:
mport itertools
N = int(input())
a = list(map(int,input().split()))
ruiseki = list(itertools.accumulate(a))
ans = 10**100
for i in range(N-1):
    ans = min(ans,abs(sum(a[:i+1])-sum(a[i+1:])))
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
a=input().split()
A=[int(b) for b in a]
first=A[0]
last=A[-1]
if N==2:
    print(abs(first-last))
else:
    average = sum(A) // 2
    number = 0
    del A[0]
    del A[-1]
    for a in range(N - 1):
        number += A[a]
        if number >= average:
            break
    a1 = abs(sum(A[:a]) + first - last - sum(A[a:]))
    a2 = abs(sum(A[:a + 1]) + first - last - sum(A[a + 1:]))
    print(min(a1, a2))

Submission status: Runtime Error

Solution code:
# ABC067 C Splitting Pile

import itertools
import bisect

n = int(input())
a_list = [int(x) for x in input().split()]
c_list = list(itertools.accumulate(a_list))

_sum = c_list[-1]
_idx = bisect.bisect_left(c_list, _sum//2)
print(abs(_sum - 2*c_list[_idx]))

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))

whole = a.sum()
div = np.abs(a.cumsum() - whole / 2).argmin()

if div == 0 or div == (n - 1):
    print(min(np.abs(a[0] - a[1:].sum()), np.abs(a[:-1] - a[-1].sum())))
else:
    print(np.abs(a[:div + 1].sum() - a[div + 1:].sum()))

Submission status: Runtime Error

Solution code:
import sys

sys.stdin = open('test.txt')

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

all_sum = sum(li)
x = 0
y = all_sum
ans = 10**18

for i in range(n):
    x += li[i]
    y -= li[i]

    if ans >= abs(x-y):
        ans = abs(x-y)

    if i == n - 2:
        break

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
a = list(map(int,input().split()))
value = []
for i in range(2,N):
    x = sum(a[0:i])
    y = sum(a[i:N])
    value.append(abs(x-y))

print(min(value))

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
a = list(map(int,input().split()))
value = deque()
for i in range(2,N):
    x = sum(a[0:i])
    y = sum(a[i:N])
    value.append(abs(x-y))
print(min(value))

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
A_sum = sum(A)
ans = 10**19
for i in range(n):
  x = A[:i+1]
  x_sum = sum(x)
  z = abs(A_sum - 2*x_sum)
  ans = min(ans,z)
print(b)

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
A_sum = sum(A)
ans = 10**19
for i in range(n):
  x = sum(A[:i+1])
  z = abs(A_sum -2*x)
  ans = min(ans,z)
print(b)

Submission status: Runtime Error

Solution code:
n = int(input())
A = [map(int,input().split())]
ans = 10**19
for i in range(n):
  a = A[:i]
  a_sum = sum(a)
  b = abs(sum(A)-a_sum)
  ans = min(ans,b)
print(b)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
x = y = 0
y_cnt = 1
candidate = []

for i in range(n-1):
    x += a[i]
    while y >= x or y_cnt+i+1 == n:
        y += a[-y_cnt]
        y_cnt += 1
    candidate.append(abs(x - y))

print(min(candidate))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!


N = int(input())
a =list(map(int,input().split()))
minit = []
for i in range(1,N):
    a = abs(2*sum(a[0:i])-sum(a))
    minit.append(a)
    
print(min(minit))

Submission status: Runtime Error

Solution code:
n = int(input())
v = list(map(int, input().split()))
t = [0]
s = 2000000000
for i in range(0, n):
  t.append(t[i]+v[i])
for i in range(0, n-1):
  s = min(s, abs(t[n]-t[i+1])
print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
v = list(map(int, input().split()))
t = [0]
s = 2000000000
for i in range(0, n):
  t.append(t[i]+v[i])
for i in range(0, n-1):
  s = min(s, abs(s-t[i+1])
print(s)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
from collections import defaultdict
edges = []
to = defaultdict(list) # 1から到達できるか
ot = defaultdict(list) # Nから到達できるか
N, M, P = map(int, input().split())
visited_toN = [False]*(N+1)
visited_fm1 = [False]*(N+1)    
for i in range(M):
    a,b,c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a,b,-c+P))
    to[a].append(b)
    ot[b].append(a)

def dfs(v):
    global visited_fm1
    if visited_fm1[v]:return
    visited_fm1[v] = True
    for nxt in to[v]:
        dfs(nxt)
    
def dfs_r(v):
    global visited_toN
    if visited_toN[v]:return
    visited_toN[v] = True
    for nxt in ot[v]:
        dfs_r(nxt)
        
dfs(0)
dfs_r(N-1)
is_ok = []

for s, t in zip(visited_fm1,visited_toN):
    is_ok.append(s and t)


# BellmanFord    
INF = 10 ** 18
def bellmanFord(n, edges, s, g):
    """
    input
        n : 頂点の数
        edges : (始点, 終点, コスト)のリスト
        s : 探索の始点
        g : 探索の終点
    output
        sから各点への最短距離
    """    
    d = [INF] * n
    d[s] = 0
    cnt = 0
    while True:
        update = False
        for a, b, c in edges:
            if not is_ok[a]:continue
            if not is_ok[b]:continue
            if d[a] != INF and d[b] > d[a] + c:
                d[b] = d[a] + c
                update = True
        if not update:
            break
        cnt += 1
        # 負閉路の存在をチェック
        if cnt > n:
            print(-1)
            exit()
        # print(d,cnt)
    return d[g]



ans = bellmanFord(N,edges,0,N-1)
print(max(0,-ans))

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=10**9
sumA=sum(A)

for i in range(n-1):
  x+=A[i]
  ans=min(ans,abs(sumA-x*2))

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
d = []

for i in range(n):
  sum = sum+a[i]
  d[i] = sum
for i in range(n):
  d[i] = abs(sum[i]-d[i])
d.pop(0)
d.pop(n-1)
print(min(d))

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
C=[0]*(N);C[0]=abs(A[0]-sum(A[1:]));x=10**9;P=0
for i in range(1,N):
  if N-i==1:
    break
  else:
    C[i]=abs(C[i-1]+A[i]-sum(A[i+1:]))
    if x>C[i]:
      x=C[i]
      p=i
print(abs(C[p-1]+A[p]-sum(A[p+1:])))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

import numpy as np
a = list(np.cumsum(a))

import bisect

ma = bisect.bisect_right(a,a[-1]/2)+1
mi = bisect.bisect_left(a,a[-1]/2)-1

ma = ma if ma < n-1 else n-2
mi = mi if mi > 0 else 0
mid = mi+1 if n>2 else mi

print(min(abs(a[-1]-a[ma]*2), abs(a[-1]-a[mi]*2), abs(a[-1]-a[mid]*2)))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

import numpy as np
a = list(np.cumsum(a))

import bisect
print(a)

ma = bisect.bisect_right(a,a[-1]/2)+1
mi = bisect.bisect_left(a,a[-1]/2)-1

ma = ma if ma < n-1 else n-2
mi = mi if mi > 0 else 0
mid = mi+1 if n>2 else mi

print(min(abs(a[-1]-a[ma]*2), abs(a[-1]-a[mi]*2), abs(a[-1]-a[mid]*2)))

Submission status: Runtime Error

Solution code:
N = int(input())
from bisect import bisect_left
from itertools import accumulate
a = map(int, input().split())
A = list(accumulate(a))
m = A[-1]//2
i = bisect_left(A, m)
ans1 = abs(A[-1] - A[i] - A[i])
ans2 = abs(A[-1] - A[i-1] - A[i-1]) if i >= 1 else ans1
print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:
N=int(input())
a=[0]*N
for i in range(N):
    a[i]=int(input().split())

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

x=[0]*N
y=[0]*N
z=[0]*N
for j in range(N):
    x[j]=b[j]
    y[j]=b[-1]-b[j]
    z[j]=abs(x[j]-y[j])

print(min(z[:-1]))

Submission status: Runtime Error

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

if len(a) == 2:
    x = a[0]
    y = a[-1]
    print(abs(x-y))
    exit()


total = sum(a)
mid = total//2

for i in range(1, N):
    x += a[i]
    if x <= mid:
        continue
    else:
        print(min(abs(x-a[i]-(total - (x-a[i]))), abs(x-(total - x))))
        exit()

Submission status: Runtime Error

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

if len(a) == 2:
    x = a[0]
    y = a[-1]
    print(abs(x-y))
    exit()


total = sum(a)
mid = total//2

print(mid)
for i in range(1, N):
    x += a[i]
    if x <= mid:
        continue
    else:
        print(x-(total - x))
        print(min(abs(x-a[i]-(total - (x-a[i]))), abs(x-(total - x))))
        exit()

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in input().split()]


s = sum(a)
m = s - a[:1]
#print(m)
for i in range(2, N):
    b = sum(a[:i])
    c = s - b
    #print(b)
    #print(c)
    if m > (abs(b-c)):
        m = abs(b-c)
    else:
        break
print(m)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [int(i) for i in input().split()]


s = sum(a)
m = s - a[:1]
print(m)
for i in range(2, N):
    b = sum(a[:i])
    c = s - b
    #print(b)
    #print(c)
    if m > (abs(b-c)):
        m = abs(b-c)
    else:
        break
print(m)

Submission status: Runtime Error

Solution code:
from itertools import accumulate

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

cumsum = list(accumulate(lst))

mi = 2 * 10 ** 9
su = cumsum[-1]
for i in cumsum:
  mi = min(mi, abs(i - (su-i))
print(mi)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<ll> a(n);
  for (auto &x: a) cin >> x;

  ll total = 0;
  for (auto &x: a) total += x;

  ll alpha = a[0];
  ll beta = total - a[0];
  ll minimum = abs(alpha - beta);
  for (int i = 1; i < n-1; i++) {
    alpha += a[i];
    beta -= a[i];
    if (abs(alpha - beta) < minimum) minimum = abs(alpha - beta);
  }

  cout << minimum << endl;

  return 0;
}

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
b,i=l10**9,1
while(i<n):
  p=sum(l[:i])
  q=sum(l[i:])
  b=min(b,abs(p-q))
  i+=1
print(b)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [0]
if (n == 2):
    print(abs(a[0] - a[1]))
else:
    for i in range(n):
        s.append(a[i] + s[i])

    b = 1e+9
	j = 1
    while j != n:
        if (abs(s[j] - (s[n] - s[j])) < b):
            b = abs(s[j] - (s[n] - s[j]))
        j++         
    print(b)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [0]
if (n == 2):
    print(abs(a[0] - a[1]))
else:
    for i in range(n):
        s.append(a[i] + s[i])

    b = 1e+9
	j = 1
    while(j != n):
        if (abs(s[j] - (s[n] - s[j])) < b):
            b = abs(s[j] - (s[n] - s[j]))
        j++         
    print(b)