Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

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

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

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

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

Sample Input 2

2
10 -10

Sample Output 2

20

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



Sample input:
6
1 2 3 4 5 6


Sample output:
1

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
# @lru_cache(None)


n=l()[0]
A=l()
X=sum(A)
x=0
ans=10**10
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:
from itertools import accumulate

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

ans = float('inf')
for i in range(N-1):
  x = B[i]
  y = S - x
  ans = min(ans, abs(x - y))

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

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

    ans = float("inf")
    for i in range(N - 1):
        x = S[i]
        y = S[-1] - x
        cost = abs(x - y)
        if cost < ans:
            ans = cost

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

p=sum(a)

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

Submission status: Accepted

Solution code:
import numpy as np

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

b = a[0]
c = a[1:].sum()
ans = abs(b - c)

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

  if ans > abs(b-c):
    ans = abs(b-c)
else:
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
ans = 10**18
for i, cumsum in enumerate(accumulate(a), start=1):
    if i==n:
        break
    sunuke = cumsum
    araiguma = total - cumsum
    ans = min(ans, abs(sunuke - araiguma))
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
lista = list(map(int, input().split()))
a = 0
b = sum(lista)
ans = abs(lista[0]- (b- lista[0]))

for i in range(N- 1):
  a+= lista[i]
  b-= lista[i]
  if(abs(a- b)< ans):
    ans = abs(a- b)
    
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate


N = int(input())
A = list(accumulate(map(int, input().split())))
print(min(abs(A[N - 1] - 2 * A[i]) for i in range(N - 1)))

Submission status: Accepted

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

Submission status: Accepted

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

x = [0] * (N+1)

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

suma = x[N]
y = [suma] * (N+1)
for i in range(1, N+1):
  y[i] = y[i-1] - a[i-1]
  
ans = sum([abs(i) for i in a])
for i in range(1, N):
  ans = min(ans, abs(x[i]-y[i]))
  
print(ans)

Submission status: Accepted

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

total = sum(A)
ans = 10**10
res = 0

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

print(ans)

Submission status: Accepted

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

s = sum(a)

ans= 10**18

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

    ans = min(ans,abs(subtotal - (s - subtotal)))


print(ans)

Submission status: Accepted

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

    s = [0] * N
    s[0] = a[0]
    sr = [0] * N
    sr[0] = a[-1]

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

    ans = 10 ** 18
    for i in range(N-1):
        tmp = abs(s[i] - sr[N-i-2])
        if tmp < ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N = int(input())
a = list(map(int,input().split()))
ruiseki = list(itertools.accumulate(a))
b = sum(a)
ans = 10**100
for i in range(N-1):
    ans = min(ans,abs(ruiseki[i]*2-b))
print(ans)

Submission status: Accepted

Solution code:
import itertools

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

su = sum(a)
ruisekiwa = list(itertools.accumulate(a))
ruisekiwa.pop()
ans = 10**99

for i in ruisekiwa:
  ans = min(ans,abs(su - 2*i))
  
print(ans)

Submission status: Accepted

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

candidates = []
snuke = 0
for a in A:
    snuke += a
    raccoon = total - snuke
    result = abs(snuke - raccoon)
    candidates.append(result)

print(min(candidates))

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
aaa = list(map(int, input().split()))
a_sum = list(accumulate(aaa))
ans = 1e10
for i in range(n - 1):
    ans = min(ans, abs(a_sum[i] - a_sum[n - 1] + a_sum[i]))
print(ans)

Submission status: Accepted

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

X = sum(A)
x = 0
ans = 10000000000007

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

print(ans)

Submission status: Accepted

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

X = sum(A)
x = 0
ans = 10000000000007

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

print(ans)

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().strip().split()))

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

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

print(ans)

Submission status: Accepted

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

from itertools import accumulate

lin = list(accumulate(li))
mi = 10**18
S = sum(li)

for i in range(n-1):
    ara = lin[i]
    sunu = S-ara
    mi = min(mi,abs(ara-sunu))

print(mi)

Submission status: Accepted

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

total = sum(A)
x = 0
ans = 10000000007

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

print(ans)

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal


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


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = readints()
# print(a)
sum_a = sum(a)
ans = 10**19
s = [0]*(n+1)
for i in range(n-1):
    s[i+1] = s[i]+a[i]
    # print(s[i+1])
    tmptmp = sum_a-s[i+1]
    #print('tmp', tmp)
    #print('tmptmp', tmptmp)
    x = abs(s[i+1]-tmptmp)
    ans = min(ans, x)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    tot = sum(A)

    ans = (10 ** 9) * (2 * 10 ** 5) + 1
    s = 0
    for x in A[:-1]:
        s += x + x
        diff = abs(tot - s)
        if ans > diff:
            ans = diff

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    from itertools import accumulate

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

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

    ans=10**18
    for i in range(1, N):
        snuke=B[i]
        araiguma=B[N]-B[i]
        ans=min(ans,abs(snuke-araiguma))
    print(ans)
resolve()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
su = sum(a)
acc = list(itertools.accumulate(a))
b = [abs((su-acc[i]) - acc[i]) for i in range(n-1)]
# print(b)
print(min(b))

Submission status: Accepted

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

total = sum(a)
b = list(itertools.accumulate(a))

ans = pow(10, 12)
for i in range(n-1):
    ans = min(abs(b[i] - (total - b[i])), ans)

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

Submission status: Accepted

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

Max = N[-1]
ans = float("INF")
for i in range(1, n):
    if abs(N[i] - (Max-N[i])) < ans:
        ans =  abs(N[i] - (Max-N[i]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
s = a[:1]
t = a[1:]
x = sum(s)
y = sum(t)
if x==y:
  ans = 0
else:
  ans = abs(x-y)

for i in range(1,N-1):
  x += a[i]
  y -= a[i]
  if x==y:
    ans = 0
    break
  elif abs(x-y)<ans:
    ans = abs(x-y)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
r = [0]*(N+1)
#累積和
a = 0
for i in range(N):
  a += A[i]
  r[i+1] = a
minimum = 10**18
for i in range(1,N):
  snuke = r[i]
  arai = r[N]-r[i]
  if minimum > abs(snuke-arai):
    minimum = abs(snuke-arai)
print(minimum)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
if N==2:
    print(abs(A[0]-A[1]))
    exit()
SUM=sum(A)
ans=float("INF")
now=0
for i in range(N-1):
    now+=A[i]
    ans=min(ans, abs(now-(SUM-now)))


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

S = sum(a)
s = np.cumsum(a)
ans = float('INF')

for i in range(n-1):
    x = s[i]
    y = S - x
    ans = min(ans, abs(x - y))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
from itertools import accumulate
s=list(accumulate(a))
a_sum=sum(a)
ans=10**18
if n==2:
  print(abs(a[0]-a[1]))
  exit(0)
for i in range(1,n-1):
  x=s[i]
  y=a_sum-x
  ans=min(ans,abs(x-y))
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    snuke = sum(A)
    arai = 0

    res = f_inf
    for i in range(n - 1):
        snuke -= A[i]
        arai += A[i]
        res = min(res, abs(snuke - arai))

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

N = ni()
A = nl()


def solve():
    tot = sum(A)
    ans = 1 << 60
    s = 0
    for i, x in enumerate(A):
        if i == N - 1:
            break
        s += x
        a = abs(2 * s - tot)
        ans = min(ans, a)
    return ans


print(solve())

Submission status: Accepted

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

sa=sum(a)
ans=10**10+1
x=0
for i in range(0,n-1):
  x+=a[i]
  if abs(2*x-sa)<ans:
    ans=abs(2*x-sa)
print(ans)

Submission status: Accepted

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

X = sum(A)
Y = 0
MIN = 10**18
for i,a in enumerate(A):
    if i < N-1:
        X -= a
        Y += a
        tmp = abs(X-Y)
        if tmp < MIN:
            MIN = tmp
    
print(MIN)

Submission status: Accepted

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

ans = 10 ** 20

for a in Acum[:-1]:
    snuke = a
    arai = Acum[-1] - a
    ans = min(ans, abs(snuke - arai))
print(ans)

Submission status: Accepted

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

ans = 10**20
for i in range(1,N):
    x = cums[i]
    y = cums[-1] - cums[i]
    ans = min(ans, abs(x-y))
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
from collections import deque
import heapq
import math
import fractions
import bisect
import itertools
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
a=lmp()
sa=0
sb=sum(a)
ans=float("inf")
for i in range(n-1):
    sa+=a[i]
    sb-=a[i]
    ans=min(ans,abs(sa-sb))
print(ans)

Submission status: Accepted

Solution code:
# 累積じゃね
n = int(input())
arr = list(map(int, input().split()))
ruiseki = [0] * (n + 1)
for i in range(1, n + 1):
    ruiseki[i] = ruiseki[i - 1] + arr[i - 1]
ans = 10**10
for i in range(1, n):
    takahashi = ruiseki[i]
    raskal = ruiseki[n] - takahashi
    ans = min(ans, abs(raskal - takahashi))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

s = sum(A)

x = 0
ans = 1 << 60
for a in A[:-1]:
    x += a
    ans = min(ans, abs(s - 2 * x))

print(ans)

Submission status: Accepted

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

min_diff = float('inf')
a_sum = sum(a)
l_sum = 0

for i in range(N - 1):
  l_sum += a[i]
  min_diff = min(min_diff, abs(l_sum * 2 - a_sum))

print(min_diff)

Submission status: Accepted

Solution code:
N = int(input())
*a, = map(int, input().split())
l, r = 0, sum(a)
ans = 10**19
for i in range(N-1):
    l += a[i]
    r -= a[i]
    t = l-r
    if abs(t) < ans:
        ans = abs(t)
print(ans)

Submission status: Accepted

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

print(min(d))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    tot = sum(A)
    s = 0
    ans = float('inf')
    for a in A[:-1]:
        s += a
        ans = min(ans, abs(2 * s - tot))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
z=list(accumulate(lis))
#print(list(accumulate(lis)))
mn=inf
for i in range(n-1):
    dif=abs(z[-1]/2-z[i])
    if dif<mn:
        mn=dif
print(int(2*mn))

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
total = sum(L)

# e.g.) Sunuke takes i cards from the top (1 <= i <= N-1)
# Sunuke's score is sum(L[:i]) ... L[i] -> + L[i+1]
# Racoon's score is sum(L[i:]) ... sum(L) - L[i] -> L[i+1]


sunuke = 0
ans = 2**64-1
for i in range(N-1):
  sunuke += L[i]
  racoon = total - sunuke
  ans = min(ans, abs(sunuke - racoon))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    input()
    t = list(map(int, input().split()))
    # if len(t) == 2:
    #     print(abs(t[0] - t[1]))
    #     exit()
    all = sum(t)
    left = t[0]
    right = all - t[0]
    ans = abs(left-right)
    for i in range(1, len(t)-1):
        left += t[i]
        if ans > abs(left-(all-left)):
            ans = abs(left-(all-left))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

s = 0
for i in a:
    s += i

ans = float('inf')
tmp = 0
for i in range(n-1):
    tmp += a[i]
    ans = min(ans,abs(s-tmp-tmp)) 

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=set()
for i in range(1,n):A[i]+=A[i-1]
for i in range(n-1):
    ans.add(abs(A[-1]-A[i]-A[i]))
print(min(ans))

Submission status: Accepted

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

sal=sum(al)
res=10**9+1
temp=0
for i in range(0,n-1):
    temp += al[i]
    res = min(res,abs(sal/2-temp))
    if res <=0.5:
        break

print(int(res*2))

Submission status: Accepted

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

res = 10**12
n = sum(a)
x = 0
for i in range(N-1):
    x += a[i]
    y = n - x
    ans = abs(x-y)
    res = min(res,ans)
print(res)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = np.array(list(map(int, input().split())))

a = abs(2*np.cumsum(a) - a.sum())[:-1]

print(min(a))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ruisekiwa = [0]*(N+1)
now = 0
minimum = 10**10
for i in range(N):
  now += A[i]
  ruisekiwa[i+1] = now
for i in range(N-1):
  snuke = ruisekiwa[i+1]
  arai = ruisekiwa[N]-snuke
  K = abs(snuke-arai)
  if K < minimum:
    minimum = K
print(minimum)

Submission status: Accepted

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

ans = 2*10**9
allsum = sum(a)
snukesum = 0

for n in range(1,N):
  snukesum += a[n-1]
  ans_candidate = abs(2*snukesum- allsum)
  if ans > ans_candidate:
    ans = ans_candidate
    
print(ans)

Submission status: Accepted

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

min=1000000000000000
left_sum=0
right_sum=sum(a)
for i in range(n-1):
    left_sum += a[i]
    right_sum -= a[i]
    xy=abs(left_sum-right_sum)
    if xy < min:
        min=xy
print(min)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
M = sum(L)
m = float('inf')
s = 0
for i in range(n - 1):
    s += L[i]
    r = M - s
    m = min(m, abs(s - r))
print(m)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
import numpy as np

n = ii()
A = iil()

l = np.cumsum(A)
su = l[-1]
#print(l)
ans = 1e18
for i in range(n-1):
    x = l[i]
    y = su - x
    ans = min(ans,abs(x-y))

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
total = sum(A)
x = 0
ans = float("inf")

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
sum_A = np.sum(A)
cumsum_A = np.cumsum(A)

ans = float('inf')
for i in range(N - 1):
    ans = min(ans, abs(sum_A - 2 * cumsum_A[i]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

whole = sum(a)

if n == 2:
    print(abs(a[0] - a[1]))
else:
    now = 0
    ans = float("inf")
    for i in a[:-1]:
        now += i
        diff = abs(whole - 2 * now)
        ans = min(ans, diff)

    print(ans)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

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

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1

#############
# Main Code #
#############

N = I()
A = IL()

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

S = [A[0]]
for a in A[1:]:
  S.append(S[-1]+a)
SUM = S[-1]
S = [ abs(SUM-s-s)for s in S[1:N-1]]
P(min(S))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ruiseki = [0]*(N+1)
total = 0
now = 10**20
sa = 0
now_2 = 0
for i in range(N):
  total += A[i]
  ruiseki[i+1] = total
for i in range(1,len(ruiseki)-1):
  snuke = ruiseki[i]-ruiseki[0]
  arai = ruiseki[N]-ruiseki[i]
  now_2 = abs(snuke-arai)
  now = min(now,now_2)
print(now)

Submission status: Accepted

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

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

total_sum = a2[-1]

a2.pop(0)
sum_n = a2[-1]

output = []
for i in range(N-1):
    output.append(abs(sum_n - (2 * a2[i])))
    
print(min(output))