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:
n=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=1)
qq=min(A, key=lambda x:abs(x))
a,b=0,0
c,d=0,0
for i in range(n):
    if A[i]>0 :
        if a<b: a+=A[i];c=1
        else: b+=A[i];d=1
    else:
        if a<b:
            b+=A[i];d=1
        else:
            a+=A[i];c=1
if c==0 or d==0:
    #print("ooio")
    print(abs(a-b)+2*abs(qq));exit()
print(abs(a-b))

Submission status: Wrong Answer

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

N = int(input())
A = list(map(int, input().split()))[::-1]
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: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

ans = 10**9 + 1
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: Wrong Answer

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

ans = 10**9
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: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

S = sum(A)
ans = 10**9
for n in range(N):
    x = abs(S - (sum(A[:n]) * 2))
    if x < ans and 0 < len(A[:n]) and 0 < len(A[n:]):
        ans = x

print(ans)

Submission status: Wrong Answer

Solution code:
N, *A = map(int, open(0).read().split())
ans = 10**9
for m in range(len(A)):
    a = A[:m]
    b = A[m:]
    if len(a) == 0:
        continue
    x = abs(sum(a) - sum(b))
    if x < ans:
        ans = x
    else:
        break

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

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

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

ans = abs(a[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: Wrong Answer

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
n = a // 2
if sum(ar[0:n]) <= sum(ar[n:a]):
    while True:
        l = sum(ar[0:n])
        r = sum(ar[n:a])
        if l >= r:
            break
        elif n == a - 1:
            print(abs(l - r))
            exit()
        n += 1
    print(min(abs(l - r),abs(sum(ar[0:n-1]) - sum(ar[n-1:a]))))
else:
    while True:
        l = sum(ar[0:n])
        r = sum(ar[n:a])
        if l <= r:
            break
        elif n == 1:
            print(abs(l - r))
            exit()
        n -= 1
    print(min(abs(l - r),abs(sum(ar[0:n+1]) - sum(ar[n+1:a]))))

Submission status: Wrong Answer

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
n = a // 2
if sum(ar[0:n]) <= sum(ar[n:a]):
    while True:
        l = sum(ar[0:n])
        r = sum(ar[n:a])
        if l >= r:
            break
        elif n == a - 1:
            print(abs(l - r))
            break
        n += 1
    print(min(abs(l - r),abs(sum(ar[0:n-1]) - sum(ar[n-1:a]))))
else:
    while True:
        l = sum(ar[0:n])
        r = sum(ar[n:a])
        if l >= r:
            break
        elif n == 1:
            print(abs(l - r))
            break
        n -= 1
    print(min(abs(l - r),abs(sum(ar[0:n+1]) - sum(ar[n+1:a]))))

Submission status: Wrong Answer

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

S = sum(A)

snuke = 0
ans = 10 ** 9

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

Submission status: Wrong Answer

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

S = sum(A)

snuke = 0
ans = 10 ** 9

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

Submission status: Wrong Answer

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

S = sum(A)

snuke = A[0]
ans = 10 ** 9

for i in range(1,N - 1):
  snuke += A[i]
  P.append(snuke)
  racoon = S - snuke
  dif = abs(snuke - racoon)

  if ans >= dif:
    ans = dif
    
print(ans)

Submission status: Wrong Answer

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

S = sum(A)

snuke = 0
ans = 10 ** 9

for i in range(N - 1):
  snuke += A[i]
  P.append(snuke)
  racoon = S - snuke
  dif = abs(snuke - racoon)

  if ans >= dif:
    ans = dif
    
print(ans)

Submission status: Wrong Answer

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

S = sum(A)

snuke = 0
ans = 10 ** 9

for i in range(N):
  snuke += A[i]
  P.append(snuke)
  racoon = S - snuke
  dif = abs(snuke - racoon)

  if ans >= dif:
    ans = dif
    
print(ans)

Submission status: Wrong Answer

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

S = sum(A)

snuke = 0
ans = 10 ** 9

for i in range(N - 1):
  snuke += A[i]
  P.append(snuke)
  racoon = S - snuke
  dif = abs(snuke - racoon)
  
  if ans >= dif:
    ans = dif
  elif dif < ans:
    break
    
print(ans)

Submission status: Wrong Answer

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()
    A.sort(reverse = True)
    a = 0
    for i in range(N):
        if a > 0:
            a -= A[i]
        else :
            a += A[i]
    print(abs(a))
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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()
    A.sort(reverse = True)
    a,b = A[N - 1],A[N - 2]

    for i in range(N - 2):
        if abs(a + A[i]) < abs(b + A[i]):
            a += A[i]
        else:
            b += A[i]
    print(abs(a - b))
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

a = sorted([int(x) for x in input().split()])

ans = []
for i in range(N):
  ans.append(abs(sum(a[:i])-sum(a[i:])))
  
print(min(ans))

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
L = (list(map(int,input().split())))
if n == 2:
    print(abs(L[1]-L[0]))
    exit()

L1 = [0]*n
L1[0] = L[0]

for i in range(1,n):
    L1[i] = L1[i-1]+L[i]
a = bisect.bisect(L1,sum(L)//2)
#print(a,L,L1)
print(min(abs(sum(L[:a-1]) - sum(L[a-1:])),abs(sum(L[:a]) - sum(L[a:])),abs(sum(L[:a+1]) - sum(L[a+1:]))) )

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
L = (list(map(int,input().split())))
if n == 2:
    print(abs(L[1]-L[0]))
    exit()

L1 = [0]*n
L1[0] = L[0]

for i in range(1,n):
    L1[i] = L1[i-1]+L[i]
a = bisect.bisect(L1,sum(L)//2)
#print(a,L,L1)
print(min(abs(sum(L[:a]) - sum(L[a:])),abs(sum(L[:a+1]) - sum(L[a+1:]))) )

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
L = sorted(list(map(int,input().split())))
if n == 2:
    print(abs(L[1]-L[0]))
    exit()

L1 = [0]*n
L1[0] = L[0]

for i in range(1,n):
    L1[i] = L1[i-1]+L[i]
a = bisect.bisect(L1,sum(L)//2)
#print(a,L,L1)
print(min(abs(sum(L[:a]) - sum(L[a:])),abs(sum(L[:a+1]) - sum(L[a+1:]))) )

Submission status: Wrong Answer

Solution code:
from math import *

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

a.sort()
b = list()
sn = sum(a)

ch = 1
if(sn%2==1):
    ch = 0
f = sn//2

m = 0
for i in range(n-1):
    m += a[i]
    if(m>=f):
        break
b.append(2*abs(m-f))


m = 0
for i in range(n-1,0,-1):
    m += a[i]
    if(m>=f):
        break
b.append(2*abs(m-f))

if(ch == 1):
    print(min(b))
else:
    print(min(b)+1)

Submission status: Wrong Answer

Solution code:
N = int(input())
I = list(map(int, input().split()))
minX = 1000000
for i in range(1, len(I)):
  print(i, sum(I[:-i]), sum(I[-i:]))
  minX = min(minX, abs(sum(I[:-i]) - sum(I[-i:]) ) )
  
  
print(minX)

Submission status: Wrong Answer

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

A = tuple(accumulate(map(int,input().split())))
print(abs(A[-1] - 2 * A[min(N-2,bisect(A, A[-1]/2)-1)]))

Submission status: Wrong Answer

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()))
    for i in range(n):
        a[i] = a[i] * -1
    heapq.heapify(a)
    e = 0
    o = 0
    while True:
        if len(a) != 0:
            if e >= o:
                o += -heapq.heappop(a)
            else:
                e += -heapq.heappop(a)
        else:
            print(abs(e - o))
            exit()



if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    a.sort(reverse=True)
    x = y = 0
    for v in a:
        if x >= y:
            if v >= 0:
                y += v
            else:
                x += v
        else:
            if v >= 0:
                x += v
            else:
                y += v
    print(abs((x - y)))


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    a.sort(reverse=True)
    x = y = 0
    for v in a:
        if x >= y:
            y += v
        else:
            x += v
    print(abs((x - y)))


if __name__ == '__main__':
    slove()

Submission status: Wrong Answer

Solution code:
from numpy import cumsum

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

cum = list(cumsum(L))

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

print(ans)

Submission status: Wrong Answer

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

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

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

#print("---")
#print(n)
for i in range(1,n):
	tmp=y-x
	x+=hoge[i]
	y-=hoge[i]
	if(x>y):
		print(min(abs(tmp),abs(x-y)))
		sys.exit()
	if(i==n-1):
		print(abs(tmp))
		sys.exit()

Submission status: Wrong Answer

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

for i in range(1,n):
	tmp=y-x
	x+=hoge[i]
	y-=hoge[i]
	if(x>y)or(i==n-1):
		print(abs(min(tmp,x-y)))
		sys.exit()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
sum_A = sum(A)
tmp = A[0]
mn = 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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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+1]
  if abs(check)<ans:
    ans=abs(check)
print(ans)

Submission status: Wrong Answer

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-=num[i+1]
  if abs(check)<ans:
    ans=abs(check)
print(ans)

Submission status: Wrong Answer

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
As = list(map(int, input().split()))
sum_ = sum(As)
ave = float(sum_)/N
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: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

print(_min)

Submission status: Wrong Answer

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

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

print(_min)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

a_sum = sum(np.abs(a))
if a_sum % 2 == 0:
    print(a_sum)
else:
    sunuke = a_sum //2
    arai = a_sum - sunuke
    print(abs(arai - sunuke))

Submission status: Wrong Answer

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[1])
#print(ans)
for i in range(N-1):
  ans = min(ans, abs(2*s[i]-s[N-1]))
  #print(ans)
  
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 = 1
for i in range(N-1):
  ans = min(ans, 2*s[i]-s[N-1])
  # print(i)
  if s[i+1] == 0:
    ans = 0
  
print(ans)

Submission status: Wrong Answer

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

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

ans = 1
for i in range(N, 1):
  ans = min(ans, 2*s[i]-s[N])
  if 2*s[i]-s[N] == 0:
    ans = 0
  
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Min = 10**20
for s in S:
  Min = min(Min,abs((s-S[0])-(S[N]-s)))
print(Min)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = 0
s_list = []

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

for n in input_list:
  S += n
  s_list.append(2 * S)

all_list = [abs(sum(input_list) - a) for a in s_list]
 
print(min(all_list))

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

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

total = sum(a)
now = a[0]
ans = abs(total - 2*now)
for i in range(1, len(a)-1):
    now += a[i]
    if abs(total - 2*now) < ans:
        ans = abs(total - 2*now)
    else:
        break
print(ans)

Submission status: Wrong Answer

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

ans = float("inf")
X = a[-1]
Y = sum(a) - a[-1]

for i in range(1, N):
	if abs(X - Y) < ans:
		ans = abs(X - Y)

	X += a[i]
	Y -= a[i]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import collections
import math

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

L = [0] * N

for i in range(N):
    if i == 0:
        L[i] = A[i]
    else:
        L[i] = A[i] + L[i - 1]

min_diff = 10*20
for i in range(N):
    s = L[i]
    a = L[N-1] - L[i]
    diff = abs(a-s)
    min_diff = min(min_diff, diff)

print(min_diff)

sys.exit(0)

Submission status: Wrong Answer

Solution code:
import sys
import collections
import math

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

L = [0] * N

for i in range(N):
    if i == 0:
        L[i] = A[i]
    else:
        L[i] = A[i] + L[i - 1]

s = 0
a = 0
min_diff = 10*20
for i in range(N-1):
    s = L[i]
    a = L[N-1] - L[i]
    diff = abs(a-s)
    min_diff = min(min_diff, diff)

print(min_diff)

sys.exit(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
dif = float('inf')
snk = 0
for i in range(n-1):
    snk += a[i]
    if abs(snk*2-s) > dif:
        break
    dif = abs(snk*2-s)
print(dif)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
dif = float('inf')
snk = 0
for i in range(n-1):
    snk += a[i]
    if abs(snk*2-s) > dif:
        break
    dif = abs(snk*2-s)
print(abs(snk*2-s))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
dif = float('inf')
snk = 0
for i in range(n-1):
    snk += a[i]
    if abs(snk*2-s) > dif:
        break
    dif = abs(snk*2-s)
print(snk*2-s)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
aa = [0]
s = 0
for x in input().split():
    a = int(x)
    aa.append(aa[-1]+a)
    s += a

ret = abs(2*aa[0]-s)
for i in range(1,n):
    if abs(aa[i]-(s-aa[i]))<ret:
        ret = abs(aa[i]-(s-aa[i]))

print(ret)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
aa = [0]
s = 0
for x in input().split():
    a = int(x)
    aa.append(aa[-1]+a)
    s += a

ret = 10**9
for i in range(1,n):
    if abs(aa[i]-(s-aa[i]))<ret:
        ret = abs(aa[i]-(s-aa[i]))

print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())

A = list(map(int,input().split()))
B = [] #累積和するぞ！

s = 0
for a in A:
    s += a
    B.append(s)

sabun_m = abs(B[N-1] - (B[0]*2))

'''
1 2 3 4

1 | 2 3 4 (1234-1)-1
'''

for b in B[1:]:
    sabun = abs(B[N-1] - (b*2))
    if sabun < sabun_m:
        sabun_m = sabun
        
print(sabun_m)

Submission status: Wrong Answer

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)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
#import time
#s = time.time()
min_ = abs(min(a))
a = [i + min_ for i in a]
#e = time.time()
#print(e - s)
#これで全部0以上
# 最低一個は使わなきゃいけない

use = sum(a)//2
sum_ = a[0]
for i in range(1, n - 1):
    if sum_ >= use:
        break
    sum_ += a[i]
print(abs(sum(a) - 2*sum_))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = [i + abs(min(a)) for i in a]
#これで全部0以上
# 最低一個は使わなきゃいけない

use = sum(a)//2
sum_ = a[0]
for i in range(1, n - 1):
    if sum_ >= use:
        break
    sum_ += a[i]
print(abs(sum(a) - 2*sum_))

Submission status: Wrong Answer

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

use = sum(a)//2
sum_ = 0
for i in range(n):
    if sum_ >= use:
        break
    sum_ += a[i]
print(abs(sum(a) - 2*sum_))

Submission status: Wrong Answer

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

x = 0

total = sum(a)
difference = 100

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

print(difference)

Submission status: Wrong Answer

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

for i in range(N):
    if a[i] < 0:
        a[i] = - a[i]

mean = int(sum(a) / 2)

x = 0

for i in range(N):
    if x < mean or x == 0:
        x += b[i]
        y = sum(b) - x
    else:
        pass

if y == 0:
    x = sum(b) - b[-1]
    y = b[-1]

print(abs(x - y))

Submission status: Wrong Answer

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

for i in range(N):
    if a[i] < 0:
        a[i] = - a[i]

mean = int(sum(a) / 2)

x = 0
y = 0

for i in range(N):
    if x < mean:
        x += b[i]
    else:
        pass

y = sum(b) - x

print(abs(x - y))

Submission status: Wrong Answer

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

total = sum(x)
cum_sum = 0
ans = float('inf')
for i in range(N - 1):
    cum_sum += x[i]
    ans = min(ans, total - cum_sum)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print res

Submission status: Wrong Answer

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

all_sum = sum(a)

results = []
now = 0
for i in a:
    now += i
    at = all_sum - now
    results.append(abs(now - at))

print(min(results))

Submission status: Wrong Answer

Solution code:
n = raw_input()
a = raw_input().split(' ')
a = map(int, a)
s = 0
sunuke = a[0]
arai = a[len(a)-1]
for i in a:
	s = s + i
m = abs(sunuke - (s - sunuke))

for i in range(1,len(a)-2):
	sunuke = sunuke + a[i]
	arai = s - a[i]
	if abs(arai-sunuke) < m:
		m = abs(arai-sunuke)


print m

Submission status: Wrong Answer

Solution code:
n = raw_input()
a = raw_input().split(' ')
a = map(int, a)
s = 0
sunuke = 0
arai = 0
for i in a:
	s = s + i
m = abs(s)

for i in range(0,len(a)-1):
	arai = arai + a[i]
	sunuke = s - a[i]
	if abs(arai-sunuke) < m:
		m = abs(arai-sunuke)


print m

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
summ = a[0]-sum(a[1:])
m = sum([abs(i) for i in a])
if N == 2:
    print(abs(summ))
else:
    for i in range(1, N):
        summ += 2*(a[i])
        m = min(m, abs(summ))
    print(m)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
summ = a[0]-sum(a[1:])
m = sum([abs(i) for i in a])
if N == 2:
    print(summ)
else:
    for i in range(1, N):
        summ += 2*(a[i])
        m = min(m, abs(summ))
    print(m)

Submission status: Wrong Answer

Solution code:
n = raw_input()
a = raw_input().split(' ')
a = map(int, a)
a.sort()

arai = []
sunuke = []

arai.append(a.pop(len(a)-1))
sunuke.append(a.pop(len(a)-1))
totalarai = arai[0]
totalsunuke = sunuke[0]
while len(a):
	if a[len(a)-1] >= 0:
		if totalarai < totalsunuke:
			totalarai = a[len(a)-1] + totalarai
			arai.append(a.pop(len(a)-1))

		else:
			totalsunuke = a[len(a)-1] + totalsunuke
			sunuke.append(a.pop(len(a)-1))
	else:
		if totalarai > totalsunuke:
			totalarai = a[len(a)-1] + totalarai
			arai.append(a.pop(len(a)-1))

		else:
			totalsunuke = a[len(a)-1] + totalsunuke
			sunuke.append(a.pop(len(a)-1))
print abs(totalarai - totalsunuke)

Submission status: Wrong Answer

Solution code:
def c():
    import numpy as np

    N = int(input() )
    A = np.array([int(a) for a in input().split()])

    A_cum = np.cumsum(A)
    A_rev = A_cum[-1] - A_cum
    res = np.abs(A_rev - A_cum)[:-1]
    print(int(np.min(res)))

    print(A_cum, A_rev, res)


c()

Submission status: Wrong Answer

Solution code:
n = raw_input()
a = raw_input().split(' ')
a = map(int, a)
a.sort()

arai = []
sunuke = []

arai.append(a.pop(len(a)-1)) #add max
sunuke.append(a.pop(len(a)-1))
totalarai = arai[0]
totalsunuke = sunuke[0]
while len(a):
	if totalarai < totalsunuke:
		totalarai = a[len(a)-1] + totalarai
		arai.append(a.pop(len(a)-1))

	else:
		totalsunuke = a[len(a)-1] + totalsunuke
		sunuke.append(a.pop(len(a)-1))

print abs(totalarai - totalsunuke)

Submission status: Wrong Answer

Solution code:
n = raw_input()
a = raw_input().split(' ')
a = map(int, a)
a.sort()

arai = []
sunuke = []

print a
arai.append(a.pop(len(a)-1)) #add max
sunuke.append(a.pop(len(a)-1))
totalarai = arai[0]
totalsunuke = sunuke[0]
while len(a):
	if totalarai < totalsunuke:
		totalarai = a[len(a)-1] + totalarai
		arai.append(a.pop(len(a)-1))

	else:
		totalsunuke = a[len(a)-1] + totalsunuke
		sunuke.append(a.pop(len(a)-1))

print abs(totalarai - totalsunuke)

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
a=map(int,raw_input().split())
top=N-1
bottom=1
res=10**10
d=-sum(a)
for i in range(1,N):
    d+=2*a[i-1]
    print i,d
    if abs(d)<=res:
        res=abs(d)
print res

Submission status: Wrong Answer

Solution code:


def spaceinput():
    return list(map(int,input().split(" ")))
N=int(input())
aa=spaceinput()


suml=aa[0]
sumr=sum(aa[1:])
minv=10**9
for i in range(1,len(aa)):
    #print(suml,sumr)
    v=abs(suml-sumr)
    if minv>v:
        minv=v

    suml+=aa[i]
    sumr-=aa[i]


print(minv)

Submission status: Wrong Answer

Solution code:
N=int(raw_input())
a=map(int,raw_input().split())
top=N-1
bottom=1
res=10**10
d=-sum(a[0:])
for i in range(1,N):
    d+=2*a[i]
    if abs(d)<=res:
        res=abs(d)
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
d=a[0]-sum(a[1:])
for i in range(1,N):
    d+=2*a[i]
    if abs(d)<=res:
        res=abs(d)
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
for i in range(1,N):
    if a[i]>0 and i<N-1:
            continue
    d=sum(a[:i])-sum(a[i:])
    if abs(d)<=res:
        res=abs(d)
print res

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()


suml=aa[0]
sumr=sum(aa[1:])
minv=10**5
for i in range(1,len(aa)):
    #print(suml,sumr)
    v=abs(suml-sumr)
    if minv>v:
        minv=v

    suml+=aa[i]
    sumr-=aa[i]


print(minv)

Submission status: Wrong Answer

Solution code:
import numpy as np

N=int(input())
arr=np.array([int(i) for i in input().split()])

if N==2:
    print(abs(arr[0]-arr[1]))
else:
    arr=np.absolute(arr)
    arr=np.sort(arr)[::-1]
    a=0
    b=0
    for i in arr:
        if a<b:
            a+=i
        else:
            b+=i
    print(abs(a-b))