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: Wrong Answer

Solution code:
def spaceinput():
    return list(map(int,input().split(" ")))

def f(c):
    return abs(sum(aa[:c])-sum(aa[c:]))

N=int(input())
aa=spaceinput()

l=0
r=len(aa)-1
vl=f(l+1)
vr=f(r)
while True:
    c=int((l+r)/2)

    if c==0:
        c+=1

    vc=f(c)
    if vl >vc:
        l=c
        vl=vc
    elif vr >vc:
        r=c
        vr=vc

    else:
        break

print(f(c))

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
a=map(int,raw_input().split())
top=N-1
bottom=1
res=10**10
while top>=bottom:
    mid=(top+bottom)/2
    d=sum(a[:mid])-sum(a[mid:])
    if abs(d)<=res:
        res=abs(d)
    if d<0:
        bottom=mid+1
    if d>0:
        top=mid-1
    if d==0:
        break
    #print top,bottom,d
print res

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
a=map(int,raw_input().split())
p=N/2
res=10**10
last=p
las=p
while 1<=p<=N-1:
    d=sum(a[:p])-sum(a[p:])
    if abs(d)<=res:
        res=abs(d)
    if d<0:
        p+=1
    if d>0:
        p-=1
    if d==0 or p==last:
        break
    last=las
    las=p
print res

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
a=map(int,raw_input().split())
top=N-1
bottom=1
res=10**20
while top>=bottom:
    mid=(top+bottom)/2
    d=sum(a[:mid])-sum(a[mid:])
    if abs(d)<=res:
        res=abs(d)
    if d<0:
        bottom=mid+1
    if d>0:
        top=mid-1
    if d==0:
        break
print res

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
a=map(int,raw_input().split())
top=N-1
bottom=1
res=10**10
while top>=bottom:
    mid=(top+bottom)/2
    d=sum(a[:mid])-sum(a[mid:])
    if abs(d)<=res:
        res=abs(d)
    if d<0:
        bottom=mid+1
    if d>0:
        top=mid-1
    if d==0:
        break
print res

Submission status: Wrong Answer

Solution code:
import sys

# sys.stdin = open('c1.in')


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


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def solve():
    n = read_int()
    a = sorted(read_int_list())
    res = 2 * 10 ** 10
    for i in range(n - 1):
        d = a[i + 1] - a[i]
        if res > d:
            res = d
    return res


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = input()
A = map(int, raw_input().split())
B = [0]*(n+1)
for i in xrange(n):
    B[i+1] = B[i] + A[i]
res = 10**9+7
for i in xrange(1, n):
    res = min(res, abs(B[n] - B[i]*2))
print res

Submission status: Time Limit Exceeded

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

dp = [float("inf")]*(n+1)

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

print(dp[n])

Submission status: Time Limit Exceeded

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

S = sum(a)
arr = [abs(S - 2*sum(a[:i]))   for i in range(1,N)]

print(min(arr))

Submission status: Time Limit Exceeded

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

arr = [abs(sum(a[:i]) - sum(a[i:]))  for i in range(1,N)]

print(min(arr))

Submission status: Time Limit Exceeded

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

min = 1000000000000000000000
for i in range(1,N):
  tmp = abs(sum(a[:i]) - sum(a[i:])) 
  if min > tmp:
    min = tmp

print(min)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/2/
Solved on 2019/2/
@author: shinjisu
"""


# ARC 078 C  Splitting Pile
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]


def getIntMat(n):
    mat = []
    for i in range(n):
        mat.append(getIntList())
    return mat


def zeros2(n, m): return [zeros(m)]*n


ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7


def dmp(x, cmt=''):
    global debug
    if debug:
        if cmt != '':
            print(cmt, ':  ', end='')
        print(x)
    return x


def probC():
    N = getInt()
    A = getIntList()
    dmp((N, A))
    diff = 10**(9+6)
    for i in range(1, N):
        diff = min(diff, abs(sum(A[:i])-sum(A[i:])))
    return diff


debug = False  # True False
ans = probC()
print(ans)
#for row in ans:
#    print(row)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
b = sum(ar)
ma =[]
for i in range(1,a):
    ma.append(abs(b - sum(ar[0:i]) * 2))
print(min(ma))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = True

    try:

        while flg:

            data = input()

            array_temp = []

            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 = list(map(int, arr_data[1]))

mindata = abs(sum(list01[:0 +1]) - sum(list01[0 + 1:]))

sumdata=sum(list01)

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

     if mindata > abs(sum(list01[:i + 1]) -  (sumdata - sum(list01[:i + 1]) )):
    
        mindata = abs(sum(list01[:i + 1]) -  (sumdata - sum(list01[:i + 1]) ))
       
       
print(mindata)

Submission status: Time Limit Exceeded

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = True

    try:

        while flg:

            data = input()

            array_temp = []

            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 = list(map(int, arr_data[1]))

mindata = abs(sum(list01[:0 +1]) - sum(list01[0 + 1:]))

for i in range(0, n-1):
    
#    print(list01[:i + 1], list01[i + 1:])


    if mindata > abs(sum(list01[:i + 1]) - sum(list01[i + 1:])):
        
        mindata = abs(sum(list01[:i + 1]) - sum(list01[i + 1:]))
       
       
print(mindata)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

j = list(map(int, input().split()))
k = []
for i in range(1, len(j) + 1):
    k.append(sum(j[0:i]))
mi = 9999999999
for m in range(len(k)-1):
    mi = min(abs(k[n - 1] - 2 * k[m]), mi)
print(mi)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return tuple(map(int, input().split()))
 
N = int(input())
A = inpl()
 
print(min([abs(sum(A[:i])-sum(A[i:])) for i in range(1, N)]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from itertools import combinations

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

N = int(input())
A = inpl()
s = sum(A)

X = [sum(A[:i]) for i in range(1, N)]
print(min([abs(2*x - s) for x in X]))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
from itertools import combinations

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

N = int(input())
A = inpl()

print(min([abs(sum(A[:i])-sum(A[i:])) for i in range(1, N)]))

Submission status: Time Limit Exceeded

Solution code:
_ = input()
a = [int(i) for i in input().split(' ')]
res = 9999999999999999999999999
for i in range(1,len(a)):
    s = sum(a[:i])
    t = sum(a[i:])
    temp = abs(s-t)
    res = min(temp,res)
print(res)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    a = list(map(int, input().split()))
    #N = 1000
    #a = [i for i in range(N)]

    #summation = [abs(sum(a[:(i+1)]) - sum(a[(i+1):])) for i in range(len(a)-1)]

    summation = []
    sumtemp = sum(a)
    for i in range(len(a)-1):
        tmp1 = sum(a[:(i+1)])
        tmp2 = sumtemp - tmp1
        tmp = abs(tmp1 - tmp2)
        summation.append(tmp)
        
    print(min(summation))

main()

Submission status: Time Limit Exceeded

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

summation = [abs(sum(a[:(i+1)]) - sum(a[(i+1):])) for i in range(len(a)-1)]
print(min(summation))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int, input().split()))
b = sum(a)
minimum = float('inf')
for tmp in range(1,N):
    c = sum(a[tmp:len(a)])
    d = abs(b-2*c)
    minimum = min(minimum, d)
print(minimum)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int, input().split()))
minimum = float('inf')
for tmp in range(1,N):
    b = abs(sum(a[0:tmp])-sum(a[tmp:len(a)]))
    minimum = min(minimum, b)
print(minimum)

Submission status: Time Limit Exceeded

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

x = [0] * N

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

difference = [0] * (N-1)

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

print(min(difference))

Submission status: Time Limit Exceeded

Solution code:
#!usr/bim/python
#! -*- coding: utf-8 -*-

n = raw_input()
n = int(n)

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

score = abs(sum(a[0:1]) - sum(a[1:]))

i = 1
while i < n - 1:
    tmp = abs(sum(a[:i]) - sum(a[i:]))
    if  tmp <= score:
        score = tmp
    i += 1

print score

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
card = [int(i) for i in input().split()]
s = sum(card)
min_sum = [abs(s - 2*sum(card[0:i])) for i in range(1, N)]
print(min(min_sum))

Submission status: Time Limit Exceeded

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

min_sum =  abs(sum(card[1:N])-card[0])
for i in range(2,N):
    if abs(sum(card[0:i])-sum(card[i:N])) < min_sum:
        min_sum = abs(sum(card[0:i])-sum(card[i:N]))

print(min_sum)

Submission status: Time Limit Exceeded

Solution code:
def get_ans(a):

    X = sum(a)
    min_a = abs(a[0] - sum(a[1:]))
    for i in range(1, len(a)):
        tmp = abs(X - 2 * sum(a[0:i]))
        if tmp < min_a:
            min_a = tmp

    return min_a

if __name__ == '__main__':
    N = int(input())
    buf_a = [int(i) for i in input().split()]

    ans = get_ans(buf_a)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = input()
a = np.array([int(num) for num in input().split(' ')])

sum = 10000000000
for i in range(1, len(a)):
    sunuke = a[0: i].sum()
    araiguma = a[i:].sum()
    dif = abs(sunuke - araiguma)
    if dif < sum: sum = dif
print(sum)

Submission status: Time Limit Exceeded

Solution code:
def get_ans(a):

    min_a = abs(a[0] - sum(a[1:]))

    for i in range(1, len(a)):
        tmp = abs(sum(a[0:i]) - sum(a[i:]))
        if tmp < min_a:
            min_a = tmp

    return min_a


if __name__ == '__main__':
    N = int(input())
    buf_a = [int(i) for i in input().split()]

    ans = get_ans(buf_a)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
m = sum([abs(i) for i in a])
for i in range(1,N):
    m = min(m, abs(sum(a[:i])-sum(a[i:])))
print(m)

Submission status: Time Limit Exceeded

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

ans = None
for i in range(1,N):
    subs = sum(As[:i])
    temp = abs(2 * subs - S)
    if ans is None or temp < ans:
        ans = temp
print(ans)

Submission status: Time Limit Exceeded

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

all = sum(a)

mini = 10000000000

for i in range(1,N):
    x = sum(a[:i])
    y = all - x 
    ret = abs(x-y)

    if(ret < mini): mini=ret

print mini

Submission status: Accepted

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

Submission status: Accepted

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

def rui(A):
    R=[A[0]]*len(A)
    for i in range(len(A)-1):
        R[i+1]=R[i]+A[i+1]
    return R
R=rui(A)
S=sum(A)
ans=10**20
for i in range(n-1):
    a=R[i] ; b=S-a
    ans=min(ans, abs(a-b))
print(ans)

Submission status: Accepted

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

A = [a[0]]
for i in range(1,n):
    A.append(A[i-1] + a[i])

t = A[-1]
ans = float('inf')
for i in A[0:n-1]:
    ans = min(abs(i - (t-i)), ans)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

N = int(input())
A = list(map(int, input().split()))[::-1]

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

S = sum(A) - A[0]

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

print(ret)

Submission status: Accepted

Solution code:
N = int(input())

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

sum_a = sum(A)
ans = 10 ** 10

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

Submission status: Accepted

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

ans = 10**18
X = sum(A)
x = 0

for i,a in enumerate(A, start=1):
    if i == N: break
    x += a
    ans = min(ans, abs(X - (x * 2)))

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

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

v = sum(a)

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
b = [0]*N
b[0] = a[0]
for i in range(1, N):
    b[i] = a[i] + b[i-1]
ans = 10**15
for i in range(0, N-1):
    ans = min(ans, abs(b[i] - (b[N-1] - b[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(y-x)
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:
import sys
from itertools import accumulate
n, *lst = map(int, sys.stdin.read().split())
s = sum(lst)
res = min(abs(s - 2 * i) for i in list(accumulate(lst))[:-1])
print(res)

Submission status: Accepted

Solution code:
#!python3

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

# input
N = int(input())
A = LI()

INF = 10 ** 10


def main():
    x, y = 0, sum(A)
    ans = INF
    for a in A[:-1]:
        x += a
        y -= a
        ans = min(ans, abs(x - y))
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(a)

ans = 10**18

x, y = 0, 0

for i in range(n-1):
    x += a[i]
    y = s - x

    ans = min(ans, abs(y - x))

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(a[i]+s[-1])
tot=s[-1]
ans=10**12
for i in range(1,n):
  x=s[i]
  y=tot-x
  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)
tmp = 0
div = 10**10
for i in range(N-1):
    tmp += a[i]
    div = min(div, abs((sum_a-tmp)-tmp))
print(div)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
sunuke = 0
arai = 0
min1 = 10**10
sum1 = sum(L)
for i in range(n-1):
    sunuke += L[i]
    arai = (sum1 - sunuke)
    min1 = min(min1,abs(sunuke-arai))
    #print(sunuke,arai,min1)
print(min1)

Submission status: Accepted

Solution code:
N = int(input())
numbers = list(input().split())
sum_numbers = [0] * N
sum_numbers[0] = int(numbers[0])

for i in range(N - 1):
    sum_numbers[i + 1] = sum_numbers[i] + int(numbers[i + 1])

Ans = abs(2 * sum_numbers[0] - sum_numbers[N - 1])
for j in range(N - 1):
    if Ans >= abs(2 * sum_numbers[j] - sum_numbers[N - 1]):
        Ans = abs(2 * sum_numbers[j] - sum_numbers[N - 1])
    else:
        continue

print(Ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [A[0]]
for i in range(1, N):
    B.append(A[i]+B[i-1])
ans = 1 << 50
for i in range(0, N-1):
    tmp = abs(2*B[i]-B[N-1])
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

S = sum(A)

snuke = 0
ans = 10 ** 12

for i in range(N - 1):
  snuke += A[i]
  dif = abs(S - (2 * snuke))
  
  if ans >= dif:
    ans = dif
    
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
from _collections import defaultdict
from functools import lru_cache

class Scanner():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def map_int():
        return [int(x) for x in Scanner.string().split()]
    def string_list(n):
        return [input() for i in range(n)]
    def int_list_list(n):
        return [Scanner.mapInt() for i in range(n)]
    def int_cols_list(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True

MOD = int(1e09) + 7

def main():
    N = Scanner.int()
    A = Scanner.map_int()
    S = sum(A)
    a = A[0]
    ans = abs(S - 2 * a)
    for i in range(1,N - 1):
        a += A[i]
        ans = min(ans,abs(S - 2 * a))
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#coding:utf-8
import numpy as np
N = int(input())
A = list(map(int,input().split()))
a = np.cumsum(A)[:-1]
b = np.cumsum(A[::-1])[:-1][::-1]
print (np.min(np.abs(a-b)))

Submission status: Accepted

Solution code:
# ARC078C - Splitting Pile (ABC067C)
from itertools import accumulate


def main():
    N, *A = map(int, open(0).read().split())
    A = list(accumulate(A))
    total = A[-1]
    ans = min(abs(total - i * 2) for i in A[:-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


N = int(input())
A = inpl()
sumA = sum(A)

ans = 10**16
sunuke = 0
for a in A[:-1]:
    sunuke += a
    araiguma = sumA - sunuke
    if ans > abs(sunuke - araiguma):
        ans = abs(sunuke - araiguma)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sn = sum(a)
m = a[0]
mn = abs((sn - m)-m)
for i in range(1,n):
    if(abs((sn-m)-m)<mn):
        mn =abs( (sn  - m)-m)
    #print(mn)
    m += a[i]
    
print(mn)

Submission status: Accepted

Solution code:
N = int(input())
I = list(map(int, input().split()))
minX = 10000000000
end = I.pop()
sumI = sum(I)
for i in range(len(I)):
  minX = min(minX, abs(sumI - end ) )
  popped = I.pop()
  sumI -= popped
  end += popped
  
print(minX)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect
N = int(input())

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

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = []
b = input().split(" ")
for i in range(n):
	a.append(int(b[i]))

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

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

print(mn)

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 a[1:n-1]:
  x+=i
  y-=i
  ans=min(abs(x-y),ans)
print(ans)

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]

ans = float("inf")
for i in range(N-1):
    snuke = S[i]
    arai = S[N-1]-S[i]
    tmp = abs(arai - snuke)
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    t = 0
    ta = 10 ** 20
    ts = sum(a)
    for i in range(n-1):
        t += a[i]
        ta = min(ta, abs(t - (ts - t)))
    print(ta)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
def slove():
    import sys
    import heapq
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    s = sum(a)
    t = 0
    ma = 10 ** 15
    for i in range(n-1):
        t += a[i]
        ma = min(ma, abs(t - (s - t)))
    print(ma)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
from numpy import cumsum

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

cum = list(cumsum(L))

ans = 10 ** 10
for c in range(N-1):
    l = cum[c]
    r = cum[-1] - l
    ans = min(ans, abs(r - l))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = tuple(map(int, input().split()))
s = sum(a)
t = 0
m = 10 ** 20
for i, v in enumerate(a):
    if i < n-1:
        t += v
        m = min(m, abs(t * 2 - s))
print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
left = sum(a)
light = 0
ans = 10 **10

for i in range(0, N-1):
    left -= a[i]
    light += a[i]
    tmp = abs(left-light)
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#ARC078C
import sys
import math

n=int(input())

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))

total=0
for i in range(0,n):
	total+=hoge[i]
x=hoge[0]
y=total-x

res=abs(x-y)
#print("---")
#print(n)

for i in range(1,n-1):
	x+=hoge[i]
	y-=hoge[i]
	if(abs(x-y)<res):
		res=abs(x-y)
print(res)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

d = 10**15
s = 0
for aa in a[:-1] :
    s += aa
    d = min(d, abs(suma-2*s))
print(d)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
sum_A = sum(A)
tmp = A[0]
mn = abs(sum_A - tmp*2)
for i in range(1,N-1):
    tmp += A[i]
    # print(tmp, sum_A-tmp*2)
    if abs(sum_A-tmp*2) < mn:
        mn = abs(sum_A-tmp*2)
print(mn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().strip().split(' ')))
temp=0
pre=[]
for i in range(len(A)):
    temp+=A[i]
    pre+=[temp]

total=pre[-1]
MAX=10**20
for i in range(len(A)-1):
    MAX=min(MAX,abs(total-2*pre[i]))
print(MAX)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate


def main():
    n = int(input())
    lst = tuple(accumulate(map(int, input().rstrip().split())))
    total = lst[-1]
    ans = min(abs(total - i * 2) for i in lst[:-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate


def main():
    n = int(input())
    lst = tuple(accumulate(map(int, input().rstrip().split())))
    ans = min(abs(lst[-1] - i * 2) for i in lst[:-1])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC067C - Splitting Pile (ARC078C)
def main():
    n = int(input())
    lst = tuple(map(int, input().rstrip().split()))
    l, r, ans = 0, sum(lst), float("inf")
    for i in lst[:-1]:
        l += i
        r -= i
        ans = min(ans, abs(l - r))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class SegmentTree:
    def __init__(self, lst, op, e):
        self.n = len(lst)
        self.N = 2**((self.n-1).bit_length())
        self.op = op # operation
        self.e = e # identity element
        self.v = [None]*(2*self.N-1)
        
        # construction
        for i in range(self.N):
            self.v[i + self.N - 1] = lst[i] if i < self.n else self.e
        for i in range(self.N - 2, -1, -1):
            self.v[i] = self.op(self.v[2*i + 1], self.v[2*i + 2])
        
    # update a_i to be x 
    def update(self, i, x):
        i += self.N - 1
        self.v[i] = x
        while i > 0:
            i -= 1
            i >>= 1
            self.v[i] = self.op(self.v[2*i + 1], self.v[2*i+2])
    
    # returns answer for the query interval [l, r)
    def query(self, l, r):
        assert l < r
        left = l + self.N - 1
        right = r + self.N - 1
        L = self.e
        R = self.e
        while left < right:
            if left % 2 == 0: # left is even
                L = self.op(L, self.v[left])
                left += 1
            if right % 2 == 0: # right is even
                right -= 1
                R = self.op(self.v[right], R)
            left -= 1
            left >>= 1
            right >>= 1
        return self.op(L, R)

N = int(input())
*A, = map(int, input().split())
total = sum(A)
st = SegmentTree(A, lambda x,y:x+y, 0)
ans = 10**18
for i in range(N-1):
    snuke = st.query(0, i+1)
    arai = total - snuke
    ans = min(ans, abs(arai - snuke))
print(ans)

Submission status: Accepted

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

al, ar = [A[0]], [A[-1]]
for n in range(1, N - 1):
	al += [al[-1] + A[n]]
	ar += [ar[-1] + A[-(n + 1)]]
ar = ar[::-1]

ad = []
for n in range(N - 1):
	ad += [abs(al[n] - ar[n])]

#print(al)
#print(ar)
#print(ad)
print(min(ad))

Submission status: Accepted

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

al, ar = [A[0]], [A[-1]]
for n in range(1, N):
	al += [al[-1] + A[n]]
	ar += [ar[-1] + A[-(n + 1)]]
ar = ar[::-1]

ad = []
for n in range(N - 1):
	ad += [abs(al[n] - ar[n + 1])]

#print(al)
#print(ar)
#print(ad)
print(min(ad))

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
sum_ = sum(As)
ave = float(sum_)/2
diffs = [float('inf')]*(N-1)

mysum = 0
for i in range(N-1):
    mysum += As[i]
    diffs[i] = abs(ave-mysum)

print(int(2*min(diffs)))

Submission status: Accepted

Solution code:
import numpy

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

S = sum(a)
cs = numpy.cumsum(a)
res = abs(S - 2*cs)

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

Submission status: Accepted

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

S = sum(a)
_min  = 10000000000000000000
x = 0
for i in range(0,N-1):
  x += a[i]
  tmp = abs(S - 2*x)
  if tmp < _min:
    _min = tmp

print(_min)

Submission status: Accepted

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

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