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 re
import math
from collections import defaultdict
from collections import deque
import collections
from fractions import Fraction
import itertools
from copy import deepcopy
import random
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
#@lru_cache(maxsize=None)
readline=sys.stdin.readline
sys.setrecursionlimit(2000000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def ispow(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n,type="int"):
    if n==0:
        if type=="int":return []
        else:return [False]
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=""):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def pm(x):
    return x//abs(x)
def inputintlist():
    return list(map(int,input().split()))
######################################################################################################

n=int(input())
A=inputintlist()
ssum=sum(A)
ans=inf
now=0
for a in A[:-1]:
    now+=a
    x=now
    y=ssum-now
    ans=min(ans,abs(x-y))
    

print(ans)

Submission status: Accepted

Solution code:
#4問目
N = int(input())
a = list(map(int, input().split()))
#足していった配列を用意する
Sum = 0
Min = float("inf")
SumArray = [0] * N
for i in range(N):
    Sum += a[i]
    SumArray[i] = Sum
#仕切りを用意して，左側をすぬけくん，右側をアライグマのカードとして差分を計算する
if N == 2:
    print(abs(a[0] - a[1]))
else:
    for i in range(N-1):
        temp = abs(SumArray[i] - (SumArray[N-1] - SumArray[i]))
        if temp < Min:
            Min = temp

    print(int(Min))

Submission status: Accepted

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

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

Submission status: Accepted

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

sum_a = sum(A)
cum_a = [0]*n
cum_a[0] = A[0]

ans = abs(sum_a-2*cum_a[0])

for i in range(1,n-1):
    cum_a[i] = cum_a[i-1]+A[i]
    ans = min(ans,abs(sum_a-2*cum_a[i]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
li = []
e = sum(a)
for i in range(N-1):
    e -= 2*a[i]
    li.append(abs(e))
print(min(li))

Submission status: Accepted

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

cum = [0]

for a in A:
    s = a + cum[-1]
    cum.append(s)

ans = None

for s in cum[1:-1]:
    x = s
    y = cum[-1] - s
    if ans is None:
        ans = abs(x - y)
    else:
        ans = min(ans, abs(x - y))

print(ans)

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from collections import deque, defaultdict, Counter
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop, heapify
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
a = LIST()

a_sum = sum(a)
tmp = 0
ans = INF

for i in range(N-1):
	tmp += a[i]
	diff = abs(a_sum - tmp - tmp)
	ans = min(ans, diff)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
input()
A = list(accumulate([int(i) for i in input().split()]))
ans = 99 ** 9
for a in A[:-1]:
    ans = min(ans,abs(A[-1]-a-a))
print(ans)

Submission status: Accepted

Solution code:
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):
    x = cumsum[i]
    y = cumsum[n] - x
    ans = min(ans, abs(x - y))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
asum = [0]*n
for i in range(n):
  asum[i] = asum[i-1] + a[i]
total = asum[-1]
half = total/2
ans = abs(asum[0]-half)*2
for j in range(0,n-1):
  s = abs(asum[j]-half)*2
  if s < ans:
    ans = s
print(int(ans))

Submission status: Accepted

Solution code:
import collections


def main():
    n = int(input())
    a = input_list()
    sum_a = sum(a)
    wa = []
    for i, v in enumerate(a):
        if i == 0:
            wa.append(v)
        else:
            wa.append(v+wa[i-1])
    ans = float('inf')
    for i in range(n-1):
        ans = min(ans, abs(sum_a-(wa[i]*2)))
    print(ans)


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

partial_sum = [0] * N

tmp = 0
for i in range(N):
    tmp += A[i]
    partial_sum[i] = tmp

ans = abs(partial_sum[0]-(partial_sum[-1]-partial_sum[0]))

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

print(ans)

Submission status: Accepted

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

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

ans = 10e+12
for i in range(1, n):
  ans = min(ans, abs(l[i] - r[i]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import*;*a,s=list(accumulate(map(int,open(0).read().split()[1:])));print(min(abs(s-x*2)for x in a))

Submission status: Accepted

Solution code:
from itertools import*
_, *d = map(int, open(0).read().split())
*a, s = list(accumulate(d))
print(min(abs(s - x*2) for x in a))

Submission status: Accepted

Solution code:
def myAnswer(N:int,A:list) -> int:
   accum = [A[0]]
   total = sum(A)
   ans = []
   for i in range(1,N):
      accum.append(accum[i-1] + A[i])
   accum.pop()
   # print(accum)
   for x in accum:
      y = total - x
      ans.append(abs(x - y))
      # print(ans)

   return min(ans)

def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A))
if __name__ == '__main__':
   main()

Submission status: Accepted

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

Submission status: Accepted

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


if n == 2:
    print(abs(a[0]-a[1]))
else:
    goukei = sum(a)
    x = [0]*n
    x[0] = a[0]
    ans = 10**11
    for i in range(1, n-1):
        x[i] = x[i-1] + a[i]
        y = goukei - x[i]
        ans = min(ans, abs(x[i]-y))
    print(ans)

Submission status: Accepted

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

all_sum = sum(li)
x = 0
y = all_sum
ans = 10**18

for i in range(n):
    x += li[i]
    y -= li[i]

    if ans >= abs(x-y):
        ans = abs(x-y)

    if i == n - 2:
        break

print(ans)

Submission status: Accepted

Solution code:
def resolve():
	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):
		ans = min(ans, abs(s[-1] - s[i]*2))
	print(ans)
resolve()

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(abs(x-y),ans)
 
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
ans = pow(10, 15)
key = sum(a)
p = 0
for i in range(n-1):
  p += a[i]
  if ans > abs(key-2*p):
    ans = abs(key-2*p)
    
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    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=INF
    for i in range(1,N):
        tmp=abs(S[-1]-2*S[i])
        ans=min(ans,tmp)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May 14 12:49:47 2020

@author: shinba
"""
import itertools

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

b = list(itertools.accumulate(a))

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

Submission status: Accepted

Solution code:
import itertools
n = int(input())
a = list(itertools.accumulate(list(map(int, input().split()))))
ans = 10000000000000
sum_a = a[-1]
for i in range(n-1):
  ans = min(ans,abs(sum_a - 2 * a[i]))
print(ans)

Submission status: Accepted

Solution code:

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

n = int(input())
a = list(accumulate(map(int, sys.stdin.readline().split())))
sa = a.pop()
print(min(abs(sa - 2*i) for i in a))

Submission status: Accepted

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

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

ans = float("inf")
for i in range(1, N):
    snuke = S[i] - S[0]
    kuma = S[N] - S[i]
    score = abs(snuke - kuma)
    ans = min(ans, score)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
from itertools import accumulate

A = list(accumulate(A))
ans = float('inf')
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()))

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:
n=int(input())
a = list(map(int,input().split()))
su = a[0]
ar = sum(a[1:])
p=abs(su-ar)
for i in a[1:-1]:
    su += i
    ar -= i
    p=min(abs(su-ar),p)
print(p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy

# import heapq
# from collections import deque
# import decimal

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===

def main():
    n = ni()
    a = na()

    total = sum(a)

    sunuke = 0
    ans = sys.maxsize
    for i in range(n - 1):
        sunuke += a[i]
        ans = min(ans, abs(total - sunuke*2))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
lisA = list(map(int,input().split()))
arrA = np.array(lisA)

cumsumA = np.cumsum(arrA)

ans = 10**9 * 2 * 10**5
for num in cumsumA[:-1]:
    ans = min(ans,abs(num*2 - cumsumA[-1]))

print(ans)

Submission status: Accepted

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

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
import numpy as np
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    A = np.array(readline().split(), np.int64)
    total = A.sum()
    X = A[:-1].cumsum()
    Y = total - X
    ans = np.abs(X-Y).min()
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = tuple(map(int, input().split()))
s = 0
b = sum(A)
ans = float("inf")
for i, a in enumerate(A):
    s += a
    b -= a
    if i == N - 1:
        break
    ans = min(ans, abs(s - b))
print(ans)

Submission status: Accepted

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

if n == 2:
    ans = abs(a[0]-a[1])
else:
    ans = 10**100
for i in range(1,n-1):
    if ans > abs(sum-2*sa[i]):
        ans = abs(sum-2*sa[i])

print(ans)

Submission status: Accepted

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

for i in range(N):
    s += a[i]
    csum.append(s)

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

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

print(ans)

Submission status: Accepted

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

ans = 10**10
for i in range(0, N-1):
    sunuke += A[i]
    arai -= A[i]
    ans = min(ans, abs(sunuke - arai))

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
A = list(map(int,input().split()))
sumA = list(accumulate(A))
maxA = sumA[-1]
ans = 10**18

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
n = input()
m = list(map(int, input().split()))
s = sum(m)
f = lambda x: abs(x-(s-x))
ans = f(m[0])
for i in list(itertools.accumulate(m))[1:-2]:
    ans = min(ans, f(i))
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    S = [0 for i in range(N+1)]
    for i in range(1, N+1):
        S[i] = S[i-1] + A[i-1]

    mi = 10**10
    for i in range(1, N):
        S1 = S[i] - S[0]
        S2 = S[N] - S[i]
        d = abs(S1-S2)
        
        if d < mi:
            mi = d
            
    print(mi)
        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

n = int(input())
a = list(map(int,input().split()))
d = sum(a)
snuke = 0
arai = 0
ans = 10000000000
for i in range(n-1):
    snuke += a[i]
    arai = d - snuke
    ans = min(ans,abs(snuke-arai))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum_a=0
slist=[]
for a in alist:
  sum_a+=a
  slist.append(sum_a)
#print(slist)

allsum=slist[-1]
answer=float("inf")
for i in range(N-1):
  score_a=slist[i]
  score_b=allsum-score_a
  
  cand=abs(score_a-score_b)
  #print(score_a,score_b,cand)
  answer=min(answer,cand)
  
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
sum_A = sum(A)
ans = 10**20
X = 0
for a in A[:-1]:
    X += a
    ans = min(ans,abs(sum_A-2*X))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = [0]*n
sum = 0

for i in range(n):
    sum+=a[i]
    s[i]=sum
min = 2000000000
for i in range(n-1):
    d=abs(s[i]*2-s[n-1])
    if d < min:
        min =d
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
forward = []
backward = []
sumA = 0
for i in range(N):
    forward.append(A[i]+sumA)
    sumA += A[i]
sumA = 0
for j in range(N):
    backward.append(A[N-j-1]+sumA)
    sumA += A[N-j-1]
sa = []
for i in range(N-1):
    sa.append(abs(forward[i]-backward[N-i-2]))

print(min(sa))

Submission status: Accepted

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

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

Submission status: Accepted

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

res = 10 ** 15
cnt = 0
s = sum(A)
for a in A[:-1]:
    cnt += a
    res = min(res, abs(s - 2 * cnt))

print(res)

Submission status: Accepted

Solution code:
def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	m = sum(a)
	cnt = 0
	ans = float('inf')
	for i in range(len(a) - 1):
		cnt = cnt + a[i]
		ans = min(ans, abs(m - cnt*2))
	print(ans)
resolve()

Submission status: Accepted

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

sleft = []
sright = [0]*n
left = 0
right = 0
for i in range(n):
    left += a[i]
    sleft.append(left)
for i in range(n-1,-1,-1):
    right += a[i]
    sright[i] = right
ans = float('inf')
for i in range(1,n):
    ans = min(ans, abs(sright[i] - sleft[i-1]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = int(1e20)
for i in range(N-1):
    ans = min(ans, abs(sa[i] - (sa[-1] - sa[i])))
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

cum_sum = [0] * (n+1)
for i in range(n):
    cum_sum[i+1] = cum_sum[i] + a[i]

INF = 10**18
ans = INF
for i in range(1, n):
    ans = min(ans, abs(cum_sum[i]-(cum_sum[-1]-cum_sum[i])))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = [0]
for i in range(N):
    S.append(S[i] + A[i])

ans = float('inf')
for i in range(1,N):
    sunke = S[i] - S[0]
    araiguma = S[-1] - S[i]
    ans = min(ans, abs(sunke - araiguma))

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
cum_sum = [0]*n

total = 0
for i in range(n):
    total += a[i]
    cum_sum[i] = total

max_sum = cum_sum[-1]

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

print(ans)

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]
INF = 10**18
ans = INF
for R in range(1, n):
    ans = min(ans, abs((S[R]-S[0])-(S[n]-S[R])))
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
an = list(map(int, input().split()))
res = [0] + an
res = list(accumulate(res))
ans = 10e+9
for i in range(1, N):
    ans = min(ans, abs(res[i] - (res[-1]-res[i])))
print(ans)

Submission status: Accepted

Solution code:
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]
    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()))
r = 0
l = sum(A)
ans = 10**20
for a in A[:-1]:
    r += a
    l -= a
    if ans > abs(r-l):
        ans = abs(r-l)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
S=sum(a)
SS=0
mi=1000000000000000
for i in range(N-1):
    S=S-a[i]
    SS=SS+a[i]
    if mi>=abs(S-SS):
        mi=abs(S-SS)
print(mi)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
S = sum(a)
sum =[0]*n
sum[0] = a[0]
su =0
ar =0

min = 10**20

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

print(min)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

s = [0]*(n-1)
s[0] = a[0]
A = sum(a)
ans = 1e100
for i in range(n-2):
  s[i+1] = s[i] + a[i+1]

for t in s:
  ans = min(ans, abs(2*t - A))
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

import numpy as np
a = list(np.cumsum(a))

ans = 100000000000000

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

print(ans)

Submission status: Accepted

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

t = sum (a)
ans = 10 ** 10 + 1
X = 0
for i, x in enumerate(a):
    X += x
    Y = t - X
    ans = min(ans, abs(X-Y))
    if i+ 1 == n -1:
        break

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = list(map(int, input().split()))
total = sum(a)
Min = np.inf
s = 0
for i in range(len(a)-1):
    s += a[i]
    Min = min(Min, abs(2 * s- total))
print(Min)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    minabs = float("inf")
    tmpx = 0
    tmpy = sum(A)
    for i in range(N-1):
        tmpx += A[i]
        tmpy -= A[i]
        if abs(tmpx - tmpy) < minabs:
            minabs = abs(tmpx - tmpy)
    print(minabs)

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
a = li[0]
s = sum(li)
b = s - a
ans_li = [abs(a-b)]
#print(ans_li)

for i,l in enumerate(li):
    if i == 0:
        continue
    if i == n-1:
        continue
    a+=l
    b-=l
    ans_li.append(abs(a-b))
print(min(ans_li))

Submission status: Accepted

Solution code:

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

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

A = np.asarray(A)
A_cumsum = A.cumsum()
total = A.sum()
diff = np.abs(2*A_cumsum - total)
ans = min(diff[:-1])
print(ans)

Submission status: Accepted

Solution code:
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 = float("inf")
for i in range(1, N):
    cand = abs(cumsum[i] - (cumsum[-1] - cumsum[i]))
    ans = min(ans, cand)

print(ans)

Submission status: Accepted

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

j =a[0] 
k =0
for i in range(1,N):
  k += a[i]
  
ans =abs(j-k)
  
for x in range(1,N-1):
  j += a[x]
  k -= a[x]
  ans = min(ans,abs(j-k))
  
  
print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    A = list(map(int, readline().rstrip().split()))
    x = 0
    y = sum(A)
    ans = 10 ** 10
    for i in range(N-1):
        x += A[i]
        y -= A[i]
        ans = min(ans, abs(x-y))
    
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = [None] * (N-1)
ans[0] = sum(a[1:]) - a[0]
mina = abs(ans[0])

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
t=sum(a)
mi=abs(t-2*a[0])
k=a[0]

for i in range(1,n-1):
    k=k+a[i]
    if abs(t-2*k) < mi:
        mi=abs(t-2*k)
print(mi)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 10000000000000
x = 0
y = sum(li)

for i in range(0,n-1):
    x += li[i]
    y -= li[i]
    z = abs(x-y)
    if z < ans:
        ans = z

print(ans)

Submission status: Accepted

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