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:
N = int(input())
lia=[int(it) for it in input().split()]
lib=[0]*(N-1)
allsum = sum(lia)
s=0
for i in range(N-1):
  s+=lia[i]
  lib[i]= abs((allsum-s)-s)
print(min(lib))

Submission status: Accepted

Solution code:
from itertools import accumulate
inf = float('inf')

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

ans = inf

for i in range(N-1):
    res = abs(A[i] - (A[-1] - A[i]))
    if res < ans:
        ans = res

print(ans)

Submission status: Accepted

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

for i in range(1, len(As)):
    As[i] += As[i-1]

res = 2*10**14
for i in range(len(As)-1):
    res = min(res, abs(2 * As[i] - As[-1]))
print(res)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
for i in range(n-1):
  A[i+1]=A[i+1]+A[i]
ans=10**12
for i in range(n-1):
  x=A[i]
  y=A[-1]-A[i]
  ans=min(ans,abs(x-y))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split(' ')))
X,Y=A[0],sum(A)-A[0]
ans=abs(X-Y)
for i in range(N-2):
  X+=A[i+1]
  Y-=A[i+1]
  if abs(X-Y)<ans:
    ans=abs(X-Y)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

S = [0]
for a in A:
  S.append(S[-1]+a)

Min = 10**20
for i in range(1,N):
  Min = min(Min,abs((S[i]-S[0])-(S[N]-S[i])))
print(Min)

Submission status: Accepted

Solution code:
N = int(input())
S = 0
s_list = []

input_list = list(map(int, input().split()))
allS = sum(input_list)

for n in input_list[:-1]:
  S += n
  s_list.append(abs(allS - 2 * S))
  
print(min(s_list))

Submission status: Accepted

Solution code:
from numpy import*;n,*a=map(int,open(0).read().split());print(min(abs(cumsum(a[::-1])[::-1]-cumsum([0]+a[:-1]))[1:]))

Submission status: Accepted

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

res=[0 for i in range(n-1)]

for i in range(0,n-1):
    res[i] = abs(2*sums[i]-sums[-1])
        
print(min(res))

Submission status: Accepted

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

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


ans = 10**15
for i in range(1,n):
    k = abs(sums[i]-(sum-sums[i]))
    ans = min(k,ans)

print(ans)

Submission status: Accepted

Solution code:

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

S=[0]
s = 0
for i in range(N):
    s+=a[i]
    S.append(s)
ans=10**28
for i in range(1,N):
    ans=min(abs(S[N]-2*S[i]),ans)
print(ans)

Submission status: Accepted

Solution code:

n=int(raw_input())
s=raw_input().split(' ')

ar=[]
k=0
for i in range(len(s)):
	ar.append(int(s[i]))
	k+=int(s[i])

l=0
mn=10000000000
for i in range(len(ar)-1):
	l=l+(ar[i])
	v=(k-l)
	g=0
	if v>l:
		g=v-l
	else:
		g=l-v;
	if g<mn:
		mn=g

print(mn)

Submission status: Accepted

Solution code:
a = int(input())
x = list(map(int, input().split()))
s = sum(x)
inf = float('INF')
m = inf
S=0
for i in range(a-1):
    s-=x[i]
    S+=x[i]
    m = min(m,abs(s-S))

print(m)

Submission status: Accepted

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

x = 0
y = sum(a)
ans = []

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

print(min(ans))

Submission status: Accepted

Solution code:
import numpy
N = input()
a = map(int, raw_input().split())
cumsum_a = numpy.cumsum(a)

cand = float('inf')
for i in range(N - 1):
  cand = min(cand, abs(cumsum_a[-1] - 2 * cumsum_a[i]))
print cand

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/2/
Solved on 2019/2/
@author: shinjisu
"""


# ARC 078 C  Splitting Pile
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def probC():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    sumFormer = A[0]
    sumLatter = sum(A[1:])
    diff = abs(sumFormer-sumLatter)
    dmp(diff,'init')
    for i in range(1, N-1):
        sumFormer += A[i]
        sumLatter -= A[i]
        diff = min(diff, abs(sumFormer-sumLatter))
    return diff


def probC_v1():  # 5/16 AC 残りはTLE
    N = getInt()
    A = getIntList()
    dmp((N, A))
    diff = 10**(9+6)
    for i in range(1, N):
        diff = min(diff, abs(sum(A[:i])-sum(A[i:])))
    return diff


debug = False  # True False
ans = probC()
print(ans)
#for row in ans:
#    print(row)

Submission status: Accepted

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

max=sum(a)
s=a[0]
ans=abs(2*s-max)
for i in range(1,N-1):
    s+=a[i]
    ans=min(ans,abs(2*s-max)) 
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

acc = list(accumulate(card))
s = acc[-1]
ans = float('inf')
for i in range(N-1):
    t = acc[i]
    a = s-t
    ans = min(ans, abs(t-a))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
rui = [0] * (n+1)
for i in range(n):
  rui[i+1] = rui[i] + a[i]
ans = float("inf")
for i in range(1,n):
  ans = min(ans,abs(rui[i]-(rui[n]-rui[i])))
print(ans)

Submission status: Accepted

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

a = 0
b = sum(cards)
min_diff = float('inf')
for i in range(n - 1):
    a += cards[i]
    b -= cards[i]
    min_diff = min(min_diff, abs(a - b))
print(min_diff)

Submission status: Accepted

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

total = sum(a)
now = a[0]
ans = abs(total - 2*now)
for i in range(1, len(a)-1):
    now += a[i]
    ans = min(abs(total - 2*now), ans)
print(ans)

Submission status: Accepted

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

ans = float("inf")
X = a[0]
Y = sum(a) - a[0]

for i in range(1, N):
	if abs(X - Y) < ans:
		ans = abs(X - Y)

	X += a[i]
	Y -= a[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
count = a[0]
ans = abs(total - 2*count)
for i in range(1,n-1):
    count += a[i]
    if ans > abs(total - 2*count):
        ans = abs(total - 2*count)
print(ans)

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
import numpy as np
ans = float('inf')
N = int(input())
a = np.cumsum(np.array(inpl()))
suma = a[-1]
a = np.delete(a, -1)
for i in a:
    ans = min(ans, abs(suma-2*i))
print(ans)

Submission status: Accepted

Solution code:
n=int(input().strip())
a=list(map(int, input().split()))
sumx=a[0]
sumy=sum(a[1:])
ans=abs(sumx-sumy)

for i in range(1,n-1):
    sumx+=a[i]
    sumy-=a[i]
    ans=min(ans,abs(sumx-sumy))
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
import math

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

L = [0] * N

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

min_diff = 10**20
for i in range(N-1):
    s = L[i]
    a = L[N-1] - L[i]
    diff = abs(a-s)
    min_diff = min(min_diff, diff)

print(min_diff)

sys.exit(0)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
s=sum(a)
r=0
ans=10**10
for i in range(n-1,0,-1):
    s-=a[i]
    r+=a[i]
    ans=min(ans,abs(s-r))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = [0]
for a in map(int, input().split()):
    s.append(s[-1] + a)
ans = 10 ** 15
for i in range(1, N):
    ans = min(ans, abs(s[i] - (s[-1] - s[i])))
print(ans)

Submission status: Accepted

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

l = A[0]
r = sum(A[1:])

ret = abs(r - l)

for a in A[1:len(A)-1]:
    l += a
    r -= a
    ret = min(ret, abs(r - l))

print(ret)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *
from heapq import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

N = read()
A = reads()

psum = [0] + list(accumulate(A))
ans = min(abs(psum[k] - (psum[N] - psum[k])) for k in range(1, N))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
dif = float('inf')
snk = 0
for i in range(n-1):
    snk += a[i]
    dif = min(abs(snk*2-s),dif)
print(dif)

Submission status: Accepted

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

s = sum(a)
ass = 10**18
g = 0
h = s

for i in range(n-1):
    g += a[i]
    h = h-a[i]
    ass = min(ass,abs(g-h))
print(ass)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
aa = [0]
s = 0
for x in input().split():
    a = int(x)
    aa.append(aa[-1]+a)
    s += a

ret = abs(2*aa[1]-s)
for i in range(1,n):
    if abs(aa[i]-(s-aa[i]))<ret:
        ret = abs(aa[i]-(s-aa[i]))

print(ret)

Submission status: Accepted

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

b = sum(a)

c = 0

ans = 1000000000000000

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

Submission status: Accepted

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

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

ans = 10**20
for i in range(n-1):
    ans = min(ans, abs(a[-1]-2*a[i]))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
from itertools import accumulate
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)

Submission status: Accepted

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

comulative_sum=[0*i for i in range(N+1)]
for i in range(1,N+1):
    comulative_sum[i] = comulative_sum[i-1]+A[i-1]
minimum=pow(10,18)
for i in range(1,N):
    minimum=min(abs(comulative_sum[i]-(comulative_sum[N]-comulative_sum[i])), minimum)
print(minimum)

Submission status: Accepted

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

sub = sum(a) - 2 * a[0]
ans = abs(sub)

for i in range(1, n - 1):
	sub -= 2 * a[i]
	if abs(sub) < ans:
		ans = abs(sub)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
allsum = sum(a)
ans = float("INF")
snuke = 0
for i in range(N-1):
    snuke += a[i]
    allsum -= a[i]
    ans = min(ans, abs(snuke-allsum))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int,input().split()))
B = [] #累積和するぞ！

s = 0
for a in A:
    s += a
    B.append(s)

sabun_m = abs(B[N-1] - (B[0]*2))

'''
1 2 3 4

1 | 2 3 4 (1234-1)-1
'''

for b in B[1:N-1]:
    sabun = abs(B[N-1] - (b*2))
    if sabun < sabun_m:
        sabun_m = sabun
        
print(sabun_m)

Submission status: Accepted

Solution code:
import itertools

N = int(input())
A = [int(_) for _ in input().split()]

ts = list(itertools.accumulate(A))
total = ts[-1]

r = list(itertools.accumulate((abs(total - t * 2) for t in ts[:-1]), min))[-1]

print(r)

Submission status: Accepted

Solution code:
import itertools

N = int(input())
A = [int(_) for _ in input().split()]

ts = list(itertools.accumulate(A))
total = ts[-1]
total_half = total / 2

r = sorted(ts[:-1], key=lambda x:abs(x - total_half))[0]
print(abs(total - r - r))

Submission status: Accepted

Solution code:
from itertools import accumulate

N=int(input())
A=[int(x) for x in input().split()]
ans=abs(sum(A)-A[0]*2)

S=list(accumulate(A))

for i in range(1,N-1):
  ans=min(ans,abs(S[N-1]-S[i]*2))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

sum1 = 0
sum2 = sum(As)

min_diff = float('inf')

for a in As[0:len(As)-1]:
    sum1 += a
    sum2 -= a
    #print(abs(sum1 - sum2))
    min_diff = min(min_diff, abs(sum1 - sum2))
print(min_diff)

Submission status: Accepted

Solution code:
n = int(input())
j = list(map(int, input().split()))
k = [j[0]]
mi = 9999999999

for i in range(1, len(j)):
    k.append(k[i - 1] + j[i])
for m in range(len(k) - 1):
    mi = min(abs(k[n - 1] - 2 * k[m]), mi)

print(mi)

Submission status: Accepted

Solution code:
n = int(input())

j = list(map(int, input().split()))
k = [j[0]]
for i in range(1, len(j)):
    k.append(k[i-1] + j[i])
mi = 9999999999
for m in range(len(k)-1):
    mi = min(abs(k[n - 1] - 2 * k[m]), mi)
print(mi)

Submission status: Accepted

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

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

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

print(res)

Submission status: Accepted

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

cum_sum = [0]*N
cum_sum[0] = a[0]

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

res = 10**15
for i in range(N-1):
	x = cum_sum[i]
	y = cum_sum[N-1] - cum_sum[i]
	res = min(res, abs(x-y))

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))

N = int(input())
A = inpl()
s = sum(A)

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

for a in A[1:-1]:
    x += a
    y -= a
    if abs(x-y) < res:
        res = abs(x-y)
print(res)

Submission status: Accepted

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

x = 0
sum_ = sum(a)
sa = []
for i in range(n - 1):
    x += a[i]
    y = sum_ - x
    sa.append(abs(x - y))
#print(sa)
print(min(sa))

Submission status: Accepted

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
x=a[0]
y=sum(a)-a[0]
ans=abs(x-y)
for i in range(1,N-1):
        x+=a[i]
        y-=a[i]
        ans=min(ans,abs(x-y))
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = list(map(int, input().split()))
    #N = 1000
    #a = [i for i in range(N)]

    #summation = [abs(sum(a[:(i+1)]) - sum(a[(i+1):])) for i in range(len(a)-1)]

    summation = []
    sumtemp = sum(a)
    tmp1 = 0
    for i in range(len(a)-1):
        tmp1 += a[i]
        tmp2 = sumtemp - tmp1
        tmp = abs(tmp1 - tmp2)
        summation.append(tmp)
        
    print(min(summation))

main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int, input().split()))
b = sum(a)
c = 0
minimum = float('inf')
for tmp in range(0,N-1):
    c += a[tmp]    
    d = abs(c-(b-c))
    minimum = min(minimum, d)
print(minimum)

Submission status: Accepted

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

sum = [a[0]];
for i in range(1, n):
	sum.append(sum[i - 1] + a[i]);
	
mini = abs(sum[0] - (sum[n - 1] - sum[0]));
for i in range(1, n - 1):
	mini = min(mini, abs(sum[i] - (sum[n - 1] - sum[i])));
	
print(mini);

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
m = None
x = 0
y = 0
y = sum(a)

for i in range(len(a)-1):
	x += a[i]
	y -= a[i]
	if(m == None or m > abs(x - y)):
		m = abs(x - y)
print(m)

Submission status: Accepted

Solution code:
#ARC078C

INF=float('inf')
n=int(raw_input())
a=map(int,raw_input().split())
ss=sum(a)
res=INF
x=a[0]
y=ss-a[0]
for i in xrange(1,n):
 res=min(res,abs(y-x))
 x+=a[i]
 y-=a[i]
print res

Submission status: Accepted

Solution code:
raw_input()
cards = map(int, raw_input().split(" "))
total = sum(cards)

min_delta = None
a = 0
for i in xrange(0, len(cards)-1):
  a += cards[i]
  delta = abs((total - a) - a)
  if min_delta is None or delta < min_delta:
    min_delta = delta
print min_delta

Submission status: Accepted

Solution code:
N = int(input())
card = list(map(int, input().split()))
s = sum(card)
p_sum = 0
min_sum = []
for i in card[:-1]:
    p_sum += i
    min_sum.append(abs(s- 2 * p_sum))
print(min(min_sum))

Submission status: Accepted

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

total = sum(x)
cum_sum = 0
ans = float('inf')
for i in range(N - 1):
    cum_sum += x[i]
    ans = min(ans, abs(cum_sum - (total - cum_sum)))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
sum_num = sum(a)
current_sum = 0
min_diff = float('inf')
for i in range(0, N - 1):
    current_sum += a[i]
    min_diff = min(min_diff, abs(current_sum - (sum_num - current_sum)))
print(min_diff)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(a)
res = 10 ** 15
x = 0
for i in range(n-1):
    x  += a[i]
    res = min(res, abs(s - x - x))

print res

Submission status: Accepted

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

ans = 10 ** 15
x = 0
y = sum(A)
for a in A[:-1]:
    x += a
    y -= a
    ans = min(ans, abs(x - y))
print ans

Submission status: Accepted

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

all_sum = sum(a)

results = []
now = 0
for i in a[:-1]:
    now += i
    at = all_sum - now
    results.append(abs(now - at))

print(min(results))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    n = I()
    a = LI()
    s = sum(a)
    r = abs((s - a[0]) - a[0])
    t = a[0]
    for i in range(1,n-1):
        t += a[i]
        tr = abs((s - t) - t)
        if tr < r:
            r = tr

    return r


print(main())

Submission status: Accepted

Solution code:
from sys import stdin
ma = 10**18
n = int(stdin.readline())
a = map(int,stdin.readline().split())
x = 0
s = sum(a)
for i in a[:-1]:
 x+= i
 ma  = min(ma, abs(s-2*x) )
print ma

Submission status: Accepted

Solution code:
from numpy import cumsum
from functools import reduce
n = int(input())
cs = cumsum(list(map(int, input().split())))

print(reduce(min, map(lambda x: abs(cs[-1] - x*2), cs[:-1])))

Submission status: Accepted

Solution code:
n = raw_input()
a = raw_input().split(' ')
a = map(int, a)
s = 0
sunuke = a[0]
arai = a[len(a)-1]
for i in a:
	s = s + i
m = abs(sunuke - (s - sunuke))

for i in range(1,len(a)-2):
	sunuke = sunuke + a[i]
	arai = s - sunuke
	if abs(arai-sunuke) < m:
		m = abs(arai-sunuke)


print m

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().strip().split(' ')))
total=sum(A)
MIN=10**10+abs(total)
temp=0
for i in range(len(A)):
    temp+=A[i]
    if i==len(A)-1:
        break
    if abs(total-2*temp)<MIN:
        MIN=abs(total-2*temp)
print(MIN)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy

from math import *
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = list(read_func().strip().split())
    a = []
    for i in range(N):
        a.append(int(input_raw[i]))
    s = sum(a)
    min_sub = sys.maxint
    x = 0
    for i in range(N - 1):
        x += a[i]
        sub = abs(s - 2*x)
        if sub < min_sub:
            min_sub = sub
    print min_sub


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,*A=map(int,open(0).read().split())
while n>1:n-=1;A[n-1]+=A[n]
print(min(abs(A[0]-e*2)for e in A[1:]))

Submission status: Accepted

Solution code:
n,*A=map(int,open(0).read().split())
for i in range(n-1):A[i+1]+=A[i]
print(min(abs(A[n-1]-A[i]*2)for i in range(n-1)))

Submission status: Accepted

Solution code:
import numpy as np

n = input()
a = np.array([int(num) for num in input().split(' ')])

min = 100000000000
sunuke = 0
araiguma = a.sum()
for i in range(0, len(a) - 1):
    sunuke += a[i]
    araiguma -= a[i]
    dif = abs(sunuke - araiguma)
    if dif < min: min = dif
print(min)

Submission status: Accepted

Solution code:
import numpy as np

input()
a = np.fromiter(map(int, input().split()), int)
c = a.cumsum()
c2 = c.copy().astype(float)
c2[0] = c2[-1] = float('inf')
s = a.sum()
i = np.argmin(np.abs(c2 - s / 2))
print(abs(2 * c[i] - s))

Submission status: Accepted

Solution code:
n=int(raw_input())
N=map(int,raw_input().split())
for i in range(1,n):
	N[i]+=N[i-1]
res=10**20
for i in range(n-1):
	res=min(res,abs(N[i]-N[n-1]+N[i]))
print res

Submission status: Accepted

Solution code:
def c():
    import numpy as np

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

    A_cum = np.cumsum(A)
    A_rev = A_cum[-1] - A_cum
    res = np.abs(A_rev - A_cum)[:-1]
    print(int(np.min(res)))


c()

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
total, ano = sum(nums), 0
last_dif = 100000000000000
for i in range(len(nums)-1):
    total -= nums[i]
    ano += nums[i]    
    if abs(ano-total) < last_dif:
        dif = abs(ano-total)
        last_dif = dif
print(dif)

Submission status: Accepted

Solution code:
def spaceinput():
    return list(map(int,input().split(" ")))
N=int(input())
aa=spaceinput()


suml=aa[0]
sumr=sum(aa[1:])
minv=10**10
for i in range(1,len(aa)+1):
    #print(suml,sumr)
    v=abs(suml-sumr)
    if minv>v:
        minv=v

    if i==len(aa)-1:
        break
    suml+=aa[i]
    sumr-=aa[i]


print(minv)

Submission status: Accepted

Solution code:
N=int(raw_input())
a=map(int,raw_input().split())
top=N-1
bottom=1
res=10**10
d=-sum(a)
for i in range(1,N):
    d+=2*a[i-1]
    if abs(d)<=res:
        res=abs(d)
print res

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
csum = [0 for i in range(len(A)+1)]
for i in range(len(A)):
    csum[i+1] = csum[i] + A[i]


res = 1000000000000
for i in range(len(A)-1):
    res = min(res, abs(csum[i+1] - (csum[N]-csum[i+1])))

print(res)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    sunuke_sum = 0
    arai_sum = sum(a)
    ret = 2000000000
    for i in range(0, N - 1):
        sunuke_sum += a[i]
        arai_sum -= a[i]
        tmp_diff = abs(sunuke_sum - arai_sum)
        if tmp_diff < ret:
            ret = tmp_diff

    print(ret)

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('c1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def solve():
    n = read_int()
    a = read_int_list()
    res = 2 * 10 ** 10
    x = 0
    y = sum(a)
    for i in range(n - 1):
        x += a[i]
        y -= a[i]
        d = abs(x-y)
        if res > d:
            res = d
    return res


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
As = [int(x) for x in input().split()]
S = sum(As)

ans = None
subsum = 0
for i in range(1,N):
    subsum += As[i-1]
    temp = abs(2 * subsum - S)
    if ans is None or temp < ans:
        ans = temp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import sys
n = int(input())
nums = list(map(int, input().split()))
s = sum(nums)
pre = nums[0]
ans = sys.maxsize
for num in nums[1:]:
    ans = min(ans, abs(2 * pre - s))
    pre += num

print(ans)

Submission status: Accepted

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

all = sum(a)

mini = 10000000000

x = 0
y = all

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

    if(ret < mini): mini=ret

print mini

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0]
for x in map(int, input().split()):
    A.append(x)
    A[-1] += A[-2]
ans = int(1e18)
for i in range(N - 1):
    ans = min(ans, abs(A[i+1] - (A[N] - A[i+1])))
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N = II()
    a = ILI()
    return N, a


def solve(N, a):
    sum_a = sum(a)
    l_sum_a = [a[0]]
    for i in range(1, len(a) - 1):
        l_sum_a.append(l_sum_a[i - 1] + a[i])
    l_changed = [abs(ele * 2 - sum_a) for ele in l_sum_a]
    ans = min(l_changed)
    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
ans = float('inf')

total = sum(a)
x = 0
for i in range(n-1):
  x += a[i]
  ans = min(ans, abs(x - (total-x)))

print ans

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))

N = ri()
a = rli()

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

ans = 1145148101919
for i in range(N-1):
    ans = min(ans, abs(2*s[i]-s[-1]))

print(ans)

Submission status: Accepted

Solution code:
import sys
from math import ceil
from bisect import bisect

cin = sys.stdin
# cin = open("in.txt", "r")

def line2N():
    return map(int, cin.readline().split())


N, = line2N()
a = list(line2N())
s = sum(a)
cnt = 0
ans = 0x7fffffff
for i in range(0,N-1):
    cnt += a[i]
    ans = min(ans, abs(s - 2*cnt))

print(ans)

Submission status: Accepted

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

su=0
for i in range(n):
    su+=a[i]
best = abs(su-2*a[0])
curr=0
for i in range(n-1):
    curr+=a[i]
    diff=abs(su-2*curr)
    best=min(best,diff)

print (best)

Submission status: Accepted

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

s = sum(xs)

cumsum = 0

ans = 10000000000000000000000

for x in xs[:-1]:
    cumsum += x
    ans = min(ans, abs(s-cumsum*2))

print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
s = [0] * n
s[0] = a[0]
for i in xrange(1, n):
    s[i] += s[i - 1] + a[i]
ans = 10**12
for i in xrange(n - 1):
    ans = min(ans, abs(s[i] - (s[n - 1] - s[i])))
print ans

Submission status: Accepted

Solution code:
def main():
	raw_input()
	a = map(int, raw_input().split())
	n = len(a)
	s = sum(a)
	ans = float("inf")
	cur_sum = 0
	for i in range(n - 1):
		cur_sum += a[i]
		ans = min(ans, abs(cur_sum * 2 - s))
	print ans

if __name__ == "__main__":
	main()

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

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

s1 = 0
s2 = sum(As)

ans = 1e15
for i in range(N-1):
    s1 += As[i]
    s2 -= As[i]
    ans = min(ans, abs(s1 - s2))
print(ans)

Submission status: Accepted

Solution code:
N = input()
a = map(int, raw_input().split())
x = 0
y = sum(a)
ans = 1e100
for i in range(N-1):
    x += a[i]
    y -= a[i]
    ans = min(ans, abs(x-y))
print ans

Submission status: Accepted

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

def solve(N, As):
    total = sum(As)
    record = abs(total - As[0] - As[0])
    cum = 0
    for a in As[:-1]:
        cum += a
        record = min(record, abs(total - cum - cum))
    return record

print(solve(N, As))