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=[int(i) for i in input().split()]
X=0
Y=0
min=10**9+1
for i in range(N):
  X=X+a[i]

for j in range(N-2):
  Y=Y+a[j]
  if abs(X-2*Y) <= min:
   min=abs(X-2*Y)
print(min)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
X=0
Y=0
min=10**9+1
for i in range(N):
  X=X+a[i]

for j in range(N-1):
  Y=Y+a[j]
#  print(abs(X-2*Y))
  if abs(X-2*Y) <= min:
   min=abs(X-2*Y)
print(min)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
X=0
Y=0
min=10**9+1
for i in range(N):
  X=X+a[i]

for j in range(N-1):
 for i in range(j+1):
  Y=Y+a[i]
  if abs(X-2*Y) <= min:
   min=abs(X-2*Y)
print(min)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
X=0
Y=0
min=10**9+1
for i in range(N):
  X=X+a[i]

for j in range(N-1):
 for i in range(j):
  Y=Y+a[i]

  if abs(X-2*Y) <= min:
   min=abs(X-2*Y)
print(min)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
X=0
Y=0
min=10**9+1
for i in range(N):
  X=X+a[i]

for j in range(N):
 for i in range(j):
  Y=Y+a[i]

  if abs(X-2*Y) <= min:
   min=abs(X-2*Y)
print(min)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://abc067.contest.atcoder.jp/tasks/arc078_a

"""
import sys
from sys import stdin
import bisect
input = stdin.readline


def solve(cards):
    grand_total = sum(cards)
    # 上から順番にカードを引いていき、できるだけ合計の半分に近づける
    snuke_total = cards[0]      #  すぬけ君は最低でも1枚は引く必要がある
    prev_diff = abs(snuke_total - (grand_total - snuke_total))
    for i, c in enumerate(cards[1:-1], start=1):       #  アライグマは最低1枚は引く必要があるので、最後のカードはすぬけ君には渡さない
        curr_diff = abs((snuke_total+c) - (grand_total - (snuke_total+c)))
        if curr_diff > prev_diff:
            break
        else:
            snuke_total += c
            prev_diff = curr_diff

    # print('grand total: {}'.format(grand_total))
    # print('snuke: {}'.format(snuke_total))
    return abs(snuke_total - (grand_total - snuke_total))




def main(args):
    N = int(input())
    cards = [int(x) for x in input().split()]
    result = solve(cards)
    print(result)



if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://abc067.contest.atcoder.jp/tasks/arc078_a

"""
import sys
from sys import stdin

input = stdin.readline


def solve(cards):
    grand_total = sum(cards)
    target = grand_total // 2

    # 上から順番にカードを引いていき、できるだけ合計の半分に近づける
    snuke_total = cards[0]      #  すぬけ君は最低でも1枚は引く必要がある
    prev_diff = abs(target - snuke_total)
    for c in cards[1:-1]:       #  アライグマは最低1枚は引く必要があるので、最後のカードはすぬけ君には渡さない
        curr_diff = abs(target - snuke_total - c)
        if curr_diff > prev_diff:
            break
        else:
            snuke_total += c
            prev_diff = curr_diff
    return abs((grand_total - snuke_total) - snuke_total)



def main(args):
    N = int(input())
    cards = [int(x) for x in input().split()]
    result = solve(cards)
    print(result)



if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from itertools import product, groupby
from math import pi
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 20


def main():
    N = int(input())
    a_list = list(map(int, input().split()))
    total = sum(a_list)

    dp1 = [0] * N
    dp1[0] = a_list[0]
    for i in range(1, N):
        dp1[i] = a_list[i] + dp1[i - 1]

    ans = INF
    for i in range(N - 1):
        ans = min(ans, abs(total - dp1[i]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
card = [int(i) for i in input().split(" ")]
 
all = 0
for i in card:
    all += i
 
a = all
ab = all
for i in range(0,len(card)):
    b = a - card[len(card) - i - 1]
    if abs(a - b) > ab:
        break
    a,ab = b,abs(a - b)
print(ab)

Submission status: Wrong Answer

Solution code:
def solve():  # 068C - Splitting Pile
    N = int(input())
    a = [int(x) for x in input().split(" ")]
    min_sum = 10000

    for i in range(N - 1):
        if abs(sum(a[:i + 1]) - sum(a[i + 1:N])) < min_sum:
            min_sum = abs(sum(a[:i + 1]) - sum(a[i + 1:N]))
    print(min_sum)


solve()

Submission status: Wrong Answer

Solution code:
input()
param = sorted(map(int, input().split(" ")))
m = len(param)-1
r1 = list()
r2 = list()
for v in param:
  if sum(r1) < sum(r2):
    r1.append(param[len(r1)])
  else:
    r2.append(param[m-len(r2)])
print(abs(sum(r1)-sum(r2)))

Submission status: Wrong Answer

Solution code:
input()
param = sorted(map(int, input().split(" ")))
m = len(param)-1
r1 = list()
r2 = list()
for v in param:
  if sum(r1) < sum(r2):
    r1.append(param[len(r1)])
  else:
    r2.append(param[m-len(r2)])
print(r1, r2)

print(abs(sum(r1)-sum(r2)))

Submission status: Wrong Answer

Solution code:
n=input()
a=list(map(int,input().split()))
an=[]
def aaa(i):
        b=a[i:]
        c=a[:i]
        x=sum(b)
        y=sum(c)
        return x-y
for i in range(len(a)):
    an+=[aaa(i)]
an=[abs(x) for x in an]
print(min(an))

Submission status: Wrong Answer

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

b = a[0]
c = a[-1]

d = 1
e = -2

for i in range(n-2):
    if abs(b + a[d] - c) < abs(b - c - a[e]):
        b += a[d]
        d += 1
    else:
        c += a[e]
        e -= 1

print(abs(b-c))

Submission status: Wrong Answer

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

x = a[0]
y = a[N - 1]
s = 1
r = N - 2

while s <= r:
    p1 = abs((x + a[s]) - y)
    p2 = abs(x - (y + a[r]))
    if p1 <= p2:
        x += a[s]
        s += 1
    else:
        y += a[r]
        r -= 1

print(abs(x - y))

Submission status: Wrong Answer

Solution code:
N= int(input())

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

x = 0
sum = sum(A)
ans = 10*25

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

print(ans)

Submission status: Wrong Answer

Solution code:
N= int(input())

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

x = 0
sum = sum(A)
ans = 10*20

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

print(ans)

Submission status: Wrong Answer

Solution code:
N= int(input())

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

x = 0
sum = sum(A)
ans = 10*15

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

print(ans)

Submission status: Wrong Answer

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

b = []
diff = 1000000


for i in range(N-1):
    b.append(a.pop())
    if abs(sum(b)-sum(a)) < diff :
        diff = abs(sum(b) - sum(a))



print(diff)

Submission status: Wrong Answer

Solution code:
from math import * 
n=int(input())
l=[int(x) for x in input().split()]
summ=sum(l)
sum1=0
ans=10**30
for i in range(n):
    sum1+=l[i]
    ans=min(ans,abs(summ-2*sum1))
print(ans)

Submission status: Wrong Answer

Solution code:
from math import * 
n=int(input())
l=[int(x) for x in input().split()]
summ=sum(l)
sum1=0
ans=10**20
for i in range(n):
    sum1+=l[i]
    ans=min(ans,abs(summ-2*sum1))
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()

card_list = list(map((int),input().split(' ')))

sum_card = sum(card_list)

score = []
araiguma = 0

for i in range(int(a)-1):
    
    araiguma += sum_card - card_list[i]
    score.append(abs(sum_card-2*araiguma))

print(min(score))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
X=sum(a)
ans=[]
for i in range(N-1):
    A=2*a[i]
    if i ==0:
        ans.append(abs(X-A))
    else:
        B=abs(ans[i-1]-A)
        ans.append(B)
ans1=min(ans)
print(ans1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
X=sum(a)
ans=[]
for i in range(N-1):
    A=2*a[i]
    if i ==0:
        ans.append(X-A)
    else:
        B=abs(ans[i-1]-A)
        ans.append(B)
ans1=min(ans)
print(ans1)

Submission status: Wrong Answer

Solution code:
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
X=sum(a)
ans=[]
for i in range(N-1):
    A=2*a[i]
    if i ==0:
        ans.append(X+A)
    else:
        B=ans[i-1]+A
        ans.append(B)
ans1=min(ans)
print(ans1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
X=sum(a)
ans=[]
for i in range(N-1):
    A=abs(X-2*a[i])
    if i ==0:
        ans.append(A)
    else:
        B=ans[i-1]+A
        ans.append(B)
ans1=min(ans)
print(ans1)

Submission status: Wrong Answer

Solution code:

import math
a = input()

b = list(map((int),input().split(' ')))

acc = 0
sunuke = 0
other = []
sum_b = sum(b)
half_b = math.floor(sum_b / 2)

for i in range(int(a)):
    
    
    acc += i+1
    if acc <= half_b:
        sunuke = acc
    else:
        other.append(i+1)
        


araiguma = sum(other)
sub = sunuke - araiguma
print(abs(sub))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
X=sum(a)
ans=[]
for i in range(N):
    A=abs(X-2*sum(a[0:i+1]))
    ans.append(A)
ans1=min(ans)
print(ans1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
sum_a=abs(sum(a))
ans=sum_a%2
if sum_a==0:
    A=map(abs,a)
    B=list(A)
    ans2=min(B)
    print(ans2*2)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
sum_a=abs(sum(a))
ans=sum_a%2
if sum_a==0:
    ans2=abs(min(a))
    print(ans2*2)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
N=int(input())
b=input()
c=b.split(" ")
d=map(int,c)
a=list(d)
sum_a=abs(sum(a))
ans=sum_a%2
if ans==0:
    ans2=abs(min(a))
    print(ans2*2)
else:
    print(ans)

Submission status: Wrong Answer

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

sum_a =  sum(a)

minv = abs(2*sum(a[:1]) - sum_a)
cc = a[0]

for i in range(1, N):
    
    cc += a[i]
    tmp = abs(2*cc - sum_a)
#     print(tmp)
    
    if minv > tmp:
        minv = tmp
        
print(minv)

Submission status: Wrong Answer

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

sum_a =  sum(a)

minv = abs(2*sum(a[:1]) - sum_a)
cc = 0

for i in range(2, N):
    
    cc += sum(a[:i])
    tmp = abs(2*cc - sum_a)
    
    if minv > tmp:
        minv = tmp
        
print(minv)

Submission status: Wrong Answer

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

import sys

N = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split(' ')))

snk = 0
arg = 0

for i in range(0, N):
	arg += a[i]

s = 1000000000

for i in range(0, N - 1):
	snk += a[i]
	arg -= a[i]
	ss = snk - arg
	if ss < 0:
		ss *= -1

	if s > ss:
		s = ss

print(s)

Submission status: Wrong Answer

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

import sys

N = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split(' ')))

snk = 0
arg = 0

for i in range(0, N):
	arg += a[i]

s = arg

for i in range(0, N - 1):
	snk += a[i]
	arg -= a[i]
	ss = snk - arg
	if ss < 0:
		ss *= -1

	if s > ss:
		s = ss

print(s)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
result = abs(a[0] - sum(a[1:]))
for i in range(n):
    d = abs(sum(a[0:i]) - sum(a[i:]))
    if d<result:
        result = d
print(result)

Submission status: Wrong Answer

Solution code:
import math as ma
N = int(input())
a = list(map(int, input().split()))
s = sum(a)
A = 0
diff = 9999999999
for i in range(N):
    A += a[i]
    if diff > (ma.fabs(2*A - s)):
        diff = ma.fabs(2*A -s)
print(int(diff))

Submission status: Wrong Answer

Solution code:
import math as ma
N = int(input())
a = list(map(int, input().split()))
s = sum(a)
A = 0
diff = 9999999999
for i in range(N):
    A += a[i]
    if diff > (ma.fabs(2*A - s)):
        diff = ma.fabs(2*A -s)
print(diff)

Submission status: Wrong Answer

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

S = sum(A)
res = abs(S - 2 * A[0])
flag = 1
while res != 0 and flag < N - 1:
    S -= 2 * A[flag]
    if res > abs(S):
        res = abs(S)
    flag += 1
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
nus = 0
arai = N - 2
sumnus = A[0]
sumarai = A[-1]
while nus != arai:
    if sumnus < sumarai:
        nus += 1
        sumnus += A[nus]
    else:
        sumarai += A[arai]
        arai -= 1

print(abs(sumnus - sumarai))

Submission status: Wrong Answer

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

mini = 1000000000
x = a[0]
for i in range(N):
    x += a[i]

    tmp = abs(sum(a) - (2 * x))
    if tmp < mini:
        mini = tmp

print(mini)

Submission status: Wrong Answer

Solution code:
N = int(input())
cards = list(map(int, input().split()))
ans = float("inf")
y = sum(cards)
x = 0
L = []

for card in cards:
    x += card
    y -= card
    L.append(abs(x-y))

L = L[1:N]    
print(min(L))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
cards = list(map(int, input().split()))
y = sum(cards[1:])
x = cards[0]
ans = float("inf")
for card in cards:
    x += card
    y -= card
    if(abs(x-y) < ans):
        ans = abs(x-y)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cards = list(map(int, input().split()))
y = sum(cards[1:])
x = cards[0]
ans = float("inf")
for card in cards:
    print(card)
    x += card
    y -= card
    if(abs(x-y) < ans):
        ans = abs(x-y)
print(ans)

Submission status: Wrong Answer

Solution code:
def mainC():
    #import numpy as np
    N = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    m = float('inf')
    d2 = a[0]
    for i in range(1, N-1):
        d2 += a[i]
        m = min( m, abs(s-2*d2) )
    print( m ) 

mainC()

Submission status: Wrong Answer

Solution code:
def mainC():
    #import numpy as np
    N = int(input())
    a = list(map(int, input().split()))
    s = sum(a)
    d1 = d2 = a[0]
    for i in range(1, N-1):
        d2 += a[i]
        if d2 > s//2:
            break
        d1 = d2
    print( min( abs(s-2*d1) , abs(s-2*d2) ) ) 

mainC()

Submission status: Wrong Answer

Solution code:
N = int(input())
l = [0] * N
l = list(map(int, input().split()))
sum = 0
a = [0] * N
for i in range(N):
    sum += l[i]
x = 0
result = 1145141919810
for i in range(N):
    x += l[i]
    result = min((result, abs(sum - 2 * x)))
print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
cards = list(map(int, input().split()))
x, y = 0, 0
ans = float("inf")
for i in range(1, N):
    x = sum(cards[:i])
    y = sum(cards[i:])
    print(x, y)
    if(abs(x-y) < ans):
        ans = abs(x-y)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cards = list(map(int, input().split()))
x, y = 0, 0
ans = float("inf")
for i in range(1, N-1):
    x = sum(cards[:i])
    y = sum(cards[i:])
    if(abs(x-y) < ans):
        ans = abs(x-y)
print(ans)

Submission status: Wrong Answer

Solution code:
m = int(input())
n = list(map(int, input().split()))
sunuke = 0
araiguma = sum(n)

min = 10000000000
for i in range(m - 1):
    araiguma -= n[i]
    sunuke += n[i]
    print(abs(sunuke - araiguma))
    if min > abs(sunuke - araiguma):
        min = abs(sunuke - araiguma)
print(min)

Submission status: Wrong Answer

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

import re
import sys

n = int(raw_input())
a = map(int, re.split('\s', raw_input()))
x = 0
y = 0

a.sort()
a.reverse()

if (len(a) == 2):
        print abs(a[0] - a[1])
        sys.exit()

for i in range(0, len(a)):
        if (a[i] > 0):
                if (x >= y):
                        y += a[i]
                else:   
                        x += a[i]
        else:   
                if (y >= x):
                        y += a[i]
                else:   
                        x += a[i]

print abs(x - y)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = map(int, raw_input().split())
x = 0
y = sum(a)
min = abs(x - y)
for i in range (0,N):
    y -= a[i]
    x += a[i]
    dummy = abs(x - y)
    if min > dummy:
        min = dummy
print min

Submission status: Wrong Answer

Solution code:
num = int(input())
numList = list(map(int, input().split()))

Slist = []
Alist = []
dif = sum(numList)

for i in range(num):
    Slist = numList[0:i]
    if i < num:
        Alist = numList[i+1:num-1]
    if dif > abs(sum(Slist) - sum(Alist)):
        dif = abs(sum(Slist) - sum(Alist))

print(dif)

Submission status: Wrong Answer

Solution code:
def sa(_a,_b):
    a = max(_a,_b)
    b = min(_a,_b)
    return a-b

n = int(input())
l = list(map(int,input().split()))
m = sum(l)
a = l[0]
b = m - a
ans = sa(a,b)
for i in range(1,len(l)-1):
    a += l[i]
    b -= l[i]
    ans = min(ans,sa(a,b))
    if a > b:
        break

print(ans)

Submission status: Wrong Answer

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

total=sum(a)
ans=N*10**9
b=0
for i in range(N-1):
    b += a[i]
    ans=min(ans,abs(total-2*b))

print(ans)

Submission status: Wrong Answer

Solution code:
p = lambda  x: x#print(x)

# スペース区切りの整数の入力
N = int(input())

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



p(N)
p(l)

s = l[0]
a = sum(l) - s
min = float('inf') 

p("s=" + str(s))
#p("min=" + str(min))
p("a=" + str(a))
p("abs(s - a)=" + str(abs(s - a)))


if N==2:
	min = abs(s - a)

else:
	for i in range(1, N):
		s += l[i]
		a -= l[i]
		if min > abs(s - a):
			min = abs(s - a)
			p("min=" + str(min))

print(str(min))

Submission status: Wrong Answer

Solution code:
p = lambda  x: x#print(x)

# スペース区切りの整数の入力
N = int(input())

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



p(N)
p(l)

s = l[0]
a = sum(l) - s
min = float('inf') 

p("s=" + str(s))
#p("min=" + str(min))
p("a=" + str(a))
p("abs(s - a)=" + str(abs(s - a)))


if N==2:
	min = abs(s - a)

else:
	for i in range(1, N):
		s += l[i]
		a -= l[i]
		if min > abs(s - a):
			min = abs(s - a)
			p("min=" + str(min))
		else:
			break

print(str(min))

Submission status: Wrong Answer

Solution code:
#Python

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


b=[]
ans=1000000001
for i in range(N-1):
	i=i+1
	#print "x::"+str(sum(a[:i]))
	#print "sum-2x::"+str( abs( sum(a) - sum(a[:i])*2 ) )

	if ans>abs( sum(a) - sum(a[:i])*2 ):
		ans= abs( sum(a) - sum(a[:i])*2 )


print ans

Submission status: Wrong Answer

Solution code:
a = int(input())
c = list(map(int,input().split()))
su = sum(c)
diff = 1e9
cc = 0
for x in range(a-1):
    cc += c[x]
    diff = min(diff,abs(su-2*cc))
print(diff)

Submission status: Wrong Answer

Solution code:
import sys
import math

N = input()

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

index = len(a) - 1
i = 0
while i != len(a):
    if a[i] >= 0 or index < 2:
        break
    else:
        if math.fabs(a[index]) >= math.fabs(a[i]):
            a[index] += a[i]
            a[i] = 0
            i += 1
        else:
            a[i] += a[index]
            a[index] = 0
            index -= 1

a.sort()
a.reverse()

comp1 = a[0]
comp2 = 0

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

ans = sys.maxint

for i in range(N - 1):
    if math.fabs(comp2 - comp1) < ans:
        ans = math.fabs(comp2 - comp1)
    elif math.fabs(comp2 - comp1) > ans:
       break

    comp2 -= a[i + 1]
    comp1 += a[i + 1]

print int(ans)

Submission status: Wrong Answer

Solution code:
def solution(a):
    if len(a) == 2:
        return abs(a[0] - a[1])
    
    total =sum(a)
    min_diff = total
    part_of_sum = 0
    for i in range(len(a)-1):
        part_of_sum += a[i]
        total -= a[i]
        if abs(part_of_sum - total) < min_diff:
            min_diff = abs(part_of_sum - total)

    return min_diff


N = int(input())
a = [int(value) for value in input().split(' ')]
print(solution(a))

Submission status: Wrong Answer

Solution code:
import sys
import math

N = input()

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

comp1 = a[0]
comp2 = 0

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

ans = sys.maxint

for i in range(N - 1):
    if math.fabs(comp2 - comp1) < ans:
        ans = math.fabs(comp2 - comp1)
    #elif math.fabs(comp2 - comp1) > ans:
       #break

    comp2 -= a[i + 1]
    comp1 += a[i + 1]

print int(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math

N = input()

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

comp1 = a[0]
comp2 = 0

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

ans = sys.maxint

for i in range(N - 1):
    if math.fabs(comp2 - comp1) < ans:
        ans = math.fabs(comp2 - comp1)
    elif math.fabs(comp2 - comp1) > ans:
       break

    comp2 -= a[i + 1]
    comp1 += a[i + 1]

print int(ans)

Submission status: Wrong Answer

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

l=[]
a = abs(s-2*b)
for i in range(1,N):
    b = b + an[i]
    a=min(a,abs(s-2*b))
print(a)

Submission status: Wrong Answer

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

l = 0
r = sum(a)

d = 10 ** 9 + 1

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

print(d)

Submission status: Wrong Answer

Solution code:
if __name__=='__main__':
    alls = int(input())
    num_src = input().split(' ')
    num_cards = []
    ans = 0
    for i in num_src:
        num_cards.append(int(i))
        ans += abs(int(i))
    num_cards.sort()
    num_cards.reverse()
    x = 0
    y = 0
    for i in num_cards:
        if x<y:
            x += i
        else:
            y += i
    if y<x:
        tmp = abs(x-y)
    else:
        tmp = abs(y-x)
    if tmp < ans:
        ans = tmp
    print(ans)

Submission status: Wrong Answer

Solution code:
nums = [int(x) for x in input().split()]
N = nums[0]
A = [int(x) for x in input().split()]
X = sum(A)
MIN = 10**8
for i in range(1,N):
    s = A[0:i]
    MIN = min(MIN,abs(X - 2*sum(s)))
print(MIN)

Submission status: Wrong Answer

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

x = 0
y = 0
l = len(arr)
arr.sort()

for i in range(n / 2):
    if i % 2 == 0:
        x += arr.pop(0)
        y += arr.pop(0)
    else:
        x += arr.pop()
        y += arr.pop()


print  abs(x - y)

Submission status: Wrong Answer

Solution code:
N=int(input())
an=list(map(int,input().split()))
s = sum(an)
h = s/2
f=int(N/2)
x = sum(an[:f])
y = s - x
z=abs(x-y)
for i in range(f):
    if f+1 >= N:
        break
    t1=x + an[f+1]
    t2=y - an[f+1]
    t3=x - an[f-1]
    t4=y + an[f-1]
    k1=abs(t1-t2)
    k2=abs(t3-t4)
    p=0
    k = min(k1,k2)
    if k1 > k2:
        p = -1
    else:
        p = 1
    if z > k:
        f = f + p
        z = k
    else:
        break
print(z)

Submission status: Wrong Answer

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

l = 1
r = N-1
while True:
    s1 = abs(sum(a[:l]) - sum(a[l:]))
    s2 = abs(sum(a[:r]) - sum(a[r:]))
    if r - l <= 1:
        break
    if s1 <= s2:
        r = r - (r - l)//2
    elif s1 > s2:
        l = l + (r - l)//2

print(min(s1, s2))

Submission status: Wrong Answer

Solution code:
N=int(input())
a=input().split()
c=[]
d=[]
s=0
t=0
u=0
A=0

for i in range(N):
 b=int(a[i])
 c.append(b)

for i in range(N):
 A +=c[i]
for i in range(N):
 s +=c[i]
 t=A-s
 if(1<=i<=N-1):
  u=s-t
  
  if(u<0):
   u=-u
  d.append(u)

d=sorted(d)

print(d[0])

Submission status: Wrong Answer

Solution code:
a = int(input())
c = list(map(int,input().split()))
print(c)
su = sum(c)
su2 = su//2
cc = 0
cc2 = su
#if c[0] >= 0 and c[-1] >= 0:
    #while cc <= su2:
for x in range(a):
    cc += c[x]
    if cc >= su2:break
cc2 = cc - c[x]
ccc = []
ccc.append(abs(su - 2*cc))
ccc.append(abs(su - 2*cc2))
print(min(ccc))

        #print(cc,c[x])
    #print(cc)
"""
else:
    while cc <= su2:
        for x in range(a):
            cc += c[x]
"""

Submission status: Wrong Answer

Solution code:
def min(l):
    n = len(l)
    min = 1145141919810
    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
x=l[0]
y=l[1]
a[0]=abs(x-y)
for i in range(1,N):
    x+=l[i]
    y-=l[i]
    a[i]=abs(x-y)
print(min(a))

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
x=l[0]
y=l[1]
a[0]=abs(x-y)
for i in range(1,N):
    x+=l[i]
    y-=l[i]
    a[i]=abs(x-y)
print(min(a))

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 a > 1000000000 :
      if b > 1000000000 :
        a = a - 1000000000
        b = b - 1000000000
    if a < -1000000000 :
      if b < -1000000000 :
        a = a + 1000000000
        b = b + 1000000000
    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:
# 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

	if mid <= 2:
		return(min([abs(func(cards, i)) for i in [1, 2, 3]]))
	elif mid >= len(cards) - 2:
		return(min([abs(func(cards, len(cards) - i)) for i in [1, 2, 3]]))
	else:
		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:
m = int(input())
n = list(map(int, input().split()))
sum1 = 0
A = [0]
if m == 2:
    if n[0] >= n[1]:
        ans = n[0] - n[1]
        print(ans)
    else:
        ans = n[1] - n[0]
        print(ans)
else:
    n.sort()
    n.reverse()
    #n = list(map(int, n))
    #print(n)
    #print(sum(A))
    #print(sum(n))
    for i in range(m):
        ans = sum(n) - sum(A)
        #print(ans)
        if i == m - 1:
            if ans > 0:
                print(ans)
            else:
                print(ans * -1)
            break
        else:
            A.append(n[0])
            n.pop(0)
            ans2 = sum(n) - sum(A)
            #print(ans2)
            if ans2 >= 0 and ans <= 0:
                if ans2 * -1 < ans:
                    print(ans * -1)
                else:
                    print(ans2)
                break
            elif ans2 <= 0 and ans >= 0:
                if ans2 * -1 > ans:
                    print(ans2 * -1)
                else:
                    print(ans)
                break

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
x=0
y=0
sum=0
min=10**9
for i in range(n):
  sum+=a[i]
for i in range(n-1):
  for j in range(i+1):
    x+=a[j]
  y=sum-x
  if min>abs(x-y):
    z=x-y
    min=abs(z)
  x=0
  y=0
print(min)

Submission status: Wrong Answer

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

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

a.sort()
a.reverse()
#print a
sum_all=sum(a)
mini_p=10**10
#print "sum_all: "+str(sum_all)
for i in range(len(a)-1):   #0-5
    sum_left=sum(a[0:i+1])
    #print "sum_left: "+ str(sum_left)
    sum_right=sum_all - sum_left
    #print "sum_right: "+str(sum_right)
    diff=sum_right-sum_left
    #print "diff_p: "+str(diff)
    if diff<=0:
        mini_n=diff
        break
    else:
        mini_p=diff

#print "mini_p: "+str(mini_p)
#print "mini_n: "+str(mini_n)
print min(mini_p,abs(mini_n))

Submission status: Wrong Answer

Solution code:
import math

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

p = [x for x in a if x > 0]
m = [x for x in a if x < 0]

p.sort()
m.sort()

ph = math.ceil(sum(p) / 2)
mh = math.floor(sum(m) / 2)

ps = 0
pa = 0

for i in range(len(p)):
    if ps + p[i] <= ph and i < n:
        ps += p[i]
    else:
        pa = sum(p[i:])
        break

ms = 0
ma = 0

for i in range(len(m)):
    if ma + m[i] > mh and i < n:
        ma += m[i]
    else:
        ms = sum(m[i:])
        break

print(abs(ps + ms - pa - ma))

Submission status: Wrong Answer

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

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

a.sort()
a.reverse()
print a
sum_all=sum(a)
mini_p=10**10
print "sum_all: "+str(sum_all)
for i in range(len(a)-1):   #0-5
    sum_left=sum(a[0:i+1])
    print "sum_left: "+ str(sum_left)
    sum_right=sum_all - sum_left
    print "sum_right: "+str(sum_right)
    diff=sum_right-sum_left
    print "diff_p: "+str(diff)
    if diff<=0:
        mini_n=diff
        break
    else:
        mini_p=diff

print "mini_p: "+str(mini_p)
print "mini_n: "+str(mini_n)
print min(mini_p,abs(mini_n))