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
input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
N = int(input())

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

c_sum_left = [0]
total = 0
for i in range(N):
    c_sum_left += [c_sum_left[i] + a[i]]
    total += abs(a[i])

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


ans = total
for i in range(1, N):
    # print (c_sum_left[i], c_sum_right[N-i])
    ans = min(ans, abs(c_sum_left[i] - c_sum_right[N-i]))
print (ans)

Submission status: Accepted

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

def main():
    sum_n = sum(A)
    mid = sum_n / 2
    a = 0
    abs_list = []
    for i in range(N - 1):
        a += A[i]
        abs_list.append((abs(mid - a), i))    
    abs_list.sort()
    idx = abs_list[0][1]

    left = sum(A[:idx + 1])
    right = sum(A[idx + 1:])
    print(abs(left - right))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

s = sum(a)

w = None
for ai in a[:-1]:
  s -= 2 * ai
  if w is None:
    w = abs(s)
  else:
    w = min(w, abs(s))
print(w)

Submission status: Accepted

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

Submission status: Accepted

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

temp = sum(a) - 2*a[0]
ans = abs(temp)
for i in range(1,N-1):
    temp -= 2*a[i]
    if ans > abs(temp): ans = abs(temp)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

l.reverse()

x = 0
sum_x = []
sum_y = [sum(l)]*(n-1)
abs_xy = []

for i in range(n-1):
    x += l[i]
    sum_x.append(x)
    sum_y[i] -= sum_x[i]
    abs_xy.append(abs(sum_x[i]-sum_y[i]))


print(min(abs_xy))

Submission status: Accepted

Solution code:
n = int(input())

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

l.reverse()

x = 0

sum_x = []

for i in range(n-1):
    x += l[i]
    sum_x.append(x)

sum_y = [sum(l)]*(n-1)

for i in range(n-1):
    sum_y[i] -= sum_x[i]
    
abs_xy = []

for i in range(n-1):
    abs_xy.append(abs(sum_x[i]-sum_y[i]))

print(min(abs_xy))

Submission status: Accepted

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

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  al=sum(l)

  sm=0
  sa=inf
  for x in l[:-1]:
    sm+=x
    sa=min(sa,abs(sm-(al-sm)))

  return sa

print(main())

Submission status: Accepted

Solution code:
import numpy as np

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

A = np.array(A)
A_cumsum = np.cumsum(A)

solver = []
for i in range(N-1):
  snuke = A_cumsum[i]
  racoon = A_cumsum[-1] - A_cumsum[i]
  solver.append(np.abs(snuke - racoon))
  
print(min(solver))

Submission status: Accepted

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

L = A[0]
R = sum(A[1:])
A.pop(0)
A.pop(-1)
ans = abs(L-R)

for a in A:
	L += a
	R -= a
	if abs(L-R) < ans:
		ans = abs(L-R)
print(ans)

Submission status: Accepted

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

stim = [0]
ans = float('inf')
for i in a:
    stim.append(stim[-1]+i)
for i in stim[1:len(stim)-1]:
    ans = min(ans,abs(stim[-1]-2*i))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
card_list = [ int(v) for v in input().split() ]


s = sum(card_list) 
a = card_list[0]
b = s - a
ans = n*10**9

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(),dtype=np.int64)

Acum = A.cumsum()
total = Acum[-1]
diff = np.abs(Acum - (total-Acum))

answer = diff[:-1].min() # 最後の1枚はアライさん

print(answer)

Submission status: Accepted

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

N = int(input())
A = [int(v) for v in input().split()]
p = sum(A)
L = [0] * (N + 1)

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

ans = 10 ** 18
for i in range(N - 1):
    t = L[i + 1] - L[0]

    ans = min(ans, abs(t - p + t))

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
N, *a = map(int, open(0).read().split())
x = list(accumulate(a[:-1]))
y = list(accumulate(a[:0:-1]))[::-1]
print(min(abs(i - j) for i, j in zip(x, y)))

Submission status: Accepted

Solution code:

N = int(input())
cards = list(map(int,input().split(" ")))
ruiseki = [cards[0]]
su  = sum(cards)
for i,card in enumerate(cards[1:-1]): #[1:-1]→片方が全部取るケースを除外
    ruiseki.append(ruiseki[i]+card)

ans = 10e15
for temp  in ruiseki:
    x = temp
    y = su - temp
    cand = abs(x-y)
    if cand < ans:
        ans = cand

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
mae=[0]*n
usi=[0]*n
ma=0
for i in range(n):
    ma+=l[i]
    mae[i]=ma
us=0
for i in range(n-1,-1,-1):
    us+=l[i]
    usi[i]=us
ans=10000000000000000000
for i in range(0,n-1):
    ans=min(ans,abs(mae[i]-usi[i+1]))
    
print(ans)

Submission status: Accepted

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

b = []

t = 0
for aa in a:
    t += aa
    b.append(t)
s = b[-1]

ans = 100000000000000000

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
a = list(map(int,input().split()))
acc = list(accumulate(a))
m = float("inf")
for i in range(0, N-1):
    x = acc[i]
    y = acc[-1]-x
    m = min(abs(x-y), m)
print(m)

Submission status: Accepted

Solution code:
n = int(input())

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

arr_sum = sum(arr)

temp_sum = 0
ans_arr = []
for i in arr:
    temp_sum += i
    ans_arr.append(abs(arr_sum - temp_sum * 2))

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

Submission status: Accepted

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

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

    # 累積和で解く
    # ruiseki[i] := i番目までの累積和(0 <= i <= N-1)
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    for i in range(1, N):
        ruiseki[i] = ruiseki[i-1] + A[i]
    
    min_diff = float("inf") # xとyの最小差
    for i in range(0, N-1):
        x = ruiseki[i]
        y = ruiseki[N-1] - ruiseki[i]
        min_diff = min(min_diff, abs(x-y))
    
    print(min_diff)

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ans = 10**40
CS = [0] * (N+1)

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a_list=[int(i) for i in input().split()]
total=sum(a_list)
diff=sum(abs(a_list[i]) for i in range(n))
left=0
for i in range(n-1):
    left+=a_list[i]
    right=total-left
    diff=min(diff,abs(left-right))
print(diff)

Submission status: Accepted

Solution code:
# /usr/bin/python
# -*- coding: utf-8 -*-
import math


N = int(input())
An = list(map(int, input().split()))
sm = [0, sum(An)]

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=[]
tmp=0
for i in range(n):
    tmp+=a[i]
    l.append(tmp)
ans=[]
for k in range(n-1):
    ans.append(abs(l[k]-(l[-1]-l[k])))
print(min(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]
print(min([abs(a[N-1] - 2 * a[i]) for i in range(N-1)]))

Submission status: Accepted

Solution code:
inf = float('inf')

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

sum_A = sum(A)
ans = inf
res = 0

for a in A[:-1]:
    res += a
    ans = min(ans,abs(sum_A-2*res))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
lst_a = list(map(int, input().split()))
total = sum(lst_a)
x = 0
ans = float("INF")


for i in range(N - 1):
    x += lst_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()))

goukei = sum(a)

x = 0 
ans = 10 ** 16
for i in range(N-1):
    x += a[i]
    ans = min(ans, abs(goukei - 2 * x))

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
C = []
num = 0
for i in range(N):
    num += A[i]
    C.append(num)
A.reverse()
D = []
num = 0
for i in range(N):
    num += A[i]
    D.append(num)
D.reverse()
ans = float('inf')
for i in range(0, N - 1):
    ans = min(ans, abs(C[i] - D[i + 1]))
print(ans)

Submission status: Accepted

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

num = 0
for i in range(N):
    num += A[i]
    C.append(num)
A.reverse()
D = []
num = 0
for i in range(N):
    num += A[i]
    D.append(num)
D.reverse()
ans = float('inf')
for i in range(0, N - 1):
    ans = min(ans, abs(C[i] - D[i + 1]))
print(ans)

Submission status: Accepted

Solution code:
#ABC067C
N = int(input())
an = list(map(int, input().split()))
snk = an[0]
ari = sum(an[1:])
ans = abs(snk-ari)
for i in range(N-2):
    snk += an[i+1]
    ari -= an[i+1]
    ans = min(ans, abs(snk-ari))
print(ans)

Submission status: Accepted

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

s = sum(a)

# |x - y| = |x - (s - x)| = |2x - s|

x = [a[0]]*(N-1)

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

print(min(map(lambda y:abs(2*y-s),x)))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc067/tasks/arc078_a

import itertools
from collections import Counter
from collections import defaultdict
import bisect

def main():
    N = int(input())
    A = list(map(int, input().split()))
    total = sum(A)
    INF = int(1e15)
    ans = INF
    acc = 0
    for i in range(len(A) - 1):
        acc += A[i]
        ans = min(ans, abs(total - 2 * acc))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [0 for i in range(n)]
b[0] = a[0];
for i in range(1, n) :
    b[i] = b[i-1]+a[i]
tot = b[n-1]
ans = 10000000000
for i in range(0, n-1) :
    x = b[i]
    y = tot - x
    if abs(x-y) < ans :
        ans = abs(x-y)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

*B, su = list(accumulate(A))
print(min(abs(su - 2 * b) for b in B))

Submission status: Accepted

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

su = sum(A)
x = 0
mi = 1e15
for a in A[:-1]:
    x += a
    mi = min(mi, abs(su - 2 * x))
print(mi)

Submission status: Accepted

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

l, r = A[0], sum(A[1:])
mi = abs(l - r)
for a in A[1:N - 1]:
    l, r = l + a, r - a
    mi = min(mi, abs(l - r))
print(mi)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
sum = sum(a)
x = 0
y = sum
min_d = float('inf')
for i in range(N-1):
  x += a[i]
  y -= a[i]
  if abs(x-y) < min_d:
    min_d = abs(x-y)
print(min_d)

Submission status: Accepted

Solution code:
from collections import Counter
from functools import reduce
# import statistics
import bisect
import copy
import fractions
import math
import pprint
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) 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 MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))
def unite(x, y):
    # それぞれのノードの根を求める
    x = root(x)
    y = root(y)
    if x == y:
        return
    # node[x]の根をyに変更する
    node[x] = y
def same(x,y): return bool(root(x) == root(y))
def root(x):
    if node[x] == x:  # xが根の場合
        return x
    else:
        node[x] = root(node[x])  # 経路圧縮
        return node[x]
def dfs(v):
    # ノードに訪れた
    visited[v] = True
    for v2 in range(n):
        if graph[v][v2] == False:  # そもそもvとv2が辺でつながっていない
            continue
        if visited[v2] == True:  # もう見た
            continue
        dfs(v2)


n, a = II(), LI()
ruiseki = [0] * n
ruiseki[0] = a[0]
allsum = sum(a)
if n == 2:
    print(abs(a[0] - a[1]))
    exit()

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

mini = INF
for i in range(0, n-1):
    # abs(ruiseki[i] - (allsum - ruiseki[i])
    mini = min(mini, abs(2 * ruiseki[i] - allsum))

print(mini)

Submission status: Accepted

Solution code:

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))

    return n, alist


def submit():
    n, alist = read_input()

    total = sum(alist)

    s = 0
    mindiff = float('inf')
    for i in range(n - 1):
        s += alist[i]
        rest = total - s

        diff = abs(s - rest)
        if diff < mindiff:
            mindiff = diff

    print(mindiff)

if __name__ == '__main__':
    submit()

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy

def j(q):
    if q==1: print("Heisei")
    else:print("TBD")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())


n = ip()                              #入力整数1つ
#n,k= (int(i) for i in input().split())      #入力整数横2つ
#n,x,y = (int(i) for i in input().split())    #入力整数横3つ
#n,a,b,c= (int(i) for i in input().split())  #入力整数横4つ
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)

#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので
r = sum(a)
l = a[0]
r-= a[0]
s = abs(l-r)
for i in range(1,n-1):
	l+=a[i]
	r-=a[i]
	s = min(s,abs(l-r))
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

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

sumA = nums[-1]
ans = 10**20
for a in nums[1:-1]:
    p = abs(2*a-sumA)
    ans = min(ans, p)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def c_splitting_pile(N, A):
    # Aの要素の総和をX、カードの山の先頭i枚の総和をxとすると、
    # |y-x|=|X-2x|である。各i(1<=i<=N-1)についてこの値を計算すればよい
    from itertools import accumulate
    total = sum(A)
    return min([abs(total - 2 * x) for x in accumulate(A[:-1])])

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

Submission status: Accepted

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

l = [0] * (N+1)
r = [0] * (N+1)

for i in range(N):
    l[i+1] = l[i] + a[i]

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

res = []

for i in range(1,len(l)-1):
    res.append(abs(l[i]-r[i]))

print(min(res))

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n  = f()[0]
    a = f()

    s = [0] * n
    for i in range(n):
        s[i] = a[i] + (s[i-1] if i>0 else 0)

    mina = abs(s[n-1]-2*s[0])

    for i in range(n-1):
        mina = min(mina, abs(s[n-1] - 2*s[i]))
    
    print(mina)

solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

S = sum(a)

ans = 10 ** 18
x = 0
for i in range(N-1):
    x += a[i]
    ans = min(ans, abs((S-x)-x))

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

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

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

sum_a = sum(a)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
sum_a = []
a = [int(i) for i in input().split()]
tmp = 0
for i in a:
    tmp += i
    sum_a.append(tmp)

for i in range(N-1):
    if i == 0:
        ans = abs(sum_a[i] - (tmp - sum_a[i]))
    else:
        ans = min(ans, abs(sum_a[i] - (tmp - sum_a[i])))

print(ans)

Submission status: Accepted

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

b = sum(a)
c = [0] * (N+1)

for i in range(N):
    c[i+1] = c[i] + a[i]

ans = int(3e9)
for i in range(1, N):
    score = abs(c[i]-(b-c[i]))
    if ans > score:
        ans = score

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input().rstrip())
    a = list(int(_) for _ in input().split())
    sum_p = 0
    sum_q = sum(a)
    r = INF
    for i in range(N-1):
        sum_p += a[i]
        sum_q -= a[i]
        r = min(r,abs(sum_p-sum_q))
    print(r)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import ceil
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
Asum = sum(A)
ans = 10**10
x = 0
for i in range(N-1):
    x+=A[i]
    ans = min(ans, abs(Asum-2*x))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
tmp = 0
ans = 10 ** 10
Asum = sum(A)
for i in range(N - 1):
    tmp += A[i]
    a = abs((Asum - tmp) - tmp)
    if ans > a:
        ans = a
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import math
N = int(input())
lst = list(map(int,input().split()))
x = lst[0]
y = sum(lst[1:])
ans = abs(y - x)
for i in range(1,N-1):
    x+=lst[i]
    y-=lst[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 = 0
for i in range(1, n):
    y += a[i]
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:
import functools

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

def append_to_partial_sums(array, a_i):
    array.append((0 if len(array) == 0 else array[-1]) + a_i)
    return array

snuke = list(functools.reduce(append_to_partial_sums, a, []))
raccoon = list(reversed(functools.reduce(append_to_partial_sums, reversed(a), [])))
print(min(abs(snuke[i - 1] - raccoon[i]) for i in range(1, n)))

Submission status: Accepted

Solution code:
import itertools

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

B = list(itertools.accumulate(A))
ans = 10000000000
for i in range(N-1):
    if ans > abs(B[-1] - 2*B[i]):
        ans = abs(B[-1] - 2*B[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

sum_a = sum(a)

#i = 0の場合
left = a[0]
right = sum_a - left
delta_min = abs(left - right)
left_ = left

# i >= 1の場合
for i in range(1, n-1):
    left = left_ + a[i]
    right = sum_a - left

    delta = abs(left - right)

    delta_min = min(delta_min, delta)
 
    left_ = left

print(delta_min)

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
  if(abs(x-y) < ans):
    ans = abs(x-y)
    
print(ans)

Submission status: Accepted

Solution code:
import bisect
n=input()
a=list(map(int,input().split()))
if n=="2":print(abs(a[0]-a[1]))
else:
    s=[]
    x=0
    for i in a[:-1]:
        x+=i
        s.append(x)
    s.sort()
    s.append(float("inf"))
    y=bisect.bisect_left(s,sum(a)/2)
    print(min(abs(sum(a)-2*s[y]),abs(sum(a)-2*s[y-1])))

Submission status: Accepted

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

ta = []
S = 0
for ai in a:
    S += ai
    ta.append(S)
ans = 10 ** 16
for i in range(len(ta) - 1):
    ans = min(ans, abs(S - 2 * ta[i]))

print(ans)

Submission status: Accepted

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

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
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
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(input())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

N=_I()
A=LI()

Aac=[A[0]]

for aidx in range(1,N):
	Aac.append(Aac[aidx-1]+A[aidx])
ans=10**18
for a in Aac[:-1]:
	ans=min(ans,abs((Aac[-1]-a)-a))
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
a = nl()
summ = sum(a) - a[0]
sunn = a[0]
minn = abs(summ - sunn)
for i in range(1, n - 1):
    summ -= a[i]
    sunn += a[i]
    minn = min(minn, abs(summ - sunn))
print(minn)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
wa=sum(a)
curmin=100000000000000000000000000000
cursum=0
a.pop()
for card in a:
    cursum+=card
    curmin=min(curmin,abs(wa-2*cursum))
print(curmin)

Submission status: Accepted

Solution code:
n = int(input())
s = [ int(i) for i in input().split() ]
t = sum(s)
curr = 0
ans = 1e18
for i in range(n-1):
    curr += s[i];
    ans = min( ans, abs((t-curr)- curr));
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

s = 0

for i in range(n):
    s += data[i]
    data[i] = s

a = data[-1]
res = -1
for i in range(n-1):
    if res<0:
        res = abs(data[i]-(a-data[i]))
    else:
        res = min(res,abs(data[i]-(a-data[i])))
print(res)

Submission status: Accepted

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

p1 = 0
p2 = sum(A)
diff = 9999999999999999
for i in range(N-1):
    p1 += A[i]
    p2 -= A[i]
    diff = min(diff, abs(p1-p2))

print(diff)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
a = [int(item) for item in input().split()]
cumsum = list(accumulate(a))

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

print(ans)

Submission status: Accepted

Solution code:
#23:13-23:51
N=int(input())
A=list(map(int,input().split()))
X=sum(A)
x=0
ans=10**10
for i in range(len(A)):
    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()))
cumsum = [a[0]]
for i in range(len(a)-1):
  cumsum.append(cumsum[i] + a[i+1])

min = float('inf')
for i in range(len(cumsum)-1):
  f1 = cumsum[i]
  f2 = cumsum[-1] - cumsum[i]
  d = abs(f1 - f2)
  if min >  d:
    min = d

print(min)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
lst=[0]
for i in l:
    lst.append(lst[-1]+i)
sm=lst[-1]
ans=10**20
for i in range(1,n):
    x=lst[i]
    y=sm-x
    a=abs(x-y)
    ans=min(ans,a)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
S = sum(a)
f, m = 0, 10**20
for i in range(N - 1):
    f += a[i]
    m = min(abs(S - 2 * f), m)
print(m)

Submission status: Accepted

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

min_dif = float('inf')
s1 = 0
for i in range(N):
  if(i != N-1):
   s1 += A[i]
   s2 = sum_all - s1
   tmp_dif = abs(s1 - s2)
   min_dif = min(min_dif,tmp_dif)
print(min_dif)

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]
ans = 10**10
for i in range(n-1):
    ans = min(ans,abs(a[-1]-a[i]-a[i]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
t = (sum(a))
x = t
ans = 10 ** 10
for ai in a[:-1]:
    x -= 2 * ai
    if ans > abs(x):
        ans = abs(x)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))
    B = list(accumulate(A))

    total = sum(A)
    ans = float('inf')
    for b in B[:N-1]:
        ans = min(ans, abs((total - b) - b))

    return ans



if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

X = sum(a)

minsum = 10000000000

cnt = 0

for i in range(1,n):
    cnt += a[i-1]
    minsum = min(minsum,abs(X-2*cnt))

print(minsum)

Submission status: Accepted

Solution code:
N = int(input())
alist = list(map(int,input().split()))
ans = 10 ** 10
cnt = 0
asum = sum(alist)
for a in alist[:-1]:
    cnt += a
    diff = abs(asum - 2 * cnt)
    if ans > diff:
        ans = diff
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
Sum=sum(a)
ans=10**18
cnt=0
for i in a[:n-1]:
    cnt+=i
    diff=abs(Sum-2*cnt)
    if ans>diff:
        ans=diff
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = sum(A)
ans = 10**20
x = A[0]

for a in A[1:]:
    n = total - x
    ans = min(ans, abs(n-x))
    x += a
print(ans)