Felix6326727/beyond-accuracy-code-comprehension huggingface dataset:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
M = list(map(int, input().split()))

def exhastive_search(target, head, sum):
    for i in range(head, N):
        sum += A[i]
        print(sum)
        if target == sum:
            return True
        elif target > sum:
            if exhastive_search(target, i+1, sum):
                return True
            sum -= A[i]
        else:
            sum -= A[i]
    return False


for target in M:
    sum = 0
    result = "no"
    for i in range(N):
        if exhastive_search(target, i, sum):
            result = "yes"
            break
    print(result)


# Example run
# Input:
-507526654
-54634663
3615919
-3550
# Output:
no
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
M = list(map(int, input().split()))

def exhastive_search(target, head, sum):
    for i in range(head, N):
        sum += A[i]
        print(sum)
        if target == sum:
            return True
        elif target > sum:
            if exhastive_search(target, i+1, sum):
                return True
            sum -= A[i]
        else:
            sum -= A[i]
    return False


for target in M:
    sum = 0
    result = "no"
    for i in range(N):
        if exhastive_search(target, i, sum):
            result = "yes"
            break
    print(result)


# Example run
# Input:
-61
-6212597
36241
52108041
# Output:
no
from sys import stdin
n = int(input())
dict = {}
for i in range(n):
    line = stdin.readline()
    if line[0:6] == 'insert':
        dict[line[7:]] = 1
    else:
        if line[5:] in dict:
            print('yes')
        else:
            print('no')


# Example run
# Input:
-2
# Output:
# coding: utf-8

str_que = list(input())
num_que = int(input())
l_que = [list(map(int, input().split())) for i in range(num_que)]

print(str_que)
print(num_que)
print(l_que)

l_alp = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

n_ind = 0
count = 0
b_count = True
list_add = []

def check_min(list):
	global n_ind
	global count
	global b_count
	global list_add

	if len(list) == 0:
		print("yes")
		b_count = False

	else:
		count = 0
		list_add = [i for i in list]
		list_add.append(0)
		
		for index, item in enumerate(list):
			if index == len(list):
				break
			else:
				if list_add[index + 1] == item:
					n_ind = index
					count += 1
					break
				else:
					continue

		if count == 0:
			print("no")
			b_count = False

def change_list(index, list):
	global list_check
	if list[index] == "z":
		del list[index + 1]
		del list[index]
		list_check = list
		
	else:
		alp_index = l_alp.index(list[index])
		list[index] = l_alp[alp_index + 1]
		list[index + 1] = l_alp[alp_index + 1]
		list_check = list

for i in l_que:
	print(i)
	i_0 = i[0]
	i_1 = i[1]
	range_check = list(range((i_0 - 1), i_1))
	list_check = [str_que[j] for j in range_check]
	print(list_check)
	while b_count:
		check_min(list_check)
		change_list(n_ind, list_check)
	b_count = True

# Example run
# Input:
xWAS`~
-255155
# Output:
['x', 'W', 'A', 'S', '`', '~']
-255155
[]
n = int(input())
a = list(map(int,input().split()))

ans = 1000000000000000000000000000000000000
pair = [0,0,0,0]
for i in range(n-3):
	for j in range(i,n-2):
		for k in range(j,n-1):
			p = sum(a[0:i+1])
			q = sum(a[i+1:j+1])
			r = sum(a[j+1:k+1])
			s = sum(a[k+1:n])
			M = max(p,q,r,s)
			m = min(p,q,r,s)
			if ans > M-m:
				ans =  M-m
				pair = [p,q,r,s]
			
		
	
print(ans)
print(pair)

# Example run
# Input:
-491914923
0
# Output:
1000000000000000000000000000000000000
[0, 0, 0, 0]
N = int(input())
A = tuple(map(int, input().split()))
CA = []
cumsum = 0
for a in A:
    cumsum += a
    CA.append(cumsum)

mindiff = CA[-1]
for i in range(0, N-3):
    p = CA[i]
    for j in range(i+1, N-2):
        q =  CA[j] - CA[i]
        for k in range(j+1, N-1):
            r = CA[k] - CA[j]
            s = CA[-1] - CA[k]
            mindiff = min(mindiff, max([p, q, r, s]) - min([p, q, r, s]))
print(mindiff)

# Example run
# Input:
-740
1
# Output:
1
n = int(input()) 
l = list(map(int,input().split()))
ans = [0]*(n-3)

l_sum = list(sum(l[:i]) for i in range(n+1))
l_sum.pop(0)
for i in range(1,n-2):
    tmpl = list(abs(l_sum[i]-l_sum[j]*2) for j in range(0,i))
    tmpr = list(abs(l_sum[k]*2-l_sum[i]-l_sum[n-1])for k in range(i+1,n-1))
    indexl = tmpl.index(min(tmpl))
    indexr = tmpr.index(min(tmpr))
    p = l_sum[indexl]
    q = (l_sum[i]-l_sum[indexl])
    r = (l_sum[i+1+indexr]-l_sum[i])
    s = (l_sum[n-1]-l_sum[i+1+indexr])
    ans[i-1] = max(p,q,r,s)-min(p,q,r,s)
print(min(ans))

# Example run
# Input:
224
-3753932
# Output:
3753932
def main():
	N = int(input())
	A = list(map(int, input().split()))
	s = sum(A)
	def ano_helper(BCDE):
		BCDE = BCDE + [s - sum(BCDE)]
		BCDE.sort()
		return abs(BCDE[-1] - BCDE[0])
	def helper(BCDEs, inds):
		if len(BCDEs[0]) == 4:
			return BCDEs
		ret_BCDEs = []
		ret_inds = []
		for candidate in range(len(BCDEs)):
			BCDE = BCDEs[candidate]
			ind = inds[candidate]
			ave = sum(A[ind:]) / (5 - len(BCDE))
			temp = 0
			while temp + A[ind] < ave:
				temp += A[ind]
				ind += 1
				if ind == N:
					break
			ret_BCDEs.append(BCDE + [temp])
			ret_inds.append(ind)
			if ind < N - 1:
				ret_BCDEs.append(BCDE + [temp + A[ind]])
				ret_inds.append(ind + 1)
		return helper(ret_BCDEs, ret_inds)
	print(sorted([ano_helper(i[1:]) for i in helper([[0]], [0])])[0])
	return 0
	#BCDE = []
	#ind = 0
	#su = []
	#tempBCDE = [[]]
	#while len(BCDE[-1]) < 4:
	#	temp = 0
	#	ret = []
	#	while temp < (s - su) / (4 - len(BCDE)):
	#		te = A[ind]
	#		temp += te
	#		ret.append(te)
	#		su += te
	#		ind += 1
	#	tempBCDE.append(ret)
	#print(BCDE)
main()

# Example run
# Input:
-73864104
56
# Output:
56
from itertools import *
N = int(input())
A = list(accumulate(map(int,input().split())))
l = 0
r = 2
t = A[-1]
a = t

for m in A[1:-2]:
  while abs(t-2*A[r]+m)>abs(t-2*A[r+1]+m):
    r+=1
  while abs(m-2*A[l])>abs(m-2*A[l+1]):
    l+=1
  x = sorted([A[l],m-A[l],A[r]-m,t-A[r]])
  a = min(a,x[3]-x[0])

print(a)

# Example run
# Input:
41286958
19928
# Output:
19928
N = int(input())
A_l = list(map(int, input().split()))

import itertools
sp_l = list(itertools.combinations(range(N-1), 3))

calc_l = []

for i,j,k in sp_l:
    
    P = sum(A_l[:i+1])
    Q = sum(A_l[i+1:j+1])
    R = sum(A_l[j+1:k+1])
    S = sum(A_l[k+1:])

    calc_l.append(abs(max(P,Q,R,S) - min(P,Q,R,S)))

print(min(calc_l))

# Example run
# Input:
119
1174165
# Output:
1174165
def main():
	N = int(input())
	A = list(map(int, input().split()))
	s = sum(A)
	def ano_helper(BCDE):
		BCDE = BCDE + [s - sum(BCDE)]
		BCDE.sort()
		return abs(BCDE[-1] - BCDE[0])
	def helper(BCDEs, inds):
		if len(BCDEs[0]) == 4:
			return BCDEs
		ret_BCDEs = []
		ret_inds = []
		for candidate in range(len(BCDEs)):
			BCDE = BCDEs[candidate]
			ind = inds[candidate]
			ave = sum(A[ind:]) / (5 - len(BCDE))
			temp = 0
			while temp + A[ind] < ave:
				temp += A[ind]
				ind += 1
			ret_BCDEs.append(BCDE + [temp])
			ret_inds.append(ind)
			if ind < N - 1:
				ret_BCDEs.append(BCDE + [temp + A[ind]])
				ret_inds.append(ind + 1)
		return helper(ret_BCDEs, ret_inds)
	print(sorted([ano_helper(i[1:]) for i in helper([[0]], [0])])[0])
	return 0
	#BCDE = []
	#ind = 0
	#su = []
	#tempBCDE = [[]]
	#while len(BCDE[-1]) < 4:
	#	temp = 0
	#	ret = []
	#	while temp < (s - su) / (4 - len(BCDE)):
	#		te = A[ind]
	#		temp += te
	#		ret.append(te)
	#		su += te
	#		ind += 1
	#	tempBCDE.append(ret)
	#print(BCDE)
main()

# Example run
# Input:
-65
0
# Output:
0
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

sm = 0
for i in range(0, n):
    sm = sm + a[i]

min_dif = sm

p = 0
for i in range(n-3):
    p = p + a[i]
    if 4*p <= sm - 3*min_dif or 4*p >= sm + 3*min_dif:
        next
    q = 0
    for j in range(i+1, n-2):
        q = q + a[j]
        if 4*q <= sm - 3*min_dif or 4*q >= sm + 3*min_dif:
            next
        r = 0
        for k in range(j+1, n-1):
            r = r + a[k]
            if 4*r <= sm - 3*min_dif or 4*r >= sm + 3*min_dif:
                next
            s = 0
            for l in range(k+1, n):
                s = s + a[l]
            d = max([p, q, r, s])-min([p, q, r, s])
            min_dif = min([d, min_dif])

print(min_dif)


# Example run
# Input:
0
211
# Output:
0
N=int(input())
nums=list(map(int,input().split()))

ans=10000000000
for i in range(0,N-3):
  alis=nums[0:i+1]
  for j in range(i+1,N-2):
    blis=nums[i+1:j+1]
    for k in range(j+1,N-1):
      clis=nums[j+1:k+1]
      dlis=nums[k+1:]
      lislis=[alis,blis,clis,dlis]
      anslis=[sum(x) for x in lislis]
      preans=max(anslis)-min(anslis)
      if preans<ans:
        ans=preans
print(ans)

# Example run
# Input:
3
23349322
# Output:
10000000000
n=int(input())
a=list(map(int,input().split()))
x=sum(a)//4
p,sp=0,0
for i in range(n):
    sp+=a[i]
    p=i
    if sp>x:
        break

def qq(p_):
    global n,a
    x=(sum(a)-sum(a[:p_+1]))//3
    q_,sq_=0,0
    for i in range(p_+1,n):
        sq_+=a[i]
        q_=i
        if sq_>x:
            break
    return [q_-1,q_]
def rr(q_):
    global n,a
    x=(sum(a)-sum(a[:q_+1]))//2
    r_,sr_=0,0
    for i in range(q_+1,n):
        sr_+=a[i]
        r_=i
        if sr_>x:
            break
    return [r_-1,r_]
f=[p-1,p] if p!=0 else [p]
ans=[]
for z1 in f:
    for z2 in qq(z1):
        for z3 in rr(z2):
            cand=[sum(a[:z1+1]),sum(a[z1+1:z2+1]),sum(a[z2+1:z3+1]),sum(a[z3+1:])]
            ans.append(max(cand)-min(cand))
print(min(ans))




# Example run
# Input:
0
0
# Output:
0
N = int(input())
A = list(map(int, input().split()))

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

X = [0] * N
Y = [0] * N
x = 1
y = 3

for i in range(2, N - 1):
    L = S[i]
    R = S[N] - S[i]
    while abs(L - 2 * S[x + 1]) <= abs(L - 2 * S[x]):
        x += 1
    X[i] = x
    while abs(R - 2 * (S[y + 1] - L)) <= abs(R - 2 * (S[y] - L)):
        y += 1
    Y[i] = y

ans = S[N]
for i in range(2, N - 1):
    x = X[i]
    y = Y[i]
    B = S[x]
    C = S[i] - S[x]
    D = S[y] - S[i]
    E = S[N] - S[y]
    P = [B, C, D, E]
    ans = min(ans, max(P) - min(P))

print(ans)


# Example run
# Input:
0
-54266464
# Output:
0
N = int(input())
A = tuple(map(int, input().split()))
CA = []
cumsum = 0
for a in A:
    cumsum += a
    CA.append(cumsum)

mindiff = CA[-1]
for c in range(1,N-2):
    # left side
    p = 0
    q = CA[-1]
    for i in range(0, c):
        pi = CA[i]
        qi = CA[c] - CA[i]
        if abs(pi- qi) <= abs(p - q):
            p = pi; q = qi
        else:
            break

    # right side
    r = 0
    s = CA[-1]
    for i in range(c+1, N):
        ri = CA[i] - CA[c]
        si = CA[-1] - CA[i]
        if abs(ri- si) <= abs(r - s):
            r = ri; s = si
        else:
            break
    
    mindiff = min(mindiff, max([p, q, r, s]) - min([p, q, r, s]))
print(mindiff)

# Example run
# Input:
-55910820
-29958
# Output:
-29958
# 累積和, 尺取り法
from itertools import accumulate
_ = int(input())
*a, = accumulate(map(int,input().split()))
l,r,e,ans = 0, 2, a[-1], a[-1]
for m in a[1:-2]:
    # 右部分列を極力等しくしたい
    while a[r] - m < e - a[r+1]:
        r += 1
    # 左部分列を極力等しくしたい
    while a[l] < m - a[l+1]:
        l += 1
    tmp = sorted([ a[l], m-a[l], a[r]-m, e-a[r] ])
    ans = min(ans, tmp[-1]-tmp[0] )
print(ans)

# Example run
# Input:
11980
-4030044
# Output:
-4030044
n = int(input())
A = tuple(map(int, input().split()))
L = len(A)
B = [0]*L
s = 0
for i,v in enumerate(A):
    s += v
    B[i] = s

def sumf(i,j):
    global B
    if i == 0:
        return B[j]
    else:
        return B[j] - B[i-1]

def dv(a,b):
    r = b - a + 1
    x,y = 0,0
    while r > 0:
        if x < y:
            x += A[a]
            a += 1
        else:
            y += A[b]
            b -= 1
        r -= 1
    return x,y

minu = s
for r in range(2,L-1):
    p,q = dv(0,r-1)
    r,s = dv(r,L-1)
    t = max(p,q,r,s) - min(p,q,r,s)
    if minu > t:
        minu = t

print(minu)


# Example run
# Input:
186860433
108968
# Output:
108968
N=int(input())
A = list(map(int,input().split()))
sum_=[A[0]]
ans =float('inf')
for i in range(1,N):
    sum_.append(sum_[i-1]+A[i])

midL=0
midR=2
print(sum_)
for i in range(1,N-2):
    minL=float('inf')
    minR=float('inf')
    for j in range(midL,i):
        if abs(sum_[i]-2*sum_[j]) > minL:
            break
        else:
            midL=j
            minL=abs(sum_[i]-2*sum_[j])
        
    for k in range(midR,N-1):
        if abs(sum_[-1]-2*sum_[k]+sum_[i]) > minR:
            break
        else:
            midR=k
            minR=abs(sum_[-1]-2*sum_[k]+sum_[i])
    tmp =  max(sum_[midL],sum_[i]-sum_[midL],sum_[midR]-sum_[i],sum_[-1]-sum_[midR]) -min(sum_[midL],sum_[i]-sum_[midL],sum_[midR]-sum_[i],sum_[-1]-sum_[midR])
    if ans >tmp:
        ans =tmp
print(ans)

# Example run
# Input:
-812896393
-97075663
# Output:
[-97075663]
inf
n = int(input())
a = list(map(int, input().split()))
res = 20000000002
for k in range(n-3):
    b = sum(a[:k+1])
    for l in range(k+1,n-2):
        c = sum(a[k+1:l+1])
        for m in range(l+1,n-1):
            d = sum(a[l+1:m+1])
            e = sum(a[m+1:])
            f=[b,c,d,e]
            kari = max(f)-min(f)
            if kari < res:
                res = kari
print(res)


# Example run
# Input:
-51677853
-1
# Output:
20000000002
from itertools import accumulate

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

cumA = list(accumulate(A))

l_index = 0
r_index = 2
cumA_max = cumA[-1]

diff = cumA_max

for i in range(1, len(A)-2):
  # iで切った左の部分列において、l_iで切った時、左より右の方が大きいときは1ずらせる
  while cumA[l_index] < cumA[i] - cumA[l_index + 1]:
    l_index += 1

  # iで切った右の部分列において、r_iで切った時、左より右の方が大きいときは1ずらせる
  while cumA[r_index] - cumA[i] < cumA_max - cumA[r_index + 1]:
    r_index += 1
        
  left = cumA[l_index]
  center = cumA[i] - cumA[l_index]
  right = cumA[r_index] - cumA[i]
  last = cumA_max - cumA[r_index]

  diff = min(diff, max(left, center, right, last) - min(left, center, right, last))

print(diff)

# Example run
# Input:
517661
527162809
# Output:
527162809
N=int(input())
A=[int(i) for i in input().split()]
S=[0 for i in range(N+1)]
for i in range(N):
    S[i+1]=S[i]+A[i]
B=A[::-1]
T=[0 for i in range(N+1)]
for i in range(N):
    T[i+1]=T[i]+B[i]
F=[0 for i in range(N)]
G=[0 for i in range(N)]
for i in range(N):
    LS=S[i+1]
    k=0
    if i!=0:
        k=F[i-1]
    if k!=0:
        k-=1
    tmp=LS
    for j in range(k,i+1):
        L=S[j]
        R=LS-S[j]
        if abs(R-L)<=tmp:
            F[i]=j
        else:
            break
        tmp=abs(R-L)
    if F[i]<=0:
        F[i]=0
for i in range(N):
    RS=T[i+1]
    k=0
    if i!=0:
        k=G[i-1]
    if k!=0:
        k-=1
    tmp=LS
    for j in range(k,i+1):
        L=T[j]
        R=RS-T[j]
        if abs(R-L)<=tmp:
            G[i]=j
        else:
            break
        tmp=abs(R-L)
    if G[i]<=0:
        G[i]=0
ans=S[N]
for i in range(N):
    A=[0 for i in range(4)]
    k=0
    if i!=0:
        k=F[i-1]
    A[0]=S[k]
    A[1]=S[i]-S[k]
    A[2]=T[N-i]-T[G[N-i-1]]
    A[3]=T[G[N-i-1]]
    score=max(A)-min(A)
    if ans>score:
        ans=score
print(ans)


# Example run
# Input:
0
11
# Output:
0
def run(n, a):
    sum_a = [sum(a[0:i+1]) for i in range(n)]
    ret = sum_a[n - 1]
    for i in range(2, n-1):
        j_l = search_j(sum_a[: i], 0)
        j_r = search_j(sum_a[i:], sum_a[i - 1]) + i
        p = sum_a[j_l]
        q = sum_a[i - 1] - p
        r = sum_a[j_r] - q - p
        s = sum_a[n - 1] - r - q - p
        diff = max(p, q, r, s) - min(p, q, r, s)
        if ret > diff:
            ret = diff
    return ret


def search_j(sum_a, before):
    n = len(sum_a)
    sum_l = sum_a[0] - before
    sum_r = sum_a[n - 1] - sum_a[0]
    diff1 = sum_r - sum_l
    ret = 0
    for i in range(n - 1):
        sum_l = sum_a[i] - before
        sum_r = sum_a[n - 1] - sum_a[i]
        diff2 = sum_r - sum_l
        if diff2 < 0:
            if abs(diff1) < abs(diff2):
                return i - 1
            else:
                return i
        else:
            diff1 = diff2
            ret = i
    return ret


def read_line():
    n = int(input())
    a = list(map(int, input().split()))
    return (n, a)


def main():
    n, a = read_line()
    print(run(n, a))


if __name__ == '__main__':
    main()


# Example run
# Input:
122
0
# Output:
0
N = int(input())
A = [int(i) for i in input().split()]

diff_list = []

for c2 in range(2, N-1):
    for c1 in range(1, c2):
        P = sum(A[0:c1])
        Q = sum(A[c1:c2])
        
        for c3 in range(c2+1, N):
            R = sum(A[c2:c3])
            S = sum(A[c3:N])
            
            max_sum = max(P, Q, R, S)
            min_sum = min(P, Q, R, S)
            
            diff_list.append(max_sum - min_sum)
            
print(min(diff_list))


# Example run
# Input:
68
12536769
# Output:
12536769
n = int(input())
a = list(map(int, input().split()))
sa = [0]
for ai in a:
    sa.append(sa[-1] + ai)

def binary_search(lo, hi, v):
    while hi - lo > 1:
        mid = (hi + lo) // 2
        if sa[mid] > v:
            hi = mid
        else:
            lo = mid
    return lo

def calc(m1, m2, m3):
    if m1 <= 0 or m2 - m1 <= 0 or m3 - m2 <= 0 or n - m3 <= 0:
        return sa[n]
    values = (sa[m1], sa[m2] - sa[m1], sa[m3] - sa[m2], sa[n] - sa[m3])
    return max(values) - min(values)

ret = sa[n]
for m2 in range(2, n - 1):
    m1 = binary_search(0, m2, sa[m2] // 2)
    m3 = binary_search(m2, n, (sa[n] + sa[m2]) // 2)
    for i1 in (0, 1):
        for i2 in (0, 1):
            for i3 in (0, 1):
                ret = min(ret, calc(m1 + i1, m2 + i2, m3 + i3))
print(ret)


# Example run
# Input:
1
0
# Output:
0
N=int(input())
A=list(map(int,input().split()))

def irekae(S,T,i):
    if abs(S-T)>abs((S-A[i-1])-(T+A[i-1])):
        
        S=S-A[i-1]
        T=T+A[i-1]
        i=i-1
        
    elif abs(T-S)>abs((T-A[i])-(S+A[i])):
        #print(T,S,T-S,i)
        S=S+A[i]
        T=T-A[i]
        i=i+1
        #print(T,S,T-S,i)

    return (S,T,i)

cut=N//2
cutb=0
A_2_1=A[:cut]
S_2_0=sum(A_2_1)
A_2_2=A[cut:]
S_2_1=sum(A_2_2)


while cutb!=cut:
    #print(cut)
    cutb=cut
    S_2_0,S_2_1,cut=irekae(S_2_0,S_2_1,cut)



cut_1=cut//2
cut_2=cut
cut_3=cut+(N-cut)//2

#print(cut_1,cut_2,cut_3,N)
A_0=A[:cut_1]
S_0=sum(A_0)

A_1=A[cut_1:cut_2]
S_1=sum(A_1)

A_2=A[cut_2:cut_3]
S_2=sum(A_2)

A_3=A[cut_3:]
S_3=sum(A_3)



cutb_1=0
cutb_2=0
cutb_3=0

while (cut_1,cut_2,cut_3)!=(cutb_1,cutb_2,cutb_3):
    #print(cut_1,cut_2,cut_3,[S_0,S_1,S_2,S_3])
    cutb_1=cut_1
    cutb_2=cut_2
    cutb_3=cut_3
    

    S_0,S_1,cut_1=irekae(S_0,S_1,cut_1)
    S_1,S_2,cut_2=irekae(S_1,S_2,cut_2)
    S_2,S_3,cut_3=irekae(S_2,S_3,cut_3)


#print(cut_1,cut_2,cut_3,[S_0,S_1,S_2,S_3],max([S_0,S_1,S_2,S_3]),min([S_0,S_1,S_2,S_3]))
print(max([S_0,S_1,S_2,S_3])-min([S_0,S_1,S_2,S_3]))


# Example run
# Input:
1
-4
# Output:
4
N = int(input())

#累積和で受け取る
A = [0] * N
S = 0
for i, a in enumerate(input().split()):
    S += int(a)
    A[i] = S

answer = S

#indexは最後に回収する番号
B = 0
D = 0

for C in range(1, N - 2):
    D = max(D, C + 1)
    SB = A[B]
    SC = A[C] - A[B]
    SD = A[D] - A[C]
    SE = S - A[D]
    left_score = abs(SB - SC)
    #BCの境界の調整
    while True:
        B_ = B + 1
        SB_ = A[B_]
        SC_ = A[C] - SB_
        x = abs(SB_  - SC_)
        if left_score > x:
            B, SB, SC, left_score = B_, SB_, SC_, x
            continue
        break

    right_score = abs(SE - SD)
    while True:
        D_ = D + 1
        SD_ = A[D_] - A[C]
        SE_ = S - A[D_]
        x = abs(SD_ - SE_)
        if right_score > x:
            D, SD, SE, right_score = D_, SD_, SE_, x
            continue
        break

    score = max(SB, SC, SD, SE) - min(SB, SC, SD, SE)
    answer = min(score, answer)
print (answer)


# Example run
# Input:
12039
18501
# Output:
18501
N = int(input())
A = list(map(int, input().split()))
ans = pow(10,10)
for i in range(1, N - 2):
	for j in range(i + 1, N - 1):
		for k in range(j + 1, N):
			B = list(A[:i])
			C = list(A[i:j])
			D = list(A[j:k])
			E = list(A[k:])
			P = sum(B)
			Q = sum(C)
			R = sum(D)
			S = sum(E)
			ans = min(ans, abs(max(P, Q, R, S) - min(P, Q, R, S)))
print(ans)


# Example run
# Input:
6
143
# Output:
143
n=int(input())
*a,=map(int,input().split())

def func2(a,i):
    s1=0
    m1=10**10
    for j in range(1,i):
        t=abs(sum(a[:j])-sum(a[j:i]))
        if t<m1:
            m1=t
            s1=j
        else:
            break
    
    s2=0
    m2=10**10
    for j in range(i+1,len(a)):
        t=abs(sum(a[i:j])-sum(a[j:]))
        if t<m2:
            m2=t
            s2=j
        else:
            break
            
    *l,=map(sum,[a[:s1],a[s1:i],a[i:s2],a[s2:]])
    return abs(max(l)-min(l))

m=10**10
for i in range(2,n-2):
    t=func2(a,i)
    if t<m:
        m=t
print(m)

# Example run
# Input:
-553
324745
# Output:
10000000000
n = int(input())
a = list(map(int,input().split()))
d = 0
for i in range(0,n-3):
    for j in range(1+i,n-2):
        for k in range(1+j,n-1):
            list = [0,0,0,0]
            for index,x in enumerate(a):
                if index < (i+1):
                    list[0] += x
                elif (i+1) <= index and index < (j+1):
                    list[1]+= x
                elif (j+1) <= index and index < (k+1):
                    list[2] += x
                else:
                    list[3] += x
            tmp_max = list[0]
            tmp_min = list[0]
            for tmp in list:
                if tmp_max < tmp:
                    tmp_max = tmp
                if tmp_min > tmp:
                    tmp_min = tmp
            if i == 0 and j == 1 and k == 2:
                d = tmp_max - tmp_min
            else:
                if d > (tmp_max - tmp_min):
                    d = (tmp_max - tmp_min)
            print("i:"+str(i)+"j:"+str(j)+"k:"+str(k)+"d"+str(d))
print(d)


# Example run
# Input:
0
5071269
# Output:
0
n=int(input())
*a,=map(int,input().split())

def func2(a,i):   
    bb,bc=func3(a[:i])
    cb,cc=func3(a[i:])
    *l,=map(sum,[bb,bc,cb,cc])
    return abs(max(l)-min(l))

def func3(b):
    i=0
    s=10**10
    for j in range(1,len(b)):
        t=abs(sum(b[:j])-sum(b[j:]))
        if s>t:
            s=t
            i=j
        else:
            break
    return b[:i],b[i:]

m=10**10
for i in range(2,n-1):
    t=func2(a,i)
    if t<m:
        m=t
print(m)

# Example run
# Input:
-21323
39
# Output:
10000000000
def solve(xs, n):
    for i in range(1, n):
        xs[i] += xs[i - 1]
    l, r = (0, 2)
    xs_last = xs[n - 1]
    ans = xs_last
    for c in range(1, n - 2):
        xs_center = xs[c]
        while l < c - 1:
            if xs_center < 2 * xs[l + 1]:
                if (xs[l + 1] + xs[l]) < xs_center:
                    l += 1
                break
            l += 1
        while r < n - 2:
            if xs_last + xs_center < 2 * xs[r + 1]:
                if (xs[r + 1] + xs[r]) < xs_last + xs_center:
                    r += 1
                break
            r += 1
        pqrs = (xs[l], xs_center - xs[l], xs[r] - xs_center, xs_last - xs[r])
        ans = min(ans, max(pqrs) - min(pqrs))
    return ans

if __name__ == "__main__":
    n = int(input())
    xs = [int(s) for s in input().split()]
    print(solve(xs, n))


# Example run
# Input:
0
1418198351
# Output:
1418198351
from itertools import accumulate

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

sum_a = list(accumulate(A))
lft = 0
rgt = 2
t = sum_a[-1]
ans = t
for i in range(1,N-2):
    while sum_a[i] - sum_a[lft+1] > sum_a[lft]:
        lft += 1

    while t - sum_a[rgt+1] > sum_a[rgt] - sum_a[i]:
        rgt += 1
    p = sum_a[lft]
    q = sum_a[i] - p
    r = sum_a[rgt] - q - p
    s = t - r - q - p

    ans = min(ans, max(p,q,r,s) - min(p,q,r,s))

print(ans)


# Example run
# Input:
0
15473
# Output:
15473