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 min(l):
    n = len(l)
    min = 114514
    for i in range(n):
        if min>l[i]:
            min=l[i]
    return min

N = int(input())
l = [int(0)] * N
l = list(map(int, input().split()))
a = [0]*N
b = [0]*N
c = [0]*N
for i in range(N):
    for j in range(i):
        a[i]+=l[j]
    for j in range(i,N):
        b[i]+=l[j]
    c[i]=abs(a[i]-b[i])
print(min(c))

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
l = [int(i) for i in input().split()]
sorted(l)
#print(len(l))
A = []
x = 0
y = 0
sum = sum(l)

for i in range(N):
	x += l[i]
	if x >= int(sum/2):
		j = i+1
		while True:
			if j == N:
				break
			y += l[j]
			j+=1
	
		break


 
	
#print(int(sum/2))
#print(x,y)
print(int(math.fabs(x-y)))

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
l = [int(i) for i in input().split()]
sorted(l)
print(len(l))
A = []
x = 0
y = 0
sum = sum(l)

for i in range(N):
	x += l[i]
	if x >= int(sum/2):
		j = i+1
		while True:
			if j == N:
				break
			y += l[j]
			j+=1
	
		break


 
	
#print(int(sum/2))
#print(x,y)
print(int(math.fabs(x-y)))

Submission status: Wrong Answer

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

r_index = N - 1
l_index = 1
min_diff = None
pre_index = None

while True:
    index = (l_index + r_index) // 2
    l_sum = sum(a_list[:index])
    r_sum = sum(a_list[index:])
    diff = abs(l_sum - r_sum)
    min_diff = diff if min_diff == None else min(min_diff, diff)

    if pre_index == None:
        pass
    elif index == pre_index:
        break

    if l_sum < r_sum:
        l_index = index + 1
    else:
        r_index = index

    pre_index = index

print(min_diff)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8

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

	print(BinarySearch(calcDif, cards, 0))

def BinarySearch(func, cards, target):
	left = 1
	left_val = func(cards, left)
	right = len(cards) - 1
	right_val = func(cards, right)
	
	#print(right, left)
	if right - left == 0:
		return(abs(func(cards, 1)))
	elif right - left == 1:
		return(min(abs(func(cards, 1)), abs(func(cards, 2))))

	while right >= left:
		mid = int((left + right)/2)
		mid_val = func(cards, mid)

		# print(left, mid, right)
		# print(left_val, mid_val, right_val)
		
		if mid_val == target:

			return(abs(mid_val))
		elif mid_val > target:
			right = mid - 1
			right_val = mid_val
		elif mid_val < target:
			left = mid + 1
			left_val = mid_val

	return(min([abs(func(cards, i)) for i in [mid - 1, mid, mid + 1]]))

def calcDif(cards, k):
	# k >= 1
	#print(k)
	x = cards[:k]
	y = cards[k:len(cards)]
	return(sum(x) - sum(y))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
s.sort()

a = 0
b = 0
c = 0

for i in range(n) :
    c = i
    if i == 0 :
        a = s[i]
    elif i == 1 :
        b = s[i]
    elif s[n-1-i] >= 0 :
        if a <= b :
            a = a + s[n-1-i]
        else :
            b = b + s[n-1-i]
    else :
         break

if c != n-1 :
  for i in range(n-c) :
    if a<= b :
        b = b + s[i]
    else :
        a = a + s[i]

print (abs(a-b))

Submission status: Wrong Answer

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

sunuke = sum(a[1:])
raccoon = a[0]
minimum = abs(sunuke - raccoon)
for i in range(1, N):
    print(sunuke, raccoon)
    if(abs(sunuke - raccoon) < minimum):
        minimum = abs(sunuke - raccoon)
    sunuke -= a[i]
    raccoon += a[i]
       
print(minimum)

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input().strip())
    a = list(map(int, input().strip().split()))
    min_v = float("inf")
    for i in range(1, N):
        print(a[:i], a[i:])
        x = sum(a[:i])
        y = sum(a[i:])
        min_v = min(min_v, abs(x - y))
    print(min_v)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

SUM_CARD = sum(a)

i=0

sum_before = a[i]
diff_sum = abs(sum_before*2-SUM_CARD)
i+=1

while(i<N):
    sum_before = sum_before + a[i]
    diff_sum_new = abs(sum_before*2-SUM_CARD)
    if diff_sum_new<diff_sum:
        diff_sum = diff_sum_new
    i += 1

print(diff_sum)

Submission status: Wrong Answer

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

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

SUM_CARD = sum(a)

i=1

sum_before = sum(a[0:i])
diff_sum = abs(sum_before*2-SUM_CARD)
i+=1

while(i<N):
    sum_before = sum_before + a[i]
    diff_sum_new = abs(sum_before*2-SUM_CARD)
    if diff_sum_new<diff_sum:
        diff_sum = diff_sum_new
    i += 1

print(diff_sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
s.sort()

a = 0
b = 0
c = 0

for i in range(n) :
    c = i
    if s[n-1-i] >= 0 :
        if a <= b :
            a = a + s[n-1-i]
        else :
            b = b + s[n-1-i]
    else :
         break

for i in range(n-c) :
    if a<= b :
        b = b + s[i]
    else :
        a = a + s[i]

print (abs(a-b))

Submission status: Wrong Answer

Solution code:
import math

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

m = []
p = []

for x in a:
    if x < 0:
        m.append(x)
    else:
        p.append(x)

m.sort(reverse=True)
p.sort()

m_half = math.ceil(sum(m) / 2)
p_half = math.floor(sum(p) / 2)

p_sunuke = 0
p_araiguma = 0

for i in range(len(p)):
    if p_sunuke + p[i] <= p_half:
        p_sunuke += p[i]
    else:
        p_araiguma += sum(p[i:])
        break

m_sunuke = 0
m_araiguma = 0

for i in range(len(m)):
    if m_araiguma + m[i] >= m_half:
        m_araiguma += m[i]
    else:
        m_sunuke += sum(m[i:])
        break

print(abs((p_sunuke + m_sunuke) - (p_araiguma + m_araiguma)))

Submission status: Wrong Answer

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

total=sum(a)

ans=[]
x=0
for i in range(n-1):
    x+=a[i]
    y=total-x
    print(x,y)
    ans.append(abs(x-y))

print(min(ans))

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [int(0)] * N
l = list(map(int, input().split()))
a = [l[0],l[1]]
x=l[0]
y=l[1]
p=0
for i in range(2,N):
    a.append(l[i])
    y+=l[i]
    while abs(x-y)>abs((x+l[p+1])-(y-l[p+1])) and p+1<N:
        x+=l[p+1]
        y-=l[p+1]
        p+=1
print(abs(x-y))

Submission status: Wrong Answer

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

total = sum(a)

acc_sum = [0] * N
base = 0
for i in range(N):
    base += a[i]
    acc_sum[i] = base

diff = [abs(acc * 2 - total) for acc in acc_sum]
print(min(diff))

Submission status: Wrong Answer

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

r_index = N - 1
l_index = 1
min_diff = None
pre_index = None

while True:
    index = (l_index + r_index) // 2
    l_sum = sum(a_list[:index])
    r_sum = sum(a_list[index:])
    diff = abs(l_sum - r_sum)
    min_diff = diff if min_diff == None else min(min_diff, diff)

    if pre_index == None:
        pass
    elif index == pre_index:
        break

    if l_sum < r_sum:
        l_index = index
    else:
        r_index = index + 1

    pre_index = index

print(min_diff)

Submission status: Wrong Answer

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

sunuke = sum(a[1:])
raccoon = a[0]
minimum = abs(sunuke - raccoon)
for i in a:
    if(abs(sunuke - raccoon) < minimum):
        minimum = abs(sunuke - raccoon)
    sunuke -= i
    raccoon += i
       
print(minimum)

Submission status: Wrong Answer

Solution code:
n= int(input())
card = input().split(' ')
card = [int(s) for s in card]
min_s = 20000000
sum1 = sum(card[:1])
sum2 = sum(card[1:])
for i in range(n - 1):
    dif  = abs(sum1-sum2)

    if dif  < min_s:
        min_s = dif
    sum1 += card[i+1]
    sum2 = sum2 - card[i+1]
print(min_s)

Submission status: Wrong Answer

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

sunuke = sum(a[1:])
raccoon = a[0]
minimum = abs(sunuke - raccoon)
print(minimum)
for i in a:
    if(abs(sunuke - raccoon) < minimum):
        minimum = abs(sunuke - raccoon)
    sunuke -= i
    raccoon += i
       
print(minimum)

Submission status: Wrong Answer

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

r_index = N - 1
l_index = 1
min_diff = None
pre_diff = None

while True:
    index = (l_index + r_index) // 2
    l_sum = sum(a_list[:index])
    r_sum = sum(a_list[index:])
    diff = abs(l_sum - r_sum)
    min_diff = diff if min_diff == None else min(min_diff, diff)

    if pre_diff == None:
        pass
    elif pre_diff == diff:
        break

    if l_sum < r_sum:
        l_index = index
    else:
        r_index = index + 1

    pre_diff = diff

print(min_diff)

Submission status: Wrong Answer

Solution code:
n= int(input())
card = input().split(' ')
card = [int(s) for s in card]
min_s = 20000000
sum1 = sum(card[:1])
sum2 = sum(card[1:])
for i in range(n - 1):
    dif  = abs(sum1-sum2)
    print(dif)
    if dif  < min_s:
        min_s = dif
    sum1 += card[i+1]
    sum2 = sum2 - card[i+1]
print(min_s)

Submission status: Wrong Answer

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

m = []
p = []

for x in a:
    if x < 0:
        m.append(x)
    else:
        p.append(x)

m.sort(reverse=True)
p.sort()

m_half = sum(m) // 2
p_half = sum(p) // 2

p_sunuke = 0
p_araiguma = 0

for i in range(len(p)):
    if p_sunuke + p[i] <= p_half:
        p_sunuke += p[i]
    else:
        p_araiguma += sum(p[i:])
        break

m_sunuke = 0
m_araiguma = 0

for i in range(len(m)):
    if m_araiguma + m[i] >= m_half:
        m_araiguma += m[i]
    else:
        m_sunuke += sum(m[i:])
        break

print(abs((p_sunuke + m_sunuke) - (p_araiguma + m_araiguma)))

Submission status: Wrong Answer

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

total = sum(a)
half = total / 2.0

base = 0
diff = half
for i in range(len(a)):
    base += a[i]
    diff_n = abs(base - half)
    if diff_n > diff:
        print(abs(sum(a[:i]) * 2 - total))
        break
    diff = diff_n

Submission status: Wrong Answer

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

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

SUM_CARD = sum(a)

i=1

diff_sum = 0
sunuke = sum(a[0:i])
diff_sum = abs(sunuke*2-SUM_CARD)
i+=1

while(i<N):
    sunuke = sum(a[1:i])
    diff_sum_new = abs(sunuke*2-SUM_CARD)
    if diff_sum_new<diff_sum:
        diff_sum = diff_sum_new
    i += 1

print(diff_sum)

Submission status: Wrong Answer

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

total = sum(a)
half = total / 2.0

base = 0
diff = half
for i in range(len(a)):
    base += a[i]
    diff_n = abs(base - half)
    if diff_n > diff:
        print(abs(sum(a[:i]) * 2 - total))
        break

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

ls = []
A = sum(ax[:1])
B = sum(ax[1:])

for i in range(1, N):
    sub = abs(A-B)
    ls.append(sub)
    if(sub is 0):
        break
    print(A)
    print(B)
    A += ax[i]
    B -= ax[i]

print(sorted(ls)[0])

Submission status: Wrong Answer

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

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

SUM_CARD = sum(a)
print(SUM_CARD)

i=1

diff_sum = 0
sunuke = sum(a[0:i])
diff_sum = abs(sunuke*2-SUM_CARD)
i+=1

while(i<N):
    sunuke = sum(a[0:i])
    print(sunuke)
    diff_sum_new = abs(sunuke*2-SUM_CARD)
    print(diff_sum_new)
    if diff_sum_new<diff_sum:
        diff_sum = diff_sum_new
    i += 1
print(diff_sum)

Submission status: Wrong Answer

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

#すぬけくんとアライグマは N 枚のカードの山を作りました。
#カードの山の上から i 番目のカードには整数 ai が書かれています。
#N 枚のカードを分け合うことにしました。
#すぬけくんがカードの山の上から何枚かのカードを取ったあと、
#アライグマは残ったカード全てを取ります。
#このとき、すぬけくんもアライグマも 1 枚以上のカードを取る必要があります。
#
#すぬけくんとアライグマが持っているカードに書かれた数の総和をそれぞれ x,y として、
#|x−y| を最小化したいです。 |x−y| としてありうる値の最小値を求めなさい。

N = int(input())
A = input().split()
#N = 6
#A = [1, 2, 3, 4, 5, 6]
#N = 2
#A = [10, -10]

B = []
all_add = 0

for i in range(N):
    B.append(int(A[i]))
    all_add = all_add + B[i]

x = 0

calk = 0
ch = 10000000000

for i in range(N):
    x = B[i]
    calk = abs(2 * x - all_add)
        
    if calk < ch:
        ch = calk
    
    if N > 2:
        
        for j in range(N-i-1):
            x = x + B[i + j + 1]
            calk = abs( 2 * x - all_add)
     
            if calk < ch:
                ch = calk

print(ch)

Submission status: Wrong Answer

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

sunuke = sum(a)
raccoon = 0
minimum = abs(sunuke - raccoon)
for i in a:
    sunuke -= i
    raccoon += i
    if(abs(sunuke - raccoon) < minimum):
        minimum = abs(sunuke - raccoon)
print(minimum)

Submission status: Wrong Answer

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

j = N // 2
l = N // 4
flag = 1

while True:
    left = a[:j]
    right = a[j:]
    sumL = sum(left)
    sumR = sum(right)
    if sumL > sumR:
        j = j - l
    elif sumL < sumR:
        j = j + l
    else:
        print(0)
        flag = 0
    l //= 2
    if l == 0:
        break

if flag:
    c1 = abs(sum(a[: j]) - sum(a[j :]))
    c2 = abs(sum(a[: j-1]) - sum(a[j-1 : ]))
    c3 = abs(sum(a[: j+1]) - sum(a[j+1 : ]))
    if j - 1 == 0:
        c2 = c1
    if j + 1 == N:
        c3 = c1   
    print(min(c1, c2, c3))

Submission status: Wrong Answer

Solution code:
# encoding:utf-8

def main():
	N = int(input())
	cards = list(map(int, input().split()))
	print(abs(BinarySearch(calcDif, cards, 0)))

def BinarySearch(func, cards, target):
	left = 1
	left_val = func(cards, left)
	right = len(cards) - 1
	right_val = func(cards, right)
	
	if right - left == 0:
		return(abs(func(cards, 1)))
	elif right - left == 1:
		return(min(abs(func(cards, 1)), abs(func(cards, 2))))

	while right - left >= 1:
		mid = int((left + right)/2)
		mid_val = func(cards, mid)

		if mid == 0:
			return(abs(func(cards, 1)))
		elif mid == len(cards) - 1:
			return(abs(func(cards, len(cards) - 2)))

		# print(left, mid, right)
		# print(left_val, mid_val, right_val)
		if mid_val == target:
			return(abs(mid_val))
		elif mid_val > target:
			right = mid
			right_val = mid_val
		elif mid_val < target:
			left = mid + 1
			left_val = mid_val

		if mid == left:
			break

	return(min(abs(mid_val), abs(func(cards, mid + 1))))

def calcDif(cards, k):
	# k >= 1
	x = cards[:k]
	y = cards[k:len(cards)]
	return(sum(x) - sum(y))

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
len = len(a)
x = 0
y = 0
for i in range(len):
    x += a[i]
    y = 0
    for j in range(i+1, len):
        y += a[j]
    if i == 0:
        diff = abs(y-x)
    if diff > (y -x) and y > x:
        diff = abs(y-x)
    elif diff > (x -y) and x > y:
        diff = x -y 
print(diff)

Submission status: Wrong Answer

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

sunuke = sum(a)
raccoon = 0
minimum = abs(sunuke - raccoon)
for i in a:
    sunuke -= i
    raccoon += i
    if(abs(sunuke - raccoon) > minimum):
        break
    minimum = abs(sunuke - raccoon)
print(minimum)

Submission status: Wrong Answer

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

r_index = N - 1
l_index = 1
index = (r_index + l_index) // 2
min_diff = abs(sum(a_list[index:]) - sum(a_list[:index]))
pre_diff = None

while True:
    index = (r_index + l_index) // 2
    l_sum = sum(a_list[index:])
    r_sum = sum(a_list[:index])
    diff = abs(l_sum - r_sum)
    min_diff = min(min_diff, diff)

    if pre_diff == None:
        pass
    elif pre_diff == diff:
        break

    if l_sum > r_sum:
        l_index = index + 1
    else:
        r_index = index

    pre_diff = diff

print(min_diff)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

min = int(1e9 + 7)
temp = 0

a_cumsum = a.cumsum()
a_sum = a.sum()

for i in range(N-1):
    sunuke = a_cumsum[i]
    kuma = a_sum - sunuke
    temp = np.absolute(sunuke - kuma)
    if temp <= min:
        min = temp
        if min == 0:
            break

print(min)

Submission status: Wrong Answer

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

sunuke = sum(a)
raccoon = 0
minimum = abs(sunuke - raccoon)
for i in a:
    if(abs(sunuke - raccoon) > minimum):
        break
    minimum = abs(sunuke - raccoon)
    sunuke -= i
    raccoon += i
print(minimum)

Submission status: Wrong Answer

Solution code:
N = map(int, raw_input().split())
a = map(int, raw_input().split())
xsum = 0
for i in range(len(a)-1):
    xsum += a[i]
dif = xsum - a[len(a)-1]
m = dif
for i in range(1,len(a)-1)[::-1]:
    dif -= 2*a[i]
    if m > abs(dif):
        m = dif
print(abs(m))

Submission status: Wrong Answer

Solution code:
n= int(input())
card = input().split(' ')
card = [int(s) for s in card]
min_s = 2000004
sum1 = sum(card[:1])
sum2 = sum(card[1:])
for i in range(n - 1):
    dif  = abs(sum1-sum2)
    if dif  < min_s:
        min_s = dif
    sum1 += card[i+1]
    sum2 = sum2 - card[i+1]
print(min_s)

Submission status: Wrong Answer

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

def solve():
    if N == 2:
        return abs(src[0] - src[1])
    if N == 3:
        return min(abs(src[0]+src[1]-src[2]), abs(src[0]-src[1]-src[2]))
    ans = float('inf')
    l = 1
    h = N-1
    for i in range(20):
        m = (l+h)//2
        df1 = sum(src[:m]) - sum(src[m:])
        df2 = sum(src[:m+1]) - sum(src[m+1:])
        ans = min(ans, abs(df1), abs(df2))
        if ans == 0:
            return 0
        elif df1 < 0:
            l = m
        else:
            h = m
    return ans
print(solve())

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
Cards = list(map(int, input().split()))
B = sum(Cards)
A = 0
answer = 100000000

if N==2:
    print(abs(Cards[0] - Cards[1]))
    sys.exit()
for number in Cards:
    A += number
    B -= number
    if abs(A-B) < answer:
        answer = abs(A-B)

print(answer)

Submission status: Wrong Answer

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

if N==2:
  print abs(a[1]-a[0])
elif N==3:
  a1 = abs(a[2] - a[1] - a[0])
  a2 = abs(a[2] + a[1] - a[0])
  ans = a1 if a1 < a2 else a2
  print ans
else:
  select = N/2
  diff_prev = abs(sum(a[:select]) - sum(a[select:]))
  diff_plus = abs(sum(a[:select+1]) - sum(a[select+1:]))
  diff_minus = abs(sum(a[:select-1]) - sum(a[select-1:]))
  if diff_prev - diff_minus < diff_prev - diff_plus:
    sign = 1
  else:
    sign = -1

  while True:
    select += sign
    diff = abs(sum(a[select:]) - sum(a[:select]))
    if diff_prev > diff:
      diff_prev = diff
    else:
      print(diff_prev)
      break

Submission status: Wrong Answer

Solution code:
if(__name__ == "__main__"):
    N = int(input())
    a = [int(i) for i in input().split()]
    #print(a,b)
    
    #print(maxlist)
    distmin = 900000
    for i in range(1,N):
        sunuke = a[:i]
        arai = a[i:]
        dist = abs(sum(sunuke) - sum(arai))
        if(distmin > dist):
            distmin = dist
    
    print(distmin)

Submission status: Wrong Answer

Solution code:
def calc(x,y):
    z = x+y
    b.append(z)
    return z
from functools import reduce
import numpy as np
N = int(input())
a = list(map(int, input().split()))
s = sum(a)
b = []
reduce(calc ,a)
print(min(abs(2 * np.array(b) -s )))

Submission status: Wrong Answer

Solution code:
N = map(int, raw_input().split())
a = map(int, raw_input().split())
xsum = 0
for i in range(len(a)-1):
    xsum += a[i]
dif = xsum - a[len(a)-1]
m = dif
for i in range(1,len(a)-1)[::-1]:
    prev = dif
    dif -= 2*a[i]
    if abs(prev) > abs(dif):
        m = dif
print(abs(m))

Submission status: Wrong Answer

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

if N==2:
  print a[1]-a[0]
elif N==3:
  a1 = a[2] -a[1] - a[0]
  a2 = a[2] + a[1] - a[0]
else:
  select = N/2
  diff_prev = abs(sum(a[:select]) - sum(a[select:]))
  diff_plus = abs(sum(a[:select+1]) - sum(a[select+1:]))
  diff_minus = abs(sum(a[:select-1]) - sum(a[select-1:]))
  if diff_prev - diff_minus < diff_prev - diff_plus:
    sign = 1
  else:
    sign = -1

  while True:
    select += sign
    diff = abs(sum(a[select:]) - sum(a[:select]))
    if diff_prev > diff:
      diff_prev = diff
    else:
      print(diff_prev)
      break

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
Cards = list(map(int, input().split()))
B = sum(Cards)
A = 0
answer = 100000000

if N==2:
    print(abs(Cards[0] - Cards[1]))
    sys.exit()
for number in Cards:
    A += number
    B -= number
    if abs(A-B) < answer:
        answer = abs(A-B)
    else:
        break

print(answer)

Submission status: Wrong Answer

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

s = input().split()
a = [int(i) for i in s]

L = [0] * N
L[0] = a[0] - sum(a[1 : ])

if N == 2 : 
    print(abs(a[0] - a[1]))
else : 
    MIN = float("inf")
    for i in range(1 , N - 1 ) : 
        L[i] = L[i - 1] + 2 * a[i]
        if L[i] * L[i - 1] <= 0 : 
            if MIN > min( abs(L[i]) , abs(L[i - 1])) : MIN = min( abs(L[i]) , abs(L[i - 1]))
    print(MIN)

Submission status: Wrong Answer

Solution code:
n= int(input())
card = input().split(' ')
card = [int(s) for s in card]
min_s = 2000004
for i in range(n -1):
    dif  =abs(sum(card[:i+1]) - sum(card[i+1:]))
    if dif  < min_s:
        min_s= dif
    
print(min_s)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

min = int(1e9 + 7)
temp = 0

asum = a.sum()

for i in range(1, N):
    kuma = a[:i].sum()
    sunuke = asum - kuma
    temp = np.absolute(kuma - sunuke)
    if temp <= min:
        min = temp
        if min == 0:
            break

print(min)

Submission status: Wrong Answer

Solution code:
N = int(input())
i = [int(i) for i in input().split()]
imax = sum(i)
i0 = 0
tr = 0
while imax/2 > i0:
    i0 = i0+i[tr]
if i0 == 0:
    i0 = i[0]
print(abs((imax-i0)-i0))

Submission status: Wrong Answer

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

j = N // 2
l = N // 4

while True:
    left = a[:j]
    right = a[j:]
    sumL = sum(left)
    sumR = sum(right)
    if sumL > sumR:
        j = j - l
    elif sumL < sumR:
        j = j + l
    else:
        print(0)
        sys.exit()
    l //= 2
    if l == 0:
        break
c1 = abs(sum(a[: j]) - sum(a[j :]))
c2 = abs(sum(a[: j-1]) - sum(a[j-1 : ]))
c3 = abs(sum(a[: j+1]) - sum(a[j+1 : ]))
if j - 1== 0:
    c2 = c1
if j + 1 == N:
    c3 = c1   
print(min(c1, c2, c3))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
s.sort()
a = 0
b = 0
 
for i in range(n) :
    if a <= b :
        a = a + abs(s[n-1-i])
    else :
        b = b + abs(s[n-1-i])
 
print (abs(a-b))

Submission status: Wrong Answer

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

s = input().split()
a = [int(i) for i in s]

L = [0] * N
L[0] = a[0] - sum(a[1 : ])

if N == 2 : 
    print(abs(a[0] - a[1]))
else : 
    for i in range(1 , N ) : 
        L[i] = L[i - 1] + 2 * a[i]
        if L[i] > 0 : break
    #i か i - 1
    print(min( abs(L[i]) , abs(L[i - 1])))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(1, N):
    res += a[i]
diff = a[0]-a[i]
abs_ = abs(diff)
for i in range(1, N-1):
    diff += 2*a[i]
    abs_ = min(abs_, abs(diff))

print(abs_)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

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

min = int(1e9 + 7)
temp = 0

for i in range(1, N):
    temp = np.absolute(a[:i].sum() - a[i:].sum())
    if temp <= min:
        min = temp

print(min)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
s.sort()
a = 0
b = 0

for i in range(n) :
    if a <= b :
        a = a + s[n-1-i]
    else :
        b = b + s[n-1-i]

print (abs(a-b))

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
l = [int(i) for i in input().split()]
A = []

for i in range(2):
	A.append(max(l))
	l.pop(l.index(max(l)))

print(int(math.fabs(A[0]-A[1])))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = input().split()
a = []
for x in A:
 a.append(int(x))
a.sort()
for i in range(n):
 l = sum(a[:n-i-1])
 r = sum(a) - l
 if i == 0:
  min = abs(l - r)
 elif i >= n //2:
  break
 elif min > abs(l-r):
  min = abs(l-r)
print(min)

Submission status: Wrong Answer

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

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

aa = a[1:N-1]
if len(aa) == 0:
    aa.append(0)
for ai in aa:
    news = s - 2*ai
    if (news<=0):
        break
    s = news
print( min(abs(s), abs(news)) )

Submission status: Wrong Answer

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

def solve():
    if len(src) == 2:
        return abs(src[0] - src[1])
    ans = float('inf')
    l = 1
    h = N-1
    while h-l > 1:
        m = (l+h)//2
        df = sum(src[:m]) - sum(src[m:])
        ans = min(ans, abs(df))
        if df == 0:
            return 0
        elif df < 0:
            l = m
        else:
            h = m
    return ans
print(solve())

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
Cards = list(map(int, input().split()))
gokei = sum(Cards)
A = 0

for i in Cards:
    A += i
    if A >= math.floor(gokei/2):
        break

B = gokei - A
print(abs(A - B))

Submission status: Wrong Answer

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

a.sort()

s = sum(a) // 2

sunuke = 0
count = 0

for i in range(len(a)):
    if sunuke + a[i] <= s and i < len(a) - 1:
        sunuke += a[i]
    else:
        count = i
        break

araiguma = sum(a[count:])

print(abs(sunuke - araiguma))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(1, N):
    res += a[i]
diff = a[0]-a[i]
abs_ = abs(diff)
for i in range(1, N-1):
    diff += 2*a[i-1]
    abs_ = min(abs_, abs(diff))

print(abs_)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
a_sum = sum(a)
dif = abs(a_sum - 2 *a[0])
ans = dif

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

print(ans)

Submission status: Wrong Answer

Solution code:

s = input().split()
s = [int(i) for i in s]
N = s[0]

s = input().split()
a = [int(i) for i in s]

L = [0] * N
L[0] = a[0] - sum(a[1 : ])

if N == 2 : 
    print(abs(a[0] - a[1]))
else : 
    for i in range(1 , N - 1) : 
        L[i] = L[i - 1] + 2 * a[i]
        if L[i] > 0 : break
    print(L)
    #i か i - 1
    print(min( abs(L[i]) , abs(L[i - 1])))

Submission status: Wrong Answer

Solution code:
n= int(input())

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

m = 500000
for suke_n in range(1, n-1):
    araiguma_n = n - suke_n
    suke_sum = sum(a[0:suke_n])
    araiguma_sum = sum(a[suke_n:n])
    ans = abs(suke_sum - araiguma_sum)
    if m > ans:
        m = ans

print(m)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split(" ")]
a_sum = sum(a)
ans = 100000000000

for i in range(1,N):
    x = sum(a[0:i])
    y = a_sum - x
    tmp = abs(x-y)
    if ans >= tmp :
        ans = tmp
    else :
        break
print(ans)

Submission status: Wrong Answer

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

if len(a) > 2:
    a.sort()

    s = sum(a) // 2

    sunuke = 0
    count = 0

    for i in range(len(a)):
        if sunuke + a[i] <= s:
            sunuke += a[i]
        else:
            count = i
            break

    araiguma = sum(a[count:])

    print(abs(sunuke - araiguma))
else:
    print(abs(a[0] - a[1]))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
dif = 0
ans = 10**9

for i in range(1, N):
	dif = abs(sum(a[:i]) - sum(a[i:]))
	if dif < ans:
		ans = dif

print(ans)

Submission status: Wrong Answer

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

Submission status: Time Limit Exceeded

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

final = 10**32
for i in range(1, len(total)):
    a = sum(total[:i])
    b = sum(total[i:])
    final = min(final, abs(a - b))

print(final)

Submission status: Time Limit Exceeded

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