Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

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

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

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

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

Sample Input 2

2
10 -10

Sample Output 2

20

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



Sample input:
6
1 2 3 4 5 6


Sample output:
1

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
sum = int(0)
arai = int(0)
ans = int(0)
mini = float('inf')

for i in range(N):
	sum+=a[i]
for i in range(N-1):
	ans+=a[i]
	arai=sum-ans
	mini=min(mini,float(abs(ans-arai)))

print(int(mini))

Submission status: Accepted

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

x, y = 0, sum(a)
ans = abs(a[0] - (sum(a) - a[0]))
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 = 10**50
x, y = 0, sum(A)
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()))
for i in range(N-1):
    A[i+1] = 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)

Submission status: Accepted

Solution code:

import sys
import numpy as np

readline = sys.stdin.readline

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

    base0 = A[0]
    base1 = sum(A) - base0

    ans = abs(base1 - base0)

    for i in range(1,N-1):
        base0 += A[i]
        base1 -= A[i]
        ans = min(abs(base1 - base0), ans)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
def main():
  N = int(input())
  A = [int(_) for _ in input().split()]
  B = list(accumulate(A))
  s = B[-1]
  B = [abs(2 * b - s) for b in B]
  print(min(B[:-1]))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    total = sum(a)
    ans = []
    x = 0
    for i in range(n - 1):
        x += a[i]
        y = total - x

        ans.append(abs(x - y))

    print(min(ans))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """6
1 2 3 4 5 6"""
        output = """1"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """2
10 -10"""
        output = """20"""
        self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
mn_list = []
sunuke = a[0]
araiguma = sum(a[1:])
res = abs(sunuke-araiguma)
for i in range(1,n-1):
    sunuke += a[i]
    araiguma -= a[i]
    res = min(res,abs(sunuke-araiguma))
print(res)

Submission status: Accepted

Solution code:
data = int(input())
array = list(map(int, input().split()))
counter = 0
l = sum(array)
r = []
for a in array[:-1]:
  counter += a
  k = abs(l - 2*counter)
  r.append(k)
r.sort()
print(r[0])

Submission status: Accepted

Solution code:
import itertools
n=int(input())
a=list(map(int,input().split()))
li=list(itertools.accumulate(a))
ans=float("inf")
#print(li)

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
b=a.cumsum()
ans=float("inf")
for i in range(n-1):
    ans=min(ans,abs(b[-1]-b[i]-b[i]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
def slove(n, a):
    total = sum(a)
    ans = min(map(lambda x: abs(total - x*2), itertools.accumulate(a[:-1])))
    return ans

if __name__ == "__main__":
    n = int(input())
    a_list = [int(i) for i in input().split()]
    print(slove(n, a_list))

Submission status: Accepted

Solution code:
n = int(input())
num_list = list(map(int,input().split()))
sum_cnt = sum(num_list)
sunuke = 0
arai = 0
tmp = 0
tmp_list = []
for i in range(len(num_list)-1):
    if i == 0:
        sunuke += num_list[i]
        arai = (sum_cnt - sunuke)
        tmp = abs(arai - sunuke)
        tmp_list.append(tmp)
    else:
        sunuke += num_list[i]
        arai = (sum_cnt - sunuke)
        tmp = abs(arai - sunuke)
        tmp_list.append(tmp)
print(min(tmp_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split()))
    x=a[0]
    y=sum(a)-a[0]
    ans=abs(x-y)
    for i in range(1,n-1):
        x+=a[i]
        y-=a[i]
        ans=min(abs(x-y),ans)
    print(ans)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
sa=[]
a=list(map(int,input().split()))
s=0
for ai in a:
  s+=ai
  sa.append(s)

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

Submission status: Accepted

Solution code:
n, *A = map(int, open(0).read().split())
x, y = A[0], sum(A[1:])

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

Submission status: Accepted

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

print(mini)

Submission status: Accepted

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

Submission status: Accepted

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

sunuke = A[0]
araiguma = sum(A[1:])
res = abs(sunuke - araiguma)
for i in range(1, N - 1):
    sunuke += A[i]
    araiguma -= A[i]
    res = min(res, abs(sunuke - araiguma))
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

sm = sum(A)
x = 0
y = sm
ans = 1001001001001001001

for idx, a in enumerate(A):
  if idx == n - 1:
    break
  
  x  = x + a
  y = y - a
  # print(x, y, abs(x - y))
  ans = min(ans, abs(x - y))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

cum_sum = np.cumsum(a)
track_min = float('inf')

for i in range(n-1):
    x = int(cum_sum[i])
    y = int(cum_sum[-1]-cum_sum[i])
    diff = abs(x-y)
    if diff<track_min:
        track_min = diff
    
print(track_min)

Submission status: Accepted

Solution code:
import sys

n, *a = map(int, sys.stdin.read().split())

def main():
    for i in range(n - 1): a[i+1] += a[i]
    res = float('inf')
    for i in range(n - 1): res = min(res, abs(a[-1] - a[i] * 2))
    print(res)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
l = list(map(int,input().split()))
s = sum(l)
l1 = list(accumulate(l))
del l1[-1]
l2 = [abs(2*i-s) for i in l1]
print(min(l2))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    x = a[0]
    y = sum(a) - x
    x_y = abs(x - y)
    for i in range(1, n - 1):
        x += a[i]
        y -= a[i]
        x_y = min(x_y, abs(x - y))
    print(x_y)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    cards = list(map(int,input().split()))
    ruisekiwa = [0] + [0] * n

    for i in range(0,n):
        ruisekiwa[i+1] = ruisekiwa[i] + cards[i]
    #print(cards)
    #print(ruisekiwa)
    mn = 100000000000000000000000000
    for sunuke_take in range(1,n):
        #print()
        araiguma_take = n - sunuke_take
        sunuke_sum = ruisekiwa[sunuke_take] - ruisekiwa[0]
        araiguma_sum = ruisekiwa[n] - ruisekiwa[sunuke_take]
        mn = min(mn,abs(sunuke_sum-araiguma_sum))
        #print(sunuke_take,araiguma_take)
        #print(sunuke_sum,araiguma_sum)
    print(mn)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
sunuke = sum(A)
arai = 0

f_inf = float("inf")
res = f_inf
for i in range(n - 1):
    arai += A[i]
    sunuke -= A[i]
    res = min(res, abs(sunuke - arai))
print(res)

Submission status: Accepted

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

x = [0 for i in range(n-1)]
y = [0 for i in range(n-1)]
num = sum(lst)
x[0] = lst[0]
y[0] = num-lst[0]

for i in range(1,n-1):
    x[i] = x[i-1]+lst[i]
    y[i] = num-x[i]
# print(x)
# print(y)

ans = float("INF")
for i in range(n-1):
    ans = min(ans, abs(x[i]-y[i]))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  n = int(input())
  A = readInts()
  l = 0
  r = sum(A)
  LIST = []
  for i in range(n-1):
    l += A[i]
    r -= A[i]
    LIST.append(abs(l-r))
  print(min(LIST))
  # 全部足してから、左から消していく感じ
if __name__ == '__main__':
  main()

Submission status: Accepted

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

def main(arr):
    sum_sunuke = 0
    sum_arai      = sum(arr)
    min_val = 10**10
    
#     if N == 2:
#         return abs(arr[0]- arr[1])
    
    for i in range(N-1):
        sum_sunuke += arr[i]
        sum_arai      -= arr[i]
        val = abs(sum_sunuke - sum_arai)
#         print("sunuke: {}".format(sum_sunuke))
#         print("arai: {}".format(sum_arai))
        if min_val > val:
            min_val = val
#             print("min val: {}".format(min_val))
#         print("*************")
    return min_val

print(main(a_arr))

Submission status: Accepted

Solution code:
import numpy as np

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

suml = np.cumsum(a)
sumr = np.cumsum(a[::-1])[::-1]
diff = np.abs(sumr[1:] - suml[:-1])

print(min(diff))

Submission status: Accepted

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

Submission status: Accepted

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

sub = sum(a)
s = 0
ans = (10 ** 10) + 100
for i in range(n):
    s += a[i]
    if (i + 1) < n:
        ans = min(ans, abs(sub - (2 * s)))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
max_ans = sum(a)
ans = abs(a[0]-(max_ans - a[0]))
sumu = a[0]
for i in range(1,n-1):
  sumu += a[i]
  arai = max_ans - sumu
  kari = abs(sumu-arai)
  if kari < ans:
    ans = kari
print(ans)

Submission status: Accepted

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

sum_ls = list(np.cumsum(a_ls))
sum_ls.insert(0,0)

ans = float('inf')
for i in range(1,n):
    su = sum_ls[i]
    ara = sum_ls[-1] - sum_ls[i]
    dif = abs(su-ara)
    if dif < ans:
        ans = dif

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 = float("inf")
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()))

Asum = sum(A)
ans = 10**18
cumsum = 0
for i in range(N-1):
    cumsum += A[i]
    ans = min(ans, abs(Asum - 2*cumsum))
print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

#　素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# ここから書き始める
n = int(input())
a = list(map(int, input().split()))
x = a[0]
y = sum(a[1:])
a = a[1:-1]
ans = abs(x - y)
# print("x =", x)
# print(a)
for i in range(n - 2):
    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 = []
sunuke = 0
Sum = sum(a)
for i in range(n-1):
    sunuke += a[i]
    ans.append(abs(Sum-2*sunuke))
print(min(ans))

Submission status: Accepted

Solution code:
import math
# N = 2
# ARR = [10, -10]
#
# N = 6
# ARR = [1, 2, 3, 4, 5, 6]

N = int(input())
ARR = [int(s) for s in input().split(" ")]
def calculate(n,arr):
    lSum = 0
    tSum = sum(arr)
    res = []
    for index in range(n-1):
        ar = arr[index]
        lSum = lSum + ar
        rSum = tSum - lSum
        res.append(int(math.fabs(lSum-rSum)))

    print(min(res))



calculate(N,ARR)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#4
#入力
N=int(input())

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

#処理
B=[]

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

C=[0 for i in range(N)]

for i in range(N):
    C[i]=abs(B[N-1]-2*B[i])
print(min(C[:N-1]))

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(N-1):
    S[i+1] = a[i+1]+S[i]
ans = 10000000000
for i in range(N-1):
    ans = min(ans, abs(S[N-1]-S[i]-S[i]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,(input().split())))
s = sum(a)
S = [0]*N
p = [0]*N
gap = [100000000000]*N
S[0] = a[0]
for i in range(1,N):
    S[i] = S[i-1] + a[i]
for i in range(N):
    p[i] = s - S[i]
for i in range(N-1):
    gap[i] = abs(S[i]-p[i])
print(min(gap))

Submission status: Accepted

Solution code:
n = int(input().strip())
a = list(map(int, input().split()))
s = [a[0]]
for i in range(0, (n - 1)):
    s.append(s[i] + a[i + 1])
mi = float('inf')
su = sum(a)
for i in range(n - 1):
    rep = abs(su - 2 * s[i])
    if rep < mi:
        mi = rep
print(mi)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
sm = sum(a)//2
mn = 10**10
ar = a[0]
su = a[-1]
for i in range(1,len(a)-1):
    if abs(sm - ar) < mn:
        mn = abs(sm - ar)
        mn_i = i
    ar += a[i]

if len(a) > 2:
    ar = sum(a[0:mn_i])
    su = sum(a[mn_i::])
    print(abs(ar-su))
else:
    print(abs(ar-su))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
sunuke = a[0]
arai = sum(a[1:])
result = abs(sunuke-arai)
for el in a[1:-1]:
    sunuke += el
    arai -= el
    if abs(sunuke-arai) < result:
        result = abs(sunuke-arai)
print(result)

Submission status: Accepted

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

S = A[0]
T = sum(A) - S
cnt = abs(S - T)
for i in range(1, N - 1):
    S += A[i]
    T -= A[i]
    cnt = min(cnt, abs(S - T))
print(cnt)

Submission status: Accepted

Solution code:
n, *cards = map(int, open(0).read().split())
sum_cards = sum(cards)

snuke = cards[0]
araiguma = sum_cards - cards[0]

ans = abs(snuke - araiguma)

for num in cards[1:n-1]:
    snuke += num
    araiguma -= num
    ans = min(ans, abs(snuke-araiguma))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
sum1 = sum(L)
sum2 = 0

if n == 2:
    print(abs(L[1]-L[0]))
    exit ()
ans = 10**20
for i in range(n-1):
    sum2 +=L[i]
    sa = abs(sum1 - sum2- sum2)
    ans = min(ans,sa)
print(ans)

Submission status: Accepted

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

if n==2:
    print(abs(a[1]-a[0]))
else:
    asum=sum(a)
    xsum=0
    xmin=n*(10**9)+1
    for i in range(n-1):
        xsum=xsum+a[i]
        xmin=min(xmin,abs(asum-2*xsum))
    print(xmin)

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] = s[i] + a[i]
ans = float('Inf')
for i in range(1, N):
    #print(ans)
    kouho = abs(s[i] - ((s[-1]) - s[i]))
    if ans > kouho:
        ans = kouho
    #ans = min(ans, kouho)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
s_right = [0] * (N + 1)
s_left = [0] * (N + 1)
for i in range(N):
    s_left[i + 1] = s_left[i] + a[i]
    s_right[i + 1] = s_right[i] + a[N - 1 - i]
ans = float('Inf')
for i in range(1, N):
    #print(ans)
    if ans > abs(s_left[i] - s_right[N - i]):
        ans = abs(s_left[i] - s_right[N - i])
print(ans)

Submission status: Accepted

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

sumA = sum(a)
x_y = []
x = 0

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

print(min(x_y))

Submission status: Accepted

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


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

    A_sum = sum(A)
    tmp = 0

    result = []

    for a in A:
        tmp += a
        result.append(abs(A_sum - tmp * 2))
    print(min(result[:-1]))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():

    from itertools import accumulate

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

    B = [0] + A
    B = list(accumulate(B))  # 累積和を格納したリスト作成

    minans=10**10
    # この問題は長さが1-Nの連続部分の和の最大値を出せというものなので以下の通り
    for i in range(1, N):
        snuke=B[i]
        arai=B[-1]-B[i]
        minans = min(minans,abs(snuke-arai))
    print(minans)


resolve()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

c=x-y
ans=abs(c)

for i in range(1,N-1):
  c+=2*A[i]
  if abs(c)< abs(ans):
    ans = abs(c)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
from itertools import accumulate
acc = list(accumulate(As))
s = sum(As)
mn = 10**10
for i,x in enumerate(acc[:-1]):
    y = s - x
    if mn >= abs(x-y):
        mn = abs(x-y)
print(mn)

Submission status: Accepted

Solution code:
n = int(input())
a =[int(i)  for i in input().split()]
z = sum(a)
ans = 10**20
c = 0

for i in range(n-1):
    c += a[i]
    z -= a[i]
    if abs(c-z) < ans:
        ans = abs(c-z)
    
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    cards = [int(item) for item in input().split()]

    all_sum = sum(cards)

    
    def candi_abs(sumu):
        # print(sumu, all_sum - sumu, abs(2*sumu - all_sum))
        return abs(2*sumu - all_sum)

    res = 3 * 10**9
    sumu = 0
    for i in range(N-1):
        sumu += cards[i] 
        res = min(res, candi_abs(sumu))

    print(res)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

X = sum(a)
x =[a[0]]
for i in range(n-1):
	x.append(x[i]+a[i+1])
ans = min(list(abs(X-2*x[i]) for i in range(n-1)))

print(ans)

Submission status: Accepted

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

ans = 10 ** 20

s1 = a[0]
s2 = sum(a[1:])
ans = abs(s1 - s2)
if len(a)==2:
    print(ans)
else:
    for i in range(1, N-1):
        s1 = s1 + a[i]
        s2 = s2 - a[i]
        ans = min(ans, abs(s1 - s2))

    print(ans)

Submission status: Accepted

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

accum=[0]
for i in range(N):
    accum.append(accum[i]+a[i])

accum.remove(0)
#print(accum)
S=accum[-1]
lst=[]
for i in range(N-1):
    lst.append(abs(S-accum[i]*2))
print(min(lst))

Submission status: Accepted

Solution code:
import numpy
N = int(input())
List = list(map(int,input().split()))
cumsum = numpy.cumsum(List)
total = sum(List)
result = [abs(total -item*2) for item in cumsum ]
print(min(result[:-1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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]=s[i]+A[i]
C=[0]*(N)
C[0]=-s[N]
x=10**10
p=0
for i in range(1, N):
    C[i]=C[i-1]+2*A[i-1]
    if x>abs(C[i]):
        x=abs(C[i])
        p=i
print(x)
#print(C)

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] = s[i] + a[i]

ans = 3e9
for i in range(n - 1):
    ans = min(ans, abs((s[n] - s[i + 1]) - s[i + 1]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

B=[0]*N
B[0] = A[0]
saitei = float("inf")
for i in range(N-1):
    saitei = min(abs(B[i]-(a-B[i])), saitei)
    B[i+1] += (A[i+1] + B[i])
    
print(saitei)

Submission status: Accepted

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

total = sum(a)
ans = []
for x in range(n-1):
  total -= 2*a[x]
  ans.append(abs(total))

print(min(ans))

Submission status: Accepted

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

su = 0
ar = sum(a)
minsc = 10**18

for i in range(n-1):
    su += a[i]
    ar -= a[i]
    minsc = min(minsc, abs(su - ar))

print(minsc)

Submission status: Accepted

Solution code:
n=int(input())

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

goukei=sum(t)

ans = 10000000000000

fir = 0

for i in range(n-1):
    fir += t[i]
    sec = goukei-fir
    ans = min(ans,abs(sec-fir))
print(ans)

Submission status: Accepted

Solution code:
_,*a=map(int,open(0).read().split());s,t=sum(a),0;l=[]
for i in a:t+=i;l+=[abs(s-t*2)]
print(min(l[:-1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import numpy as np
from sys import maxsize

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

cumsum = np.cumsum(a)
res = maxsize
for i in range(n-1):
    val = abs(cumsum[i] - (cumsum[-1] - cumsum[i]))
    if val < res:
        res = val

print(res)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
all=sum(a)
su=0
mi=float("inf")
for i in range(N-1):
 su=su+a[i]
 if abs(all-2*su)<mi:
  mi=abs(all-2*su)
print(mi)

Submission status: Accepted

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

x = 0
y = total
B = []
for i in range(n-1):
    x += A[i]
    y -= A[i]
    B.append(abs(x - y))
print(min(B))

Submission status: Accepted

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

Submission status: Accepted

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
x=0
k=sum(a)
m = 2*10**9

for i in range(n-1):
  x+=a[i]
  if m>abs(2*x-k):
    m=abs(2*x-k)
print(m)