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:
from bisect import bisect_left


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


cumsum = []
ret = 0


for i in (nums):
    ret += i
    cumsum.append(ret)


s = cumsum[-1]


min_val = float('inf')


for i in range(len(cumsum)-1):
    min_val = min(min_val, abs(cumsum[i] * 2 - s))




print(min_val)

Submission status: Accepted

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

Submission status: Accepted

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

ans = float('inf')
left = 0
right = sum(A)

for a in A[: N - 1]:
    left += a
    right -= a
    ans = min(ans, abs(left - right))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate
INF = float("inf")


def solve(N: int, a: "List[int]"):
    acc = list(accumulate(a))
    m = INF
    for i in range(N-1):
        buf = abs(acc[i]-(acc[N-1]-acc[i]))
        m = min(m, buf)
    print(m)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    x = [0] * (N+1)
    for i in range(1,N+1):
        x[i] = a[i-1]+x[i-1]

    sum_of = sum(a)
    m = 10000000000000000
    for i in range(1,N):
        m = min(m, abs(sum_of - 2*x[i]))

    print(m)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

suma = sum(a)
sums = [0]
for i in range(N):
    sums.append(sums[-1] + a[i])

ans = 10**14
for i in range(1, N):
    ans = min(ans, abs(suma - sums[i] * 2))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

ans = float("inf")
suml = sum(a)
snuke = 0

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

print(ans)

Submission status: Accepted

Solution code:
# 2019/08/09

from itertools import accumulate

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

acc=list(accumulate(a))

ans=float('inf')
for i in range(n-1):
    ans=min(ans,abs(acc[i]-(acc[-1]-acc[i])))
    
print(ans)

Submission status: Accepted

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

res = float("inf")
suma = sum(A)
pa = [0]

for i, a in enumerate(A):
    if i == len(A) - 1:
        break
    pa.append(pa[i] + A[i])
    res = min(res, abs(suma - 2 * pa[i + 1]))

print(res)

Submission status: Accepted

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

sumleft = [0]*(n+1)

for i in range(n):
    sumleft[i+1] = sumleft[i] + lst[i]

total = sumleft[-1]
ans = None

for i in range(1,n):
    x = sumleft[i]
    y = total - x
    if ans == None:
        ans = abs(x-y)
    else:
        ans = min(ans,abs(x-y))
print(ans)

Submission status: Accepted

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

X, Y = [0]*N, [0]*N
X[0] = a[0]
Y[-1] = a[-1]

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

ans = 99999999999999

for i in range(N-1):
    ans = min(abs(X[i]-Y[i+1]),ans)
print(ans)

Submission status: Accepted

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


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

ret = 10**10
for i in range(1, N):
    x = cum[i]
    y = cum[-1] - x
    if abs(x-y) < ret:
        ret = abs(x-y)
    
print(ret)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools


def solve(N: int, a: "List[int]"):
    S = sum(a)
    L = [a[0]]
    for i in range(1, N):
        L.append(L[-1] + a[i])
    mx = 1e12
    for i in range(0, N-1):
        d = abs((S - L[i]) - L[i])
        mx = min(d, mx)
    print(mx)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
an = list(map(int,input().split()))
sum_an = sum(an)
from itertools import accumulate
an_acc = list(accumulate(an))[0:-1]
print(min(list(map(lambda x : abs(sum_an-x*2),an_acc))))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
# 初期値
snuke = a[0]
araiguma = sum(a) - a[0]
ans = abs(snuke - araiguma)
# 更新
for i in range(1, N - 1):
    snuke += a[i]
    araiguma -= a[i]
    ans = min(ans, abs(snuke - araiguma))
# 出力
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
total = sum(A)
S = [0]*N
s = 0
for i in range(0,N-1):
    s += A[i]
    S[i] = s
print(min(abs(S[i]-(total-S[i])) for i in range(0, N-1)))

Submission status: Accepted

Solution code:
N = int(input())
cards = list(map(int, input().split()))
all_c = sum(cards)
half_c = all_c//2
sum_c = 0
ans = 2*10**14
for idx, num in enumerate(cards):
  if idx == N-1:
    break
  sum_c += num
  ans = min(ans, abs(sum_c*2-all_c))
print(ans)

Submission status: Accepted

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

def main(N,A):
    zenbu = sum(A)
    part_sum = [A[0]]
    ans = float("INF")
    for i in range(1,N): 
        ans = min(ans, abs(zenbu-2*part_sum[-1]))
        part_sum.append(part_sum[-1]+A[i])

    print(ans)
    
if __name__ == '__main__':
    N = int(input())
    A = [int(a) for a in input().split()]
    main(N,A)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math
import itertools
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
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()
    
    ans = float("inf")
    
    if N == 2:
        print(abs(A[0] - A[1]))
        sys.exit()
    
    a_sp = []
    for i, val in enumerate(A):
        if i == 0:
            a_sp.append(val)
            continue
        tmp = val + a_sp[i-1]
        a_sp.append(tmp)
        
    for i, val in enumerate(a_sp):
        if i == (len(a_sp)-1):
            break
            
        tmp = abs(val - ((a_sp[-1])-val))
        
        if tmp < ans:
            ans = tmp
            
    print(ans)
        
if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

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

S = sum(A)

k = 0
ans = int(1E18)
for i, a in enumerate(A):
    if i == N-1:
        break
    k += a
    ans = min(ans, abs(2*k-S))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate


n = int(input())
a = list(map(int, input().split()))
a_acc = list(accumulate(a))
print(min(abs(a_acc[-1] - 2 * x)for x in a_acc[:-1]))

Submission status: Accepted

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

    sum_a = sum(a)
    min_diff = float('inf')
    sum_first = 0
    for i in range(N-1):
        sum_first += a[i]
        if min_diff > abs(sum_a - 2 * sum_first):
            min_diff = abs(sum_a - 2 * sum_first)

    print(min_diff)

main()

Submission status: Accepted

Solution code:
from copy import *
from sys import *
import math
import queue
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac

setrecursionlimit(1000000)


def main():
	n = int(input())
	a = list(map(int,input().split()))
	r = [a[0]]
	for i in range(1,n):
		r.append(r[i-1] + a[i])
	s = sum(a)
	m = []
	for i in range(n-1):
		m.append(abs(2*r[i]-s))
	print(min(m))

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(m,n):
	x = max(m,n)
	y = min(m,n)
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	return y

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

main()

Submission status: Accepted

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

a_sum = sum(A)

sums = [0] * (N + 1)
diffs = [0] * (N + 1)

for n in range(1, N + 1):
    sums[n] = sums[n - 1] + A[n - 1]
    diffs[n] = abs(a_sum - 2 * sums[n])

diffs.pop(len(diffs) - 1)
diffs.pop(0)

print(min(diffs))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    if not (a % 3 and b % 3 and (a + b) % 3):
        print("Possible")
    else:
        print("Impossible")
    return

#B
def B():
    _, n = L
    I()
    l = LI()
    l.sort(reverse = True)
    print(sum(l[:n]))
    return

#C
def C():
    n = II()
    a = LI()
    for i in range(n-1):
        a[i + 1] += a[i]
    maxa = a[-1]
    ans = inf
    for i in range(n-1):
        ans = min(ans, abs(2 * a[i] - maxa))
    print(ans)
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

ltor=[]
rtol=[]
tmp_ltor=0
tmp_rtol=0
for i in range(N):
    tmp_ltor+=a[i]
    ltor.append(tmp_ltor)
    tmp_rtol+=a[N-1-i]
    rtol.append(tmp_rtol)
num=2*10**9
for i in range(N-1):
    num=min(num,abs(ltor[i]-rtol[N-1-i-1]))
print(num)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
a_list=list(map(int,input().split()))
S=sum(a_list)
x=0
ans=10**20
for i in range(n-1):
    x+=a_list[i]
    y=S-x
    temp=abs(x-y)
    ans=min(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a_list=list(map(int,input().split()))
S=sum(a_list)
x=0
ans=10**20
for i in range(n-1):
    x+=a_list[i]
    y=S-x
    temp=abs(x-y)
    ans=min(ans,temp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
left = 0
right = sum(A)
ans = float('inf')
for a in A[:-1]:
    left += a
    right -= a
    ans = min(ans, abs(left-right))
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = list(map(int,input().split()))
tmp = a[0]
S = sum(a)
ans = np.abs(S-2*tmp)
for i in range(1,N-1):
    tmp += a[i]
    ans = min(ans,np.abs(S-2*tmp))
    
print(ans)

Submission status: Accepted

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

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

ans = float("inf")
for i in range(n - 1):
    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()))

s1 = a[0]
s2 = 0
for i in range(1,N):
    s2 = s2 + a[i]
MIN = abs(a[0]-s2) 
for i in range(1,N):
    MIN = min(MIN,abs(s1-s2))
    s1 = s1+ a[i]
    s2 = s2 - a[i]
    
print(MIN)

Submission status: Accepted

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

left = [0] * N
right = [0] * N

left[0] = a[0]
right[-1] = a[-1]
for i in range(1, N):
    left[i] = a[i] + left[i-1]
for i in range(N-2, -1, -1):
    right[i] = a[i] + right[i+1]

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC67 C

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

front = a[0]
back = sum(a[1:])
mi = abs(front - back)
for i in range(1,n-1):
    back -= a[i]
    front += a[i]
    mi = min(mi,abs(front-back))
print(mi)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sum_arai = sum(A)
sum_sunuke = 0
dif = 0

ans = 10 ** 15

for i in range(N - 1):
    sum_arai -= A[i]
    sum_sunuke += A[i]
    dif = abs(sum_arai - sum_sunuke)
    if dif <= ans:
        ans = dif

print(ans)

Submission status: Accepted

Solution code:
n,x,*a,b=map(int,open(0).read().split())
y=sum(a)+b
z=abs(x-y)
for i in a:x+=i;y-=i;z=min(z,abs(x-y))
print(z)

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split())
y=sum(a)
z=abs(x-y)
for i in a[:-1]:x+=i;y-=i;z=min(z,abs(x-y))
print(z)

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split())
y=sum(a)
z=abs(x-y)
for i in a[:-1]:
    x+=i
    y-=i
    z=min(z,abs(x-y))
print(z)

Submission status: Accepted

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

left_sum = [0] * (N - 1)
right_sum = [0] * (N - 1)

left_sum[0] = a[0]
right_sum[0] = a[-1]
for i in range(1,len(a) - 1):
    left_sum[i] = left_sum[i-1] + a[i]

for i in reversed(range(1,len(a) - 1)):
    right_sum[N-1-i] = right_sum[N-2-i] + a[i]


ans = 9999999999999999

for i in range(len(left_sum)):
    ans = min(ans, abs(left_sum[i] - right_sum[N-2-i]))
print(ans)

Submission status: Accepted

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

snk = a[0]
ari = sum(a)-snk
ans = abs(snk-ari)
for i in range(n-2):
    snk += a[i+1]
    ari -= a[i+1]
    ans = min(ans, abs(snk-ari))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(a)
l = 0

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

print(ans)

Submission status: Accepted

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

li=[0]*(n+1)

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

all=sum(a)

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

Submission status: Accepted

Solution code:

N = int(input())

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


x = sum(a)
y = 0
su = N
ans = float("inf")

a.reverse()

for i in a:

        x -= i
        y += i
        su -= 1

        if su != N and su != 0:
            ans = min (ans , abs(x-y))

            #print (su,x,y)

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]
lasta = a[N-1]
g = 999999999999
for i in range(N-1):g=min(g,abs(2*a[i]-lasta))
print(g)

Submission status: Accepted

Solution code:
def main():
    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)


main()

Submission status: Accepted

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

Submission status: Accepted

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

right_list=[] #右に進む
left_list=[] #左に進む
diff_list=[] #差分

right_list.append(l[0])
left_list.append(l[n-1])

for i in range(1,n):
    right_list.append(right_list[i-1]+l[i])
    left_list.append(left_list[i-1]+l[n-1-i])

left_list.reverse()

for i in range(0,n-1):
    diff_list.append(abs(right_list[i]-left_list[i+1]))

print(min(diff_list))

Submission status: Accepted

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

sum_card = sum(card)
min_value = 10000000000
sunuke = 0
for i in range(0,N-1,1):
  sunuke += card[i]
  if abs(sum_card - 2*sunuke) < min_value:
    min_value = abs(sum_card - 2*sunuke)
  
print(int(min_value))

Submission status: Accepted

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

Submission status: Accepted

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

ls = []
ls.append(a[0])

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

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

print(ans)

Submission status: Accepted

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

s = 0
t = 0
a_sum = []
b_sum = []
for i in range(n-1):
    s += a[i]
    a_sum.append(s)
    t += a[n-i-1]
    b_sum.append(t)
ans = 2*(10**14)
for i in range(n-1):
    ans = min(ans, abs(a_sum[i]-b_sum[n-i-2]))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input());
a = input().split(" ");
x = int(a[0]);
y = 0;
for i in range(1,n) :
	y += int(a[i]);

ans = abs(x - y);
for i in range(1,n-1) :
	x += int(a[i]);
	y -= int(a[i]);
	if abs(x - y) < ans :
		ans = abs(x - y);
print(ans);

Submission status: Accepted

Solution code:
# ARC078C - Splitting Pile (ABC067C)
from itertools import accumulate


def main():
    n = int(input())
    lst = tuple(accumulate(map(int, input().rstrip().split())))
    total = lst[-1]
    ans = min(abs(total - i * 2) for i in lst[:-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC078C - Splitting Pile (ABC067C)
from itertools import accumulate


def main():
    n = int(input())
    lst = tuple(accumulate(map(int, input().rstrip().split())))
    ans = min(abs(lst[-1] - i * 2) for i in lst[:-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC067C - Splitting Pile
def main():
    n = int(input())
    lst = tuple(map(int, input().rstrip().split()))
    cur, total, ans = 0, sum(lst), float("inf")
    for i in lst[:-1]:
        cur += i
        ans = min(ans, abs(total - cur * 2))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC067C - Splitting Pile
def main():
    n = int(input())
    lst = tuple(map(int, input().rstrip().split()))
    l, r, ans = 0, sum(lst), float("inf")
    for i in lst[:-1]:
        l += i
        r -= i
        ans = min(ans, abs(l - r))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC067C - Splitting Pile
def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    l, r, ans = 0, sum(lst), float("inf")
    for i in lst[:-1]:
        l += i
        r -= i
        ans = min(ans, abs(l - r))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

snuke=0
araiguma=sum(a)
ans=10**15

for i in range(N-2):
    snuke+=a[i]
    araiguma-=a[i]
    ans=min(ans,abs(snuke-araiguma))

if N==2:
    print(abs(a[0]-a[1]))
else:
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
total_A = sum(A)
S = A[0]; ans = abs(total_A - 2*S);
for a in A[1:-1]:
    S += a
    ans = min(ans, abs(total_A - 2*S))
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())

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

f = list(accumulate(A))

b = list(accumulate(A[::-1]))
# print(f)

b = b[::-1]

# print(b)
ans_list = []
for i in range(0, N-1):

    ans_list.append(abs(f[i]-b[i+1]))

print(min(ans_list))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
acc = [0] * (N + 1)
for i in range(N):
    acc[i + 1] = acc[i] + a[i]
result = 10 ** 15
for i in range(1, N):
    x = acc[i]
    y = acc[N] - x
    result = min(result, abs(x - y))
print(result)

Submission status: Accepted

Solution code:
input()
l = list(map(int,input().split()))
tomato = sum(l)
s = 0
m = 1000000000000
for i in range(len(l)-1):
    s += l[i]
    if(m > abs(s-(tomato-s))):
        m = abs(s-(tomato-s))
print(m)

Submission status: Accepted

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

le,ri,le2,ri2 = 0,sum(l),sum(l),0
ans,ans2 = 10**10,10**10
for i in range(len(l)//2):
    le += l[i]
    ri -= l[i]
    le2 -= l[-(i+1)]
    ri2 += l[-(i+1)]
    ans = min(ans,abs(le-ri))
    ans2 = min(ans2, abs(le2-ri2))
    # print(ans,ans2)
else:
    print(min(ans,ans2))

Submission status: Accepted

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

le,ri = 0,sum(l)
ans = 10**10
for i in range(len(l)-1):
    le += l[i]
    ri -= l[i]
    if ans < min(ans,abs(le-ri)):
        print(ans)
        break
    else:
        ans = min(ans,abs(le-ri))
else:
    print(ans)

Submission status: Accepted

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

le,ri = 0,sum(l)
ans = 10**10
for i in range(len(l)-1):
    le += l[i]
    ri -= l[i]
    if ans < min(ans,abs(le-ri)):
        print(ans)
    else:
        ans = min(ans,abs(le-ri))
else:
    print(ans)

Submission status: Accepted

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

le,ri = 0,sum(l)
ans = 10**10
for i in range(len(l)-1):
    le += l[i]
    ri -= l[i]
    ans = min(ans,abs(le-ri))
else:
    print(ans)

Submission status: Accepted

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

le,ri = 0,sum(l)
ans = []
for i in range(len(l)-1):
    le += l[i]
    ri -= l[i]
    ans.append(abs(le-ri))
else:
    print(min(ans))

Submission status: Accepted

Solution code:
# from collections import defaultdict
# import sys
# input = sys.stdin.readline


def main():
    num = int(input())
    data = list(map(int, input().split()))

    a = data[0]
    b = sum(data) - a
    ans = abs(a - b)
    for i in range(1, num - 1):
        a += data[i]
        b -= data[i]
        ans = min(ans, abs(a - b))

    print(ans)





if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = float('inf')

for a in A[:-1]:
    if ans > abs(2*a - sm):
        ans = abs(2*a - sm)

print(ans)

Submission status: Accepted

Solution code:
# num = int(input())
# h, w = list(map(int, input().split()))
# arr = list(map(int, input().split()))

# y = goukei - x
# y - x = goukei - x - x = goukei - 2*x

num = int(input())
org_list = list(map(int, input().split()))

goukei = sum(org_list)

pre_min_diff = 10**12
wip_sum = 0

for i in range(num-1):
     wip_sum += org_list[i]
     min_diff = abs(goukei - wip_sum*2)
     if min_diff < pre_min_diff:
         pre_min_diff = min_diff

print(pre_min_diff)

Submission status: Accepted

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

s = sum(a_list)
mae = 0
min_diff = 10**16
for a in a_list[:-1]:
    mae += a
    ato = s - mae
    diff = abs(mae-ato)
    if diff < min_diff:
        min_diff =diff
print(min_diff)

Submission status: Accepted

Solution code:
n=int(input())
al=list(map(int,input().split()))
sm=sum(al)
t=0
ans=10**15
for i in range(n-1):
    t+=al[i]
    ans=min(ans,abs(sm-t*2))
print(ans)

Submission status: Accepted

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

Sum = sum(a)
c = 0
difMin = 10**14

for i in range(N-1):
    c += a[i]
    dif = abs(Sum - c*2)
    if dif < difMin:
        difMin = dif

print(difMin)

Submission status: Accepted

Solution code:
n = int(input())
list_a = map(int, input().split())
list_a = list(list_a)
total = sum(list_a)
l = 0
res = 100000000000
if n == 2:
    print(abs(list_a[0] - list_a[1]))
else:
    for i in range(0, n-1):
        #print("---")
        #print(l)
        #print(total)
        l += list_a[i]
        total -= list_a[i]
        res = min(res, abs(l - total))
    print(res)

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
a = list(map(int, input().split()))
aac = list(accumulate(a))
suma = sum(a)
ans = 1e10
for i in range(0, N - 1):
    ans = min(ans, abs(suma - 2 * aac[i]))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    s = sum(a)
    ret = float('inf')
    tmp = 0
    for i in range(N - 1):
        tmp += a[i]
        ret = min(ret, abs(tmp - (s - tmp)))
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()