Problem:


Score : 300 points

Problem Statement

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

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

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

Constraints

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

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

a_i is an integer.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print the answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

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

Sample Input 2

2
10 -10

Sample Output 2

20

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



Sample input:
6
1 2 3 4 5 6


Sample output:
1

Submission status: Accepted

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

Submission status: Accepted

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

tot = 0
ans = 1 << 62
for i in range(len(a) - 1):
    tot += a[i]
    ans = min(abs(tot - (s - tot)), ans)
print(ans)

Submission status: Runtime Error

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]
  ans = (S - 2 * snuke)
  
  if ans >= dif:
    ans = dif
    
print(ans)

Submission status: Runtime Error

Solution code:
print('?', 10**10)
if input() == 'Y':
    for nod in range(10):
        print('?', 10**nod+1)
        if input() == 'Y':
            break
    ans = 10**nod
else:
    for nod in range(1, 10):
        print('?', 10**nod)
        if input() == 'Y':
            continue
            nod -= 1
        break
    ok = 10**(nod-1)
    ng = 10**nod
    while abs(ok - ng) > 1:
        med = (ok + ng)//2
        print('?', med*10+1)
        if input() == 'N':
            ok = med
        else:
            ng = med
    ans = ok+1
print('!', ans)

Submission status: Runtime Error

Solution code:

N=int(input())
e_list = [[] for i in range(N)]
for i in range(N-1):
    a,b=list(map(int,input().split()))
    a,b=a-1,b-1
    e_list[a].append(b)
    e_list[b].append(a)

vi = 0 #時と場合によってここを変える
from collections import deque
Q = deque([vi])
checked_list = [False for i in range(N)]
checked_list[vi]=True
prev_list = [-1 for i in range(N)]
min_path_list = [10**27 for i in range(N)] #問題によりここを変える
min_path_list[vi] = 0
while len(Q)>0:
    v = Q.pop()
    for v1 in e_list[v]:
        if not checked_list[v1]:
            checked_list[v1]=True
            Q.appendleft(v1)
            prev_list[v1]=v
            min_path_list[v1]=min(min_path_list[v1],min_path_list[v]+1) #問題によりここを変える

path = [N-1]
v=N-1
while prev_list[v]!=-1:
    path.append(prev_list[v])
    v=prev_list[v]

a=path[(min_path_list[N-1]-1)//2]
b=path[(min_path_list[N-1]-1)//2+1]
for i in range(len(e_list[a])):
    if e_list[a][i]==b:
        break
del e_list[a][i]

for i in range(len(e_list[b])):
    if e_list[b][i]==a:
        break
del e_list[b][i]

vi = 0 #時と場合によってここを変える

Q = deque([vi])
checked_list = [False for i in range(N)]
checked_list[vi]=True
count = 1
while len(Q)>0:
    v = Q.pop()
    for v1 in e_list[v]:
        if not checked_list[v1]:
            checked_list[v1]=True
            Q.appendleft(v1)
            count+=1

if count>N-count:
    print("Fennec")
else:
    print("Snuke")

Submission status: Runtime Error

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

x = 0
y = sum(a)
ans = []

if i in range(n-1):
    x += a[i]
    y -= a[i]
    ans.append(abs(x-y))

print(min(ans))

Submission status: Runtime Error

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*a[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: Runtime Error

Solution code:
n = int(input())
root = [[] for i in range(n+1)]
leaf = [0]*(n+1)
leaf[0] = -1
leaf[1] = 1
leaf[-1] = 2

for _ in range(n-1):
    a, b = map(int, input().split())
    root[a].append(b)
    root[b].append(a)

turn = 1
while 0 in leaf:
    for idx, i in enumerate(leaf):
        if i == turn:
            for t in root[idx]:
                if leaf[t] == 0:
                    leaf[t] = turn
    if turn == 1: turn = 2
    else : turn = 1
    
if leaf.count(1) > int(n/2):
    print('Fennec')
else :
    print('Snuke')

Submission status: Runtime Error

Solution code:
def QSortPosition(list,low,high):
    LsaveNum=list[low]
    pivotkey=list[low]
    while(low<high):
        while(list[low]<pivotkey) :
            low+=1
        list[high]=list[low]
        high-=1
        while(list[high]>pivotkey):
            high-=1
        list[low]=list[high]
        low+=1
    list[low]=LsaveNum
    return low

def QSort(list,s,t):
    if(s<t):
        pivotkey=QSortPosition(list,s,t)
        QSort(list,s,pivotkey-1)
        QSort(list,pivotkey+1,t)
    return list

def FindMinS(list,length):
	count1=0
	sum=0

	while count1<length:
		sum=sum+list[count1]
		count1+=1

	InvidNum=length-1
	location=0
	count2=0
	MinList=[]

	while count2<InvidNum:
		count3=count2
		ab1=0
		ab2=0
		while count3<=location:
			ab1=ab1+list[count3]
			count3+=1
			ab2=sum-ab1
			ab3=abs(ab2-ab1)
			MinList.append(ab3)
		count2+=1

	MinList=QSort(MinList,0,InvidNum-1)
	return MinList[0]

num=input("请输入列表长度：")
count=0
list=[]
while count<num:
	listele=input("输入元素：")
	list.append(listele)
	count+=1
minnum=FindMinS(list,num)
print(minnum)

Submission status: Runtime Error

Solution code:
def QSortPosition(list,low,high):
    LsaveNum=list[low]
    pivotkey=list[low]
    while(low<high):
        while(list[low]<pivotkey) :
            low+=1
        list[high]=list[low]
        high-=1
        while(list[high]>pivotkey):
            high-=1
        list[low]=list[high]
        low+=1
    list[low]=LsaveNum
    return low

def QSort(list,s,t):
    if(s<t):
        pivotkey=QSortPosition(list,s,t)
        QSort(list,s,pivotkey-1)
        QSort(list,pivotkey+1,t)
    return list

def FindMinS(list,length):
	count1=0
	sum=0

	while count1<length:
		sum=sum+list[count1]
		count1+=1

	InvidNum=length-1
	location=0
	count2=0
	MinList=[]

	while count2<InvidNum:
		count3=count2
		ab1=0
		ab2=0
		while count3<=location:
			ab1=ab1+list[count3]
			count3+=1
			ab2=sum-ab1
			ab3=abs(ab2-ab1)
			MinList.append(ab3)
		count2+=1

	MinList=QSort(MinList,0,InvidNum-1)
	return MinList[0]

num=input("请输入列表长度：")
count=0
list=[]
while count<num:
	listele=input("输入元素：")
	list.append(listele)
	count+=1
minnum=FindMinS(list,num)
print(minnum)

Submission status: Runtime Error

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

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

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

main()

Submission status: Runtime Error

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

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

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

main()

Submission status: Runtime Error

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

n = raw_input()
n = int(n)

tree = []

i = 0
while i < n:
    tree.append([])
    i += 1

i = 0
while i < n-1:
    ab = raw_input()
    ab = ab.split()
    ab = map(int,ab)
    tree[ab[0]-1].append(ab[1])
    tree[ab[1]-1].append(ab[0])
    i += 1

black = [n]*n
white = [n]*n

black[0] = 0
white[n-1] = 0

i = 0
while max(black) >= n and max(white) >= n:
    black_next_list = []
    black_i_list = []
    while i in black:
        black_i = black.index(i)
        black_next_list.extend(tree[black_i])
        black[black_i] = n
        black_i_list.append(black_i)

    for black_i in black_i_list:
        black[black_i] = i

    for black_next in black_next_list:
        if black[black_next - 1] < i:
            pass
        else:
            black[black_next - 1] = i + 1

    white_next_list = []
    white_i_list = []
    while i in white:
        white_i = white.index(i)
        white_next_list.extend(tree[white_i])
        white[white_i] = n
        white_i_list.append(white_i)

    for white_i in white_i_list:
        white[white_i] = i

    for white_next in white_next_list:
        if white[white_next - 1] < i:
            pass
        else:
            white[white_next - 1] = i + 1

    i += 1

status = 0

i = 0
while i < n:
    if black[i] <= white[i]:
        status += 1
    else:
        status -= 1
    i += 1

if status > 0:
    print "Fennec"
else:
    print "Snuke"

Submission status: Runtime Error

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

# http://arc078.contest.atcoder.jp/tasks/arc078_a

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

sum_num = sum(a)
current_sum = 0
min_diff = float('inf')
for i in range(0, N - 1):
    current_sum += a[i]
    min_diff = min(min_diff, abs(current_sum - (sum_num - current_sum)))
print(min_diff)

Submission status: Runtime Error

Solution code:
import sys


def _rec(pos, n, edges, path, visited):
    if pos in visited:
        return None
    visited.add(pos)
    if pos == n:
        return path
    for to_node in edges[pos]:
        if to_node in visited:
            continue
        path.append(to_node)
        ret = _rec(to_node, n, edges, path, visited)
        if ret is not None:
            return ret
        path.pop()
    return None


def _count(pos, invalid, edges, visited):
    if pos in visited:
        return 0
    visited.add(pos)
    all = 0
    for edge in edges[pos]:
        if edge != invalid:
            all += _count(edge, pos, edges, visited) + 1
    return all


def _main(n, edges):
    visited = set()
    path = _rec(1, n, edges, [1], visited)
    if len(path) % 2 == 0:
        fennec_pos = path[len(path) / 2]
        sunuke_pos = path[len(path) / 2 + 1]
    else:
        fennec_pos = path[len(path) / 2]
        sunuke_pos = path[len(path) / 2 + 1]
    visited = set()
    fennec_count = _count(fennec_pos, sunuke_pos, edges, visited)
    visited = set()
    sunuke_count = _count(sunuke_pos, fennec_pos, edges, visited)
    #print fennec_count, sunuke_count
    if fennec_count >= sunuke_count:
        return "Fennc"
    else:
        return "Sunuke"


def main():
    global nodes
    n = int(sys.stdin.readline())
    edges = {}
    for i in xrange(n - 1):
        (node_a, node_b) = sys.stdin.readline().rstrip().split()
        node_a = int(node_a)
        node_b = int(node_b)
        edges.setdefault(node_a, []).append(node_b)
        edges.setdefault(node_b, []).append(node_a)
    print _main(n, edges)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from sys import stdin
ma = 10**18
n = int(stdin.readline())
a = map(int,stdin.readline().split())
x = 0
s = sum(n)
for i in a[:-1]:
 x+= i
 ma  = min(ma, abs(s-2*x) )
print ma

Submission status: Runtime Error

Solution code:
from sys import stdin
ma = 10**18
n = int(stdin.readline().split())
a = map(int,stdin.readline().split())
x = 0
s = sum(n)
for i in a[:-1]:
 x+= i
 ma  = min(ma, abs(s-2*x) )
print ma

Submission status: Runtime Error

Solution code:
DEBUG = False
if DEBUG:
    N = int(input())

def check(n):
    print("? " + str(n), flush = True)
    if DEBUG:
        rtn = 'Y' if (n <= N) == (str(n) <= str(N)) else 'N'
        print(rtn)
        return rtn
    else:
        return input()

if check(10000000000) == 'Y':
    for i in range(11, 0, -1):
        n = 1
        tmp = 10**i - 1
        if check(tmp) == 'N':
            n = tmp + 1
            break
else:
    for i in range(1, 10):
        tmp = 10**i
        if check(tmp) == 'N':
            l = tmp // 10
            r = tmp - 1
            break
    while l + 1 < r:
        m = (l + r) // 2
        if check(10 * m) == 'Y':
            r = m
        else:
            l = m
    n = r

print("! "+ str(n), flush = True)

Submission status: Runtime Error

Solution code:
N, nums = int(input()), list(map(int, input().split()))
total, ano = sum(nums), 0
for i in range(len(nums)-1):
    total -= nums[i]
    ano += nums[i]    
    if abs(ano-total) < last_dif or i == 0:
        dif = abs(ano-total)
        last_dif = dif
print(dif)

Submission status: Runtime Error

Solution code:
N, nums = int(input()), list(map(int, input().split()))
total, ano = sum(nums), 0
for i in range(len(nums)-1):
    total -= nums[i]
    ano += nums[i]    
    if abs(ano-total) < last_dif or i == 0:
        dif, last_dif = abs(ano-total), dif
print(dif)

Submission status: Runtime Error

Solution code:
N = int(input())
nums = list(map(int, input().split()))
total, ano = sum(nums), 0
last_dif = 100000000
for i in range(len(nums)-1):
    total -= nums[i]
    ano += nums[i]    
    if abs(ano-total) < last_dif:
        dif = abs(ano-total)
        last_dif = dif
print(dif)

Submission status: Runtime Error

Solution code:
# N = int(input())
nums = list(map(int, input().split()))
total, ano = sum(nums), 0
last_dif = 100000000
for i in range(len(nums)-1):
    total -= nums[i]
    ano += nums[i]    
    if abs(ano-total) < last_dif:
        dif = abs(ano-total)
        last_dif = dif
print(dif)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)

def find_path(now, par):
    if now == n - 1:
        path.append(n - 1)
        return True
    for to in g[now]:
        if to == par: continue
        if find_path(to, now):
            path.append(now)
            return True
    return False

def dfs(now, par, color):
    color[now] = "b"
    for to in g[now]:
        if to == par: continue
        if color[to] == "w": continue
        dfs(to, now, color)

n = int(raw_input())
g = [[] for _ in xrange(n)]
for i in xrange(n - 1):
    a, b = map(int, raw_input().split())
    a -= 1; b -=1
    g[a].append(b)
    g[b].append(a)
path = []
find_path(0, -1)
path.reverse()
color = ["bl"] * n
color[0] = "b"
color[n - 1] = "w"
for i in xrange(1, len(path) - 1):
    v = path[i]
    if i % 2 == 1:
        color[v] = "b"
    else:
        color[v] = "w"
dfs(0, -1, color)
cnt = 0
for i in xrange(n):
    if color[i] == "b":
        cnt += 1
if cnt > n - cnt:
    print "Fennec"
else:
    print "Snuke"

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys

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


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

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


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


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


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


def read_str():
    return input()


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


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


if __name__ == '__main__':
    main()


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


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


def read_str():
    return input()


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


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


if __name__ == '__main__':
    main()