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:
# coding:utf-8

import sys

input = sys.stdin.readline
INF = float('inf')


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


N = int(input())
A = inpl()
B = [0]
for a in A:
    B.append(B[-1] + a)
del B[0]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]
    aDiff  = [0]*(iN-1)
    iD = sum(aA)
    iF = 0
    for i in range(iN-1):
        iF += aA[i]
        aDiff[i] = abs(iD - 2*iF)
    print(min(aDiff))
解()

Submission status: Accepted

Solution code:
def 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]

    iD = sum(aA)
    iF = 0
    minDiff = 10**9 * 2
    for a in aA[:-1] :
        iF += a
        thisDiff = abs(iD - 2*iF)
        minDiff = min(minDiff , thisDiff)
    print(minDiff)
解()

Submission status: Accepted

Solution code:

def 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]
    #itertools をつかわず 累積和でやってみると？
    aAc = [0]*iN
    aAc[0] = aA[0]
    for i in range(1,iN):
        aAc[i] = aAc[i-1] + aA[i]
    iD = aAc[-1]
    minDiff = 10**9 *2
    for i in range(iN-1):
        minDiff = min(minDiff,abs(iD-2*aAc[i]))
    print(minDiff)
解()

Submission status: Accepted

Solution code:

def 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]

    iD = sum(aA)
    iF = 0
    iB = iD
    minDiff =  10**9 * 2
    for a in aA[:-1] :
        iF += a
        iB -= a
        thisDiff = abs(iF - iB)
        minDiff = min(minDiff , thisDiff)
    print(minDiff)
解()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
tmp = 10 ** 12
after = sum(L)
before = 0
for i in range(N-1):
    before += L[i]
    after -= L[i]
    if abs(before -after) < tmp:
        tmp = abs(before -after)
print(tmp)

Submission status: Accepted

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

x, y = 0, sum(a)
m = 1e14

for i in range(n-1):
    x += a[i] 
    y -= a[i]
    if abs(x-y) < m:
        m = abs(x-y)

print(m)

Submission status: Accepted

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

X = sum(a)
x = 0
ans = 10**100

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 -*-
N = int(input())
a = list(map(int, input().split()))

sunuke_l = []
sunuke = 0
arai_l = []
arai = 0
all_sum = sum(a)
diff_l = []
for i in range(N-1):
    sunuke = sunuke + a[i]
    arai = all_sum - sunuke
    diff_l.append(abs(sunuke - arai))

print(min(diff_l))

Submission status: Accepted

Solution code:
n, aa = input(), list(map(int, input().split()))
s = sum(aa)
m = s / 2
i = 0
min = float("inf")
ans = 0
aa.pop()
for a in aa:
    s -= a
    i += a  
    dif = abs(s - m)
    if dif < min:
        min = dif
        ans = abs(s-i)
print(ans)

Submission status: Accepted

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

x = 0
y = 0
min = 1e14
for i in range(n):
    y += a[i]
for i in range(n-1):
    x += a[i]
    y -= a[i]
    if abs(x-y) < min:
        min = abs(x-y)
print(min)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    l = list(map(int, input().split()))
    ans = int(2e+9)
    X = sum(l)
    x = 0
    for i in range(N-1):
        x += l[i]
        ans = min(ans, abs(X-2*x))
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    l = list(map(int, input().split()))
    ans = []
    X = sum(l)
    x = 0
    for i in range(N-1):
        x += l[i]
        ans.append(abs(X-2*x))
    print(min(ans))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    l = list(map(int, input().split()))
    ans = []
    sunuke = 0
    arai = sum(l)
    for i in range(N-1):
        sunuke += l[i]
        arai   -= l[i]
        ans.append(abs(sunuke-arai))
    print(min(ans))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

sl = sum(a[:1])
sr = sum(a[1:])

ans = int(1e9)
for i in range(1, N-1):
    sl += a[i]
    sr -= a[i]
    ans = min(ans, abs(sl - sr))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Sunuke = A[0]
Rac = sum(A[1:])
minPoint = abs(Sunuke-Rac)
for i in range(1, N-1):
    Sunuke += A[i]
    Rac -= A[i]
    minPoint = min(minPoint, abs(Sunuke-Rac))
print(minPoint)

Submission status: Accepted

Solution code:
n = int(input())
listA= list(map(int, input().split()))
listSum=[]

sum =0
index=0
while index < len(listA):
    if index==0:
        sum+=listA[index]
        listSum.append(sum)
    else:
        sum = listSum[index-1]+listA[index]
        listSum.append(sum)
    index+=1

allSum = listSum[len(listSum)-1]
index2=0
minDelta =2*(10**9)
while index2 < len(listSum)-1:
    if max(allSum - listSum[index2], listSum[index2]) - min(allSum - listSum[index2], listSum[index2]) < minDelta:
        minDelta = max(allSum - listSum[index2], listSum[index2]) - min(allSum - listSum[index2], listSum[index2]) 
    index2+=1

print(minDelta)

Submission status: Accepted

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

Submission status: Accepted

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

del_A = [0]

for i in range(N):
    del_A.append(del_A[i] + A[i])

ans = 10**10

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

    tmp = abs(x-y)

    ans = min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
src = list(map(int,input().split()))
cums = [0]
for a in src:
    cums.append(cums[-1] + a)

ans = float('inf')
for i in range(1,N):
    l = cums[i]
    r = cums[-1] - cums[i]
    ans = min(ans, abs(l-r))
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 22 16:00:08 2018
ABC067C
@author: maezawa
"""
n = int(input())
a = list(map(int, input().split()))

total = sum(a)
cum = a[0]
dif = abs(total - 2*cum)

for i in range(1,n-1):
    cum += a[i]
    if abs(total-2*cum) < dif:
        dif = abs(total-2*cum)
        
print(dif)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A_sum = sum(A)
deltas = [A_sum]
res = []
for i in range(N-1):
    deltas.append(deltas[-1] - 2*A[i])
    res.append(abs(deltas[-1]))
print(min(res))

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]
mini = 10000000000
for i in range(N - 1):
    if abs(a[-1] - a[i] * 2) < mini:
        mini = abs(a[-1] - a[i] * 2)
print(mini)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a_sum = sum(a_list)
min_num = float("inf")
r = []

for i in range(1, N):
    if len(r) == 0:
        r.append(a_list[0])
    else:
        r.append(r[i-2] + a_list[i-1])

    x = r[i-1]
    y = a_sum - x
    if min_num >= abs(x-y):
        min_num = abs(x-y)
print(min_num)

Submission status: Accepted

Solution code:
#ABC067C
N=int(input())
a=list(map(int,input().split()))
x=a[0]
y=sum(a)-x
ans=abs(x-y)
for i in range(N-2):
    x+=a[i+1]
    y-=a[i+1]
    if ans>abs(x-y):
        ans=abs(x-y)
print(ans)

Submission status: Accepted

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

sunuke = 0
araiguma = sum(a)
for i in range(N-1):
  sunuke += a[i]
  araiguma -= a[i]
  if(i==0):
     result = abs(sunuke-araiguma)
  else:
     result = min(result, abs(sunuke-araiguma))

print(result)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(ai) for ai in input().split()]
a_sum = sum(a)
best = 10**12
tmp = 0
for ai in a[:-1]:
    tmp += ai
    best = min(best, abs(tmp - (a_sum - tmp)))
print(best)

Submission status: Accepted

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

Submission status: Accepted

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

ans = float("inf")
cum_sum = 0

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
b=[]
sum=0
for a in A:
    sum+=a
    b.append(sum)

ans=float("inf")
for i in range(0,N-1):
    if ans>abs(b[-1]-b[i]*2):
        ans=abs(b[-1]-b[i]*2)
print(ans)

Submission status: Accepted

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

S = sum(A)
ans = 1e12
work = 0
for i in range(N-1):
  work += A[i]
  ans = min(ans, abs(S - 2*work))

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
a_sum=sum(a)
ans_list=[]
sunuke=a[0]
araiguma=a_sum-a[0]
for i in range (1,N):
    ans_list.append(abs(sunuke-araiguma))
    sunuke+=a[i]
    araiguma-=a[i]
print(min(ans_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:
from itertools import accumulate
N = int( input())
A = list( map( int, input().split()))
AA = list(accumulate(A))
ANS = abs( AA[0]*2 - AA[-1])
for i in range(N-1):
    K = abs( AA[i]*2 - AA[-1])
    ANS = min( K, ANS)
print(ANS)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


# --------------------------------------------

dp = None


def main():
    N = int(input())
    A = li_input()
    C = []

    tmpsum = 0
    for a in A:
        tmpsum += a
        C.append(tmpsum)

    ans = 10**100

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

    print(ans)


main()

Submission status: Accepted

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

S = sum(A)
count = 0
Near = 10 ** 15
for i in range(N-1):
    count = count + A[i]
    if abs(2 * Near - S) > abs(2 * count - S):
        Near = count
print(abs(2 * Near - S))

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]

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

print(res)

Submission status: Accepted

Solution code:
from itertools import accumulate as acc
input()
*s,S=list(acc(map(int,input().split())))
print(min(abs(S-2*x)for x in s))

Submission status: Accepted

Solution code:
from itertools import accumulate as acc
input()
s=list(acc(map(int,input().split())));S=s.pop()
print(min(abs(S-2*x)for x in s))

Submission status: Accepted

Solution code:
from itertools import accumulate as acc
input()
S=tuple(acc(map(int,input().split())))
print(min(abs(S[-1]-2*x)for x in S[:-1]))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
x = []
y = []

s = 0
for i in a:
    s += i
    x.append(s)
s = 0
for i in a[::-1]:
    s += i
    y.append(s)
x = x[:-1]
y = y[:-1][::-1]

res = float('inf')
for (i,j) in zip(x,y):
    res = min(res, abs(i-j))
print(res)

Submission status: Accepted

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

mindiff = 10 ** 9 * 2 * 10 ** 5 + 1
s1 = 0
s2 = sum(a)
for i in range(N-1):
    s1 += a[i]
    s2 -= a[i]
    mindiff = min(mindiff, abs(s1 - s2))
    
print(mindiff)

Submission status: Accepted

Solution code:
a = int(input())

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

for i in range(1,a):
  aa[i] += aa[i-1]
  
su = 10**20

for i in range(a-1):
  su = min(su,abs(aa[a-1]-2*aa[i]))
print(su)

Submission status: Accepted

Solution code:
N, = map(int, raw_input().split())
a = map(int, raw_input().split())
sl = [0]*N
sl[0] = a[0]
for i in range(N-1):
  sl[i+1] = sl[i]+a[i+1]
sr = [0]*N
sr[N-1] = a[N-1]
for i in range(N-1,0,-1):
  sr[i-1] = sr[i]+a[i-1]
r = abs(sl[0]-sr[1])
for i in range(1, N-1):
  r = min(r, abs(sl[i]-sr[i+1]))
print r

Submission status: Accepted

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

total = sum(A)

ans = float('inf')
nusuke = 0
for a in A[:-1]:
  nusuke += a
  ans = min(ans, abs(nusuke - (total - nusuke)))

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

sum = 0
for a_i in a:
    sum+=a_i

min = abs(a[0]-(sum-a[0]))
x=a[0]
for a_i in a[1:-1]:
    x +=a_i
    if (min>abs(x-(sum-x))):
        min=abs(x-(sum-x))

print(min)

Submission status: Accepted

Solution code:
# vim: set fileencoding=utf-8 :
n = int(raw_input())
a = map(int, raw_input().split())

diff = None
min_diff = 999999999999999999
x = 0
y = sum(a)

for i in range(n - 1):
    x += a[i]
    y -= a[i]
    diff = abs(x - y)
    if diff < min_diff:
        min_diff = diff

print(min_diff)

Submission status: Accepted

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

S = sum(A)
x = 0
ans = float('inf')
for i in range(N):
    x += A[i]
    if i < N - 1:
        ans = min(ans, abs(S - x * 2))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a_sum = sum(a)
x, ans = 0, 10**20

for i, v in enumerate(a):
    x += v
    if i < n-1:
        ans = min(ans, abs(a_sum-2*x))

print(ans)
exit(0)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(v) for v in 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]
    diff = abs(x - y)
    ans = min(ans, diff)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(v) for v in 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]
    diff = abs(x - y)
    if diff < ans:
        ans = diff

print(ans)

Submission status: Accepted

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

sum_a = sum(a)

ai_sum = 0
for i in range(0, n-1):
    ai_sum += a[i]
    if i == 0:
        min_gap = abs(ai_sum * 2 - sum_a)
    if abs(ai_sum * 2 - sum_a) <= min_gap:
        min_gap = abs(ai_sum * 2 - sum_a)

print(min_gap)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
sum_a = sum(a)
x = 0
ans = abs(sum_a - 2 * a[0])
for ai in a[:-1]:
    x += ai
    ans = min(ans, abs(sum_a - 2 * x))
print(ans)

Submission status: Accepted

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))

    cumsum = []
    for i in range(n):
        if len(cumsum) == 0:
            cumsum.append(a_list[i])
        else:
            cumsum.append(cumsum[i - 1] + a_list[i])
    cumsum_reverce = []
    a_list.reverse()
    for i in range(n):
        if len(cumsum_reverce) == 0:
            cumsum_reverce.append(a_list[i])
        else:
            cumsum_reverce.append(cumsum_reverce[i - 1] + a_list[i])
    cumsum_reverce.reverse()

    ans = []
    for i in range(1, n):
        ans.append(abs(cumsum_reverce[i] - cumsum[i - 1]))
    print(min(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

x, y = 0, sum(a)
min_num = 10000000000000
for a_i in a[:-1]:
	x += a_i
	y -= a_i
	tmp = abs(x-y)
	if tmp < min_num:
		min_num = tmp
print(min_num)

Submission status: Accepted

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

ans = float("inf")
for i in range(n - 1):
    ans = min(ans, abs(pre[i] - suf[n - i - 2]))

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate as ac
n=int(input())
l=list(ac(map(int,input().split())))
a=l[-1]
print(min(abs(a-2*i) for i in l[:-1]))

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

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

N = int(input())
aa = inpl()

S1 = 0
S2 = sum(aa)
ans = INF

for i,a in enumerate(aa):
	if i == N-1:
		break
	S1 += a
	S2 -= a
	ans = min(abs(S1-S2),ans)

print(ans)

Submission status: Accepted

Solution code:
def inpl(): return input().split()

n = int(input())
a = list(map(int, inpl()))
sa = sum(a)
b = [a[0]]
for i in range(1, n - 1):
    b.append(a[i] + b[i - 1])

ans = float('inf')
for x in b:
    ans = min(ans, abs(sa - 2 * x))
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = [int(i) for i in input().split()]
cumsum = np.cumsum(a)
ans = []
for i in range(n-1):
    ans.append(abs(-cumsum[-1]+2*cumsum[i]))
print(min(ans))

Submission status: Accepted

Solution code:
import numpy
n = int(input())
a = [int(i) for i in input().split()]
a_sum = numpy.cumsum(a)
ans = []
for i in range(n-1):
    ans.append(abs(-a_sum[-1]+2*a_sum[i]))
print(min(ans))

Submission status: Accepted

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

tot = sum(A)
half = tot / 2
res = 0
s, t = A[0], sum(A[1:])
min_v = abs(s - t)

for a in A[1:-1]:

    s += a
    tmp = abs(tot - 2 * s)
    if tmp < min_v:
        min_v = tmp

print(min_v)

Submission status: Accepted

Solution code:
def getinputdata():

    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = True

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = True

            else:

                flg = False
    finally:


        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])
list01 = [int(x) for x in arr_data[1]]

sumdata = sum(list01)

middledata = sumdata // 2
#初期値
x = list01[0]
y = sumdata-x
temp=abs(x-y)

for i in range(1, n-1):

    x += list01[i]
    y = sumdata-x
    
    if temp>abs(x-y):
        
        temp=abs(x-y)
        
        
print(temp)

Submission status: Accepted

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

Submission status: Accepted

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

a_cum = []
cumsum = 0

# cumsum array
for i in a :
    cumsum += i
    a_cum.append(cumsum)

min_abs = 2*(10**9)

for i in range(0, len(a_cum) - 1) :
    x = a_cum[i]
    y = a_cum[len(a_cum) - 1] - x
    
    min_abs = min(abs(x - y), min_abs)
    
print(min_abs)

Submission status: Accepted

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

snuke = a[0]
arai = sum(a) - snuke
ans = abs(arai-snuke)

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

Submission status: Accepted

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

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

# 初期値設定
sunuke = aN[0]
arai = sum(aN) - aN[0]
ans = abs(sunuke - arai)

for i in range(1, N-1):
    sunuke += aN[i]
    arai -= aN[i]
    ans = min(ans, abs(sunuke - arai))
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

b = sum(a)

c=a[0]
d=b-c
mi=abs(c-d)

for i in range(1,n-1):
    c+=a[i]
    d-=a[i]
    if abs(c-d)<mi:
        mi=abs(c-d) 

print(mi)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
cumSum = [0]
for i in range(N):
    cumSum.append(cumSum[i]+a[i])
def csum(l,r):
    return(cumSum[r]-cumSum[l-1])
def value(i):
    return(abs(csum(1,i)-csum(i+1,N)))
ans = value(1)
for i in range(1,N):
    ans = min(ans,value(i))
print(ans)

Submission status: Accepted

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

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

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

ans = 10**100
for i in range(n-1):
    ans = min(ans, abs(_sum[i] - (_sum[-1] - _sum[i])))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
s = sum(a)
t = 0
Min = float("inf")
for i in range(N - 1):
    t += a[i]
    s -= a[i]
    Min = min(Min, abs(s - t))

print(Min)

Submission status: Accepted

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

summ = sum(A)
scores = []
i = 0
partsum = 0
while i <= N-2:
  partsum += A[i]
  scores.append(abs(summ-2*partsum))
  i += 1
print(min(scores))

Submission status: Accepted

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

sum_A = sum(A)
temp = 2 * A[0] - sum_A
ans = abs(temp)

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

print(ans)

Submission status: Accepted

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

snuke = a[0]
total = sum(a)
arai = total - snuke

minimum = abs(snuke- arai)

for i in range(1,N-1):
    snuke = snuke + a[i]
    arai = arai - a[i]
    sub = abs(snuke-arai)
    if sub < minimum:
        minimum = sub

print(minimum)

Submission status: Accepted

Solution code:
import numpy

n = int(input())

arr = map(int,raw_input().split())

sumarr = numpy.cumsum(arr)
l = len(sumarr)
ans = float("inf")
for i in range(n-1):
	ans = min(ans,abs(sumarr[l-1]-2*sumarr[i]))

print ans

Submission status: Accepted

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

index = 1
ruiseki_list = [0]*(N+1)
result = 10*9

if N == 2:
    print(abs(input_list[0]-input_list[1]))
else:
    for i in input_list:
        ruiseki_list[index] = ruiseki_list[index-1] + i
        index += 1

    mapped_list = map(lambda x: ruiseki_list[N]-x, ruiseki_list)
    result_list = [abs(a-b) for (a, b) in zip(ruiseki_list, mapped_list)]
    print(min(result_list[2:N]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate as ac
N = int(input())
src = list(map(int,input().split()))
acum = list(ac(src))
ans = float('inf')
for i in range(N-1):
    ans = min(ans, abs(acum[i] - (acum[-1]-acum[i])))
print(ans)

Submission status: Accepted

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

up = 0
down = sum(a)
result = abs(a[0]-(sum(a)-a[0]))

for i in a[:-1]:
    up += i
    down -= i
    if result > abs(up-down):
        result = abs(up-down)

print(result)

Submission status: Accepted

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

top = 0
under = sum(A)
result = abs((A[0])-(sum(A)-A[0]))
for a in A[:-1]:
    top += a
    under -= a
    if result > abs(top - under):
        result = abs(top-under)
print(result)

Submission status: Accepted

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

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

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

diff = float('inf')
for i in range(1,n):
    diff = min(diff, abs(sum_list[-1]-2*sum_list[i]))

print(diff)

Submission status: Accepted

Solution code:
N = int(input())

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

rui = [0] * N

rui[0] = A[0]
for it in range(1, N):
    rui[it] = rui[it - 1] + A[it]

ans = 1145141919810
for i in range(0, N - 1):
    ans = min(abs(rui[i] - rui[N - 1] + rui[i]), ans)

print(ans)

Submission status: Accepted

Solution code:
def m():
    n = int(input())
    a = list(map(int, input().split()))
    b = a.copy()

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

    ans = 999999999999
    for j in range(n-1):
        ans = min(ans, abs(b[j] - a[j+1]))
    return ans
print(m())

Submission status: Accepted

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