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:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

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

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

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

        tmp = abs(x - y)
        if tmp < ans:
            ans = tmp
    print(ans)

Submission status: Accepted

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

sA = []
for i in range(len(A)):
  if i == 0:
    sA.append(A[0])
  else:
    sA.append(sA[i - 1] + A[i])

s = sum(A)
dA = [abs(2 * sA[j] - s) for j in range(len(sA) - 1)]

print(min(dA))

Submission status: Accepted

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

s = sum(a)
x = 0
ans = abs(2 * a[0] - s)
snuke = a[0]

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
val = 0
l = []
s = sum(a)
hlf = s/2
for i in range(n-1):
    val += a[i]
    l.append(abs(hlf - val))

val = min(l)
print(int(val*2))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().rstrip().split(" ")))
K = sum(a)
now = 0
ans = 10 ** 15
for i in range(N -1):
    now += a[i]
    K -= a[i]
    ans = min(ans,abs(K - now))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
l,r=a.pop(0),sum(a)
ans=10**10
for i in a:
    ans=min(ans,abs(l-r))
    l,r=l+i,r-i
print(ans)

Submission status: Accepted

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

diff = float('inf')
for i in range(N-1):
  if abs(sa[i] - (sa[-1] - sa[i])) < diff:
    diff = abs(sa[i] - (sa[-1] - sa[i]))

print(diff)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    inlis = list(map(int, input().split()))

    total = sum(inlis)
    ans = 10 ** 20

    for i in range(n-1):
        if i == 0:
            x = inlis[i]
        else:
            x += inlis[i]
        y = total - x
        tmp = abs(x-y)
        if tmp < ans:
            ans = tmp

    print(ans)





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
INF = float('inf')
N = int(input())
A = [int(x) for x in input().split()]

cumA = list(accumulate(A))
ans = INF
for i in range(N - 1):
    x = cumA[i]
    y = cumA[N - 1] - cumA[i]
    ans = min(ans, abs(x - y))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
S=sum(A)
x=A[0]
y=S-x
xy_min=abs(x-y)
for i in range(1,n-1):
  x+=A[i]
  y=S-x
  xy=abs(x-y)
  if xy<xy_min:
    xy_min=xy
print(xy_min)

Submission status: Accepted

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

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

ans=float('inf')
last=ruiseki[len(ruiseki)-1]
# print(last)
# print(ruiseki)
for i in range(1,len(ruiseki)-1):
  now=(last-ruiseki[i])
  ans=min(ans,abs(ruiseki[i]-now))
print(ans)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	n = int(input())
	#b , c = tin()
	#s = input()
	al=lin()
	sl = [0]*n
	st=0
	for i in range(n):
		st += al[i]
		sl[i] = st
		
	full=sl[-1]
	min_v=mod*mod
	for v in sl[:-1]:
		t = abs(full - v*2)
		min_v=min(min_v, t)
	print(min_v)
		
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

li = [0]

for i in range(n):
    li.append(li[i]+ L[i])

sumL = sum(L)
#print(sumL)
ans = 10**10
for i in range(1,n):
    diff = abs(li[i] - (sumL - li[i]))
    #print(diff,li[i],sumL-li[i])
    ans = min(ans,diff)        
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    A = nl()
    cumA = np.cumsum(A)
    total = cumA[-1]
    ans = 10 ** 10000
    for a in cumA[:-1]:
        ans = min(ans, abs(a - (total - a)))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

lsum = a[0]
rsum = sum(a[1:])
ans = abs(lsum - rsum)

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

    if res < ans:
        ans = res

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=2*(10**14)+1
s=sum(a)
x=0
y=s
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())
a = list(map(int, input().split()))
ans = float('inf')
l = 0
r = sum(a)

for i in range(N - 1):
    l += a[i]
    r -= a[i]
    ans = min(ans, abs(r - l))
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
x = 0
y = sum(a)
ans = 10**10
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,*a=map(int,open(0).read().split())

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

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
sums=0
a=sum(L)
mina=10**10
for i in range(N-1):
  a-=2*L[i]
  mina=min(mina,abs(a))
print(mina)

Submission status: Accepted

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

Submission status: Accepted

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

s = [0]*(n+1)
for i in range(n):
    s[i+1] += a[i] + s[i]
#print(s)
ans = float("inf")
for i in range(1, n):
    x = s[i]
    y = s[n]-s[i]
    ans = min(ans, abs(x-y))
print(ans)

Submission status: Accepted

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

cum=[0]
for i in range(N):
  cum.append(cum[-1]+a[i])
  
ans=10**20
for i in range(1,N):
  tmp = abs(cum[-1]-cum[i]-cum[i])
  ans = min(ans, tmp)
  
print(ans)

Submission status: Accepted

Solution code:









#!/usr/bin/env python3

# from numba import njit

# input = stdin.readline
INF = pow(10,10)
# @njit
def solve(n,a):
  res = INF
  x = 0
  y = sum(a)
  for i in range(n-1):
    x += a[i]
    y -= a[i]
    res = min(res,abs(x-y))
  return res



def main():
  N = int(input())
  # N,M = map(int,input().split())
  a = list(map(int,input().split()))
  print(solve(N,a))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

rui=[]
rui.append(a[0])
for x in range(1,n):
  b=rui[-1]+a[x]
  rui.append(b)

ans=[]
for y in range(n-1):
  ans.append(abs((rui[n-1]-rui[y])-rui[y]))
  
print(min(ans))

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
A = tuple(accumulate(map(int, input().split())))
x = A[-1]
y = A[0]
ans = abs(x - y - y)
for a in A[:-1]:
    ans = min(ans, abs(x - a - a))
print(ans)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
left = 0
right = sum(A)
MIN = float('inf')
for i in range(n-1):
    left += A[i]
    right -= A[i]
    #print(left, right)
    MIN = min(MIN,abs(left - right))
print(MIN)

Submission status: Accepted

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

sum_a=sum(a)
ans = 10**11
x=0
for i in range(N-1):
    x += a[i]
    diff = abs(sum_a - 2 * x)
    if diff<ans:
        ans=diff


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):
    a = A[i]
    x += a
    y -= a
    dif = abs(x - y)
    if dif < ans:
        ans = dif
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
sunuke = 0
arai = sum(a)
p = []
for i in range(len(a)-1):
  sunuke += a[i]
  arai -= a[i]
  p.append(abs(sunuke - arai))

print(min(p))

Submission status: Accepted

Solution code:
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import product, combinations,permutations
from copy import deepcopy
import sys
sys.setrecursionlimit(4100000)



if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    acc_A = []
    acc = 0
    for a in A:
        acc += a
        acc_A.append(acc)

    minv = 10e10
    for i in range(0, N-1):
        minv = min(abs(acc_A[i]-(acc_A[-1]-acc_A[i])), minv)
    print(minv)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = [int(i) for i in input().split()]
    cul_sum = [0] * (n + 1)

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

    ans = float("inf")

    for i in range(1, n):
        ans = min(abs(cul_sum[i] - (cul_sum[n] - cul_sum[i])), ans)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import itertools

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()


def solve():
    B = list(itertools.accumulate(A, initial=0))
    s = 0
    ans = 10 ** 12
    for i in range(N - 1, 0, -1):
        s += A[i]
        ans = min(ans, abs(s - B[i]))

    return ans


print(solve())

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = [0 for _ in range(N+1)]
for i in range(1,N+1):
    B[i] = B[i-1]+A[i-1]
cmin = 10**15
for i in range(1,N):
    cmin = min(cmin,abs(2*B[i]-B[N]))
print(cmin)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
a = list(map(int, input().split()))
acc = [0]

for ai in a:
    acc.append(acc[-1]+ai)

ans = 10**18

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

print(ans)

Submission status: Accepted

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

S = sum(A)

from itertools import accumulate
C = [0]+A
C = list(accumulate(C))
ans = 10**18
for i in range(1, n):
    s = C[i]
    a = S-s
    ans = min(ans, abs(s-a))
print(ans)

Submission status: Accepted

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

accum = []
csum = 0
for a in al:
    csum += a
    accum.append(csum)

asum = accum[-1]
ans = 10**10
for a in accum[:-1]:
    x = a
    y = asum-x
    ans = min(ans, abs(x-y))

print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

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

ans = INF
for i in range(1, n):
    now = abs(acc[i] - acc[0] - (acc[n] - acc[i]))
    ans = min(ans, now)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = list(accumulate(A))
    minimum = 10**18
    for i, c in enumerate(S):
        if i == N - 1: continue
        minimum = min(minimum, abs(2 * c - S[-1]))
    print(minimum)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

S=sum(A)

X=0
D=float("inf")
for i in range(N):
    X+=A[i]

    if i!=N-1:
        Y=S-X
        D=min(D,abs(X-Y))
print(D)

Submission status: Accepted

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

x = 0
y = sum(a)
ans = float("inf")
for i in range(n-1):
  x += a[i]
  y -= a[i]
  z  = abs(x-y)
  if z < ans: ans = z
print(ans)

Submission status: Accepted

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

total = sum(a)
s = 0
ans = 10**9 * 2 * 10**5
for i in range(0, n-1):
    s += a[i]
    ans = min(ans, abs(s - (total-s)))
    
print(ans)

Submission status: Accepted

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

f=0
b=sum(a)
m=abs(a[0]-sum(a[1:]))
for i in range(n-1):
    f+=a[i]
    b-=a[i]
    m=min(m,abs(f-b))
print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
a,b=l[0],sum(l[1:])
m=abs(a-b)
for v in l[1:-1]:
    a+=v
    b-=v
    m=min(m,abs(a-b))
print(m)

Submission status: Accepted

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

all_sum = sum(nums)
sums = [None] * N

min_diff_value = float('inf')
min_diff_index = -1


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

    lhs_value = sums[i]
    rhs_value = all_sum - lhs_value
    diff_value = abs(lhs_value - rhs_value)

    if diff_value < min_diff_value:
        min_diff_value = diff_value

print(min_diff_value)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())
from itertools import accumulate

cum = list(accumulate(As))
ans = 10**18
for i in range(N-1):
    ans = min(ans, abs(cum[i]-(cum[-1]-cum[i])))
print(ans)

Submission status: Accepted

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

ans=2*10**9+10
s=sum(a)
now=0
snuke=0
for i in range(N-1):
    snuke+=a[i]
    now=s-2*snuke

    ans=min(abs(now),ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

m = 10**10
x = 0
X = sum(a)

for i in range(n):
    x += a[i]
    if i+1 < n:
        m = min(m, abs(X-2*x))

print(m)

Submission status: Accepted

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

minint = 10**9

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

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

    print(minint)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

B = list(accumulate(A))
p = B[-1]
ans = 10**12
for i in range(N-1):
    ans = min(ans,abs(p-2*B[i]))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(T) for T in input().split()]
L = [0]*(N+1)
R = [sum(A)]*(N+1)
for T in range(0,N):
    L[T+1] = L[T]+A[T]
    R[T+1] = R[T]-A[T]
MIN = 10**10
for T in range(1,N):
    if abs(L[T]-R[T])<MIN:
        MIN = abs(L[T]-R[T])
print(MIN)

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

suma = sum(a)
ans = float("inf")
t = 0
for i in range(n-1):
	t += a[i]
	ans = min(ans,abs(suma-t-t))

print(ans)

Submission status: Accepted

Solution code:
import math
ans = math.inf
n = int(input())
c = list(map(int, input().split()))
acm = [0]*(n+1)
for i, v in enumerate(c, 1):
    acm[i] += acm[i-1] + v

s, a = 0, acm[n]
for i in range(1, n):
    s = acm[i]
    ans = min(ans, abs((a-s)-s))

print(ans)

Submission status: Accepted

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]
sum = sum(a)
ans = 10**10+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: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()
b = list(accumulate(a))
ans = inf
for i in range(n-1):
    ans = min(ans, abs(b[i]-(b[n-1]-b[i])))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
s = sum(a)
ans = 10**18
c = 0
for i in range(n - 1):
    c += a[i]
    ans = min(ans, abs(2 * c - s))
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
s_lst=[0]
for i,aa in enumerate(a):
  s_lst.append(s_lst[i]+aa) # len(s_lst): n+1
s_all=s_lst[-1]
ans=float("inf")
for i in range(1,len(s_lst)-1):
  ans=min(ans,abs(2*s_lst[i]-s_all))
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    total = sum(A)
    ans = INF
    s = 0
    for i in range(N-1):
        s += A[i]
        if ans > abs(total - 2 * s):
            ans = abs(total - 2 * s)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    total = sum(A)
    ans = INF
    s = 0
    for a in A[:-1]:
        s += a
        if ans > abs(total - 2 * s):
            ans = abs(total - 2 * s)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    csum = [0]
    csum.extend(accumulate(A))

    ans = INF
    for i in range(1, N):
        tmp = abs(csum[i] - (csum[N] - csum[i]))
        if ans > tmp:
            ans = tmp

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,sys.stdin.readline().strip().split()))
snk=a[0]
rac=sum(a[1:])

s=sum(list(map(abs,a)))
for i in range(1,n-1):
 snk+=a[i]
 rac-=a[i]
 s=min(s,abs(snk-rac))

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

#累積和を作成
s_list=[0]
sum=0
for i in a_list:
    sum+=i
    s_list.append(sum)

#print(s_list)

target=s_list[-1]//2

tmp1=10**10
for i in range(1,N):
    tmp2=abs(target-s_list[i])
    tmp1=min(tmp1,tmp2)#より小さいほうをtmp1に格納
    if tmp1 == tmp2:
        ans_index=i

#print(ans_index)
print(abs(s_list[-1]-2*s_list[ans_index]))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
a_c = a.cumsum()
a_t = a_c[-1]
diff = np.abs(2 * a_c - a_t)
ans = diff[:-1].min()
print(ans)

Submission status: Accepted

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

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

res = 10**10
for i in range(N-1):
    res = min(abs(l[i]-r[i+1]), res)

print(res)

Submission status: Accepted

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

# In[11]:


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


# In[12]:


ans = (10**9)*N
x = 0
y = sum(a)
for i in range(N-1):
    x += a[i]
    y -= a[i]
    ans = min(ans,abs(x-y))
    if ans < 2:
        break
print(ans)


# In[ ]:

Submission status: Accepted

Solution code:
INF = 10 ** 18
N = int(input())
A = list(map(int, input().split()))
res = INF
t = sum(A)
s = 0
for a in A[:-1]:
    s += a
    res = min(res, abs(t - s - s))
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sum_a = sum(a)
s = 0
res = float('inf')
for v in a[:-1]:
    s += v
    res = min(res, abs(sum_a - 2 * s))

print(res)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

Submission status: Accepted

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

    def accumulate(a):
        s = 0
        for x in a:
            s += x
            yield s

    *acc, = accumulate(A)
    acc.pop()

    tot = sum(A)

    ans = -1
    for x in acc:
        if ans > (abs_ := abs(tot - x - x)) or not (~ans):
            ans = abs_

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# C - Splitting Pile
N = int(input())
a = list(map(int,input().split()))

lst = [0]*(N-1)
x = [a[0]]*(N-1)
y = [sum(a)-a[0]]*(N-1)
lst[0] = abs(x[0]-y[0])

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
 
x = 0
ans = []
suma = sum(a)
 
for i in a[:-1]:
    x += i
    ans.append(abs(suma - x * 2))
else:
    print(min(ans))

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

S = sum(A)
k = 0
ans = 10**18
for i,a in enumerate(A, start=1):
    k += a
    if N == i:
        break
    ans = min(ans, abs(S - 2*k))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
a_sum = [0]
tmp = 0
for i in range(1,N+1):
  tmp = a_sum[i-1] + a[i-1]
  a_sum.append(tmp)
ans = 10**30
x,y = 0,0
for i in range(1,N):
  x = a_sum[i]
  y = a_sum[N] - 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")
X = sum(a)

x = 0
for i in range(n):
    x += a[i]
    if i+1 < n:
        ans = min(ans,abs(X-2*x))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = [0]
for i in range(N):
  temp = S[-1]+A[i]
  S.append(temp)
total = sum(A)
target = total//2
INF = float("inf")
ans = INF

for i in range(N-1): #0からN-1
  temp = abs(total-2*S[i+1])
  ans = min(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

n = ini()
a = inl()


def solve():
    al = list(accumulate(a))
    ar = list(reversed(list(accumulate(a[::-1]))))
    ans = 1 << 60
    for i in range(1, n):
        r = abs(al[i - 1] - ar[i])
        ans = min(ans, r)

    return ans


print(solve())

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
x = sum(A)
y = 0
z = []

for n in range(N-1):
  y+=A[n]
  z.append(abs(x-2*y))

print(min(z))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = [0]*N
sm = 0
A = list(map(int,input().split()))
for i in range(N):
    ai = A[i]
    sm += ai
    S[i] = sm
score = 0
min_score = float("inf")
for i in range(N-1):
    score = abs(S[i]-(S[N-1]-S[i]))
    min_score = min(score,min_score)
print(min_score)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

if n == 2:
  print(abs(A[0]-A[1]))
  exit()
  
B = list(accumulate(A))

ans = abs(B[-2]-A[-1])
y = 0
for i in range(len(A)-1, 0, -1):
    x = B[i-1]
    y += A[i]
    #print(x, y, ans)
    ans = min(ans, abs(x-y))

print(ans)

Submission status: Accepted

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