Felix6326727/beyond-accuracy-code-comprehension huggingface dataset:
n = int(input())
a = list(map(int, input().split()))

t = sum(a) // 4
i = 0
p1 = 0
while True:
    if p1 + a[i] > t:
        if t - p1 >= a[i] // 2:
            p1 += a[i]
            i += 1
            break
        else:
            break
    else:
        p1 += a[i]
        i += 1

q1 = 0
while True:
    if q1 + a[i] > t:
        if t - q1 >= a[i] // 2:
            q1 += a[i]
            i += 1
            break
        else:
            break
    else:
        q1 += a[i]
        i += 1

r1 = 0
while True:
    if r1 + a[i] > t:
        if t - r1 >= a[i] // 2:
            r1 += a[i]
            i += 1
            break
        else:
            break
    else:
        r1 += a[i]
        i += 1

s1 = sum(a[i:])

j = 0
s2 = 0
while True:
    if j == 0:
        j = a.pop()
    if s2 + j > t:
        if t - s2 >= j // 2:
            s2 += j
            j = 0
            break
        else:
            break
    else:
        s2 += j
        j = 0

r2 = 0
while True:
    if j == 0:
        j = a.pop()
    if r2 + j > t:
        if t - r2 >= j // 2:
            r2 += j
            j = 0
            break
        else:
            break
    else:
        r2 += j
        j = 0

q2 = 0
while True:
    if j == 0:
        j = a.pop()
    if q2 + j > t:
        if t - q2 >= j // 2:
            q2 += j
            j = 0
            break
        else:
            break
    else:
        q2 += j
        j = 0

j += sum(a)
p2 = j

res1 = max(p1,q1,r1,s1) - min(p1,q1,r1,s1)
res2 = max(p2,q2,r2,s2) - min(p2,q2,r2,s2)
res = min(res1,res2)
print(res)

# Example run
# Input:
-62350830
260615611
# Output:
260615611
n=int(input())
A=list(map(int,input().split()))
S=[0] #累積和
cnt=0
for i in range(n):
    cnt+=A[i]
    S.append(cnt)

ans=S[n]
d1=1 #しきり1(i個目の右で切る)
d3=2 #しきり3

for d2 in range(2,n-1): #しきり2を全探索
    L=S[d2]
    R=S[n]-S[d2]
    for i in range(d1,d2): #しきり1を右に動かす
        if S[i]>=L/2:
            break
    if abs(S[i]-L/2)>=abs(S[i-1]-L/2): #しきり1を更新
        d1=i-1
    else:
        d1=i
    for i in range(d3,n): #しきり3を右に動かす
        if S[i]-S[d2]>=R/2:
            break
    if abs(S[i]-S[d2]-R/2)>=abs(S[i-1]-S[d2]-R/2): #しきり3を更新
        d3=i-1
    else:
        d3=i
    s=[S[d1],S[d2]-S[d1],S[d3]-S[d2],S[n]-S[d3]] #4分割の結果
    ans=min(max(s)-min(s),ans) #解答の更新
    #print(d1,d2,d3,s)
print(ans)

# Example run
# Input:
0
374
# Output:
0
#102_D
N = int(input())
A = [int(i) for i in input().split()]
s = 0
AS = []
for i in range(N):
    s += A[i]
    AS.append(s)
#尺取り法
Ans = AS[N-1]+1
L = 0
R = 2
for Mid in range(1, N-2):
    Sum_L = AS[Mid]
    Sum_R = AS[N-1]-Sum_L
    while abs((Sum_L - AS[L]) - AS[L]) > abs((Sum_L - AS[L+1]) - AS[L+1]):
        L += 1

    while abs((AS[N-1]-AS[R]) - (AS[R]-Sum_L)) > abs((AS[N-1]-AS[R+1]) - (AS[R+1]-Sum_L)):
        R += 1
    P = AS[L]
    Q = Sum_L-AS[L]
    S = AS[R]-Sum_L
    T = AS[N-1]-AS[R]
    #print(Mid,L,R,abs(max(P,Q,S,T)-min(P,Q,S,T)))
    Ans = min(Ans, abs(max(P, Q, S, T)-min(P, Q, S, T)))
print(Ans)


# Example run
# Input:
1
-7798904
# Output:
-7798903
def solve():
    N = int(input())
    A = tuple(map(int, input().split()))

    from itertools import accumulate
    CA = list(accumulate(A))

    mindiff = CA[-1]

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

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

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

S = [0]
for i,a in enumerate(A,1):
    S.append(S[i-1]+a)
Sn = S[-1]
ans = Sn

i0 = 1
k0 = 3
for j in range(2,N-1):
    tmpS = S[j]
    for i in range(i0,j):
        if S[i] > tmpS/2:
            break
    i -= abs(tmpS - S[i] - S[i]) > abs(tmpS - S[i-1] - S[i-1])
    i0 = i
    for k in range(k0,N):
        if S[k]-tmpS > (Sn-tmpS)/2:
            break
    k -= abs(Sn-S[k] - S[k] + tmpS) > abs(Sn - S[k-1] - S[k-1] + tmpS)
    k0 = k
    P = (S[i],S[j]-S[i],S[k]-S[j],S[-1]-S[k])
    ans = min(ans,max(P)-min(P))
print(ans)

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

arrSum = list(sum(A[:idx+1]) for idx in range(N))

ans = float('inf')
##2分する区切り目の左のインデックスi: 1 ～ (N - 3)
nextJ, nextK = -1, -1
for i in range(1, (N - 3) + 1):
    ##左側の区間の右端はi番目まである
    ##区切り目の左のインデックスj: 0 ～ (i - 1)
    buf = float('inf')
    nextJ = max(nextJ, 0)
    for j in range(nextJ, i):
        sumB = arrSum[j]
        sumC = arrSum[i] - arrSum[j]
        d = abs(sumB - sumC)
        if buf > d:
            buf = d
            P = sumB
            Q = sumC
            nextJ = j
        else:
            break
    ##右側の区間の左端は(i + 1)番目
    ##区切り目の左のインデックスk: (i + 1) ～ (N - 2)
    buf = float('inf')
    nextK = max(nextK, (i + 1))
    for k in range(nextK, (N - 2) + 1):
        sumD = arrSum[k] - arrSum[i]
        sumE = arrSum[N-1] - arrSum[k]
        d = abs(sumD - sumE)
        if buf > d:
            buf = d
            R = sumD
            S = sumE
            nextK = k
        else:
            break
    arr = list((P, Q, R, S))
    ans = min(ans, max(arr) - min(arr))
print(ans)

# Example run
# Input:
-274078593
-28418
# Output:
inf
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 = 2

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(SD-SE)
  # DEの境界の調整
  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(answer,score)
  
print(answer)
    

# Example run
# Input:
5150
0
# Output:
0
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])
    print(midL,i,midR)
    print(sum_[midL],sum_[i]-sum_[midL],sum_[midR]-sum_[i],sum_[-1]-sum_[midR])
    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:
-597500
-530037
# Output:
[-530037]
inf
def sum(ar,st,en):
    s=0
    for i in range(st,en+1):
        s+=ar[i]
    return s
max_value=999999999999999999999

def k_part(k,n,ar):
    res=max_value
    for o in range(n):
        l_min=0
        for r_min in range(l_min,n):
            min_s=sum(ar,l_min,r_min)
            dp=[[max_value for i in range(n)] for j in range(k)]
            dp[0][r_min-l_min]=min_s
            for p in range(1,k):
                for l_max in range(r_min,n):
                    for r_max in range(l_max,n):
                        max_s=sum(ar,l_max+1,r_max)
                        if max_s>=min_s:
                            dp[p][r_max]=min(dp[p][r_max],max(max_s,dp[p-1][l_max]))
            if dp[k-1][n-1]==max_value:
                continue
            res=min(res,dp[k-1][n-1]-min_s)
        tmp=[0]*n
        for i in range(n):
            tmp[(i-1+n)%n]=ar[i]
        for i in range(n):
            ar[i]=tmp[i]
    return res

k=4
n=int(input())
deps=[int(i) for i in input().strip().split(" ")]
print(k_part(k,n,deps))

# Example run
# Input:
-5
36241
# Output:
999999999999999999999
# import math
# N = int(input())
# A_list = [int(i) for i in input().split()]


# def divide_list(num_list):
#   left = 0
#   right = len(num_list)
#   minimum = 10**9

#   while True:
#     c = (left+right)//2
#     left_sum = sum(num_list[:c])
#     right_sum = sum(num_list[c:])
#     diff = abs(left_sum - right_sum)

#     if minimum > diff:
#       minimum = diff
#       minimum_left = left
#       minimum_right = right
    
#     if left_sum > right_sum:
#       right = c
#     elif left_sum < right_sum:
#       left = c
#     else:
#       return num_list[:c], num_list[c:]

#     if c==left or c==right:
#       c = (minimum_left+minimum_right)//2
#       return num_list[:c], num_list[c:]

# tmp1_list, tmp2_list = divide_list(A_list)
# print(tmp1_list, tmp2_list)
# tmp11_list, tmp12_list = divide_list(tmp1_list)
# tmp21_list, tmp22_list = divide_list(tmp2_list)
# print(tmp11_list, tmp12_list, tmp21_list, tmp22_list)

# ans_list = [sum(tmp11_list), sum(tmp12_list), sum(tmp21_list), sum(tmp22_list)]
# ans_list.sort()

# print(ans_list[-1] - ans_list[0])


#102_D
N = int(input())
A = list(map(int, input().split()))
s = 0
AS = []
for i in range(N):
    s += A[i]
    AS.append(s)
#尺取り法
Ans = AS[N-1]+1
L = 0
R = 2
for Mid in range(1, N-2):
    Sum_L = AS[Mid]
    Sum_R = AS[N-1]-Sum_L
    while abs(Sum_L-2*AS[L]) > abs(Sum_L-2*AS[L+1]):
        L += 1

    while abs(AS[N-1]-2*AS[R]+AS[Mid]) > abs(AS[N-1]-2*AS[R+1]+AS[Mid]):
        R += 1
    P = AS[L]
    Q = AS[Mid]-AS[L]
    S = AS[R]-AS[Mid]
    T = AS[N-1]-AS[R]
    #print(Mid,L,R,abs(max(P,Q,S,T)-min(P,Q,S,T)))
    Ans = min(Ans, abs(max(P, Q, S, T)-min(P, Q, S, T)))
print(Ans)


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

SUM=sum(A)

left=0
i=0
while left<SUM/4:
    left+=A[i]
    i+=1


right=0
j=N-1
while right<SUM/4:
    right+=A[j]
    j-=1



    
def middlecut(i,j):
    left=sum(A[:i])
    right=sum(A[j:])
    ANS=10**16

    for k in range(i,j):
        midle=sum(A[i:k])
        midri=sum(A[k:j])

        if ANS>max(left,right,midle,midri)-min(left,right,midle,midri):
            ANS=max(left,right,midle,midri)-min(left,right,midle,midri)

    return ANS


ANS=10**16
for l in range(max(0,i-6),min(N,i+6)):
    for m in range(max(0,j-6),min(N,j+6)):
        ANS=min(ANS,middlecut(l,m))

print(ANS)


# Example run
# Input:
-58867430
-56661351
# Output:
10000000000000000
n=int(input())
a=[int(i) for i in input().split()]

s=sum(a)
ans=s
b=a[0]
for i in range(n-3):
	b=0
	for l in range(i+2):
		b+=a[l]
	c=s-b
	j=0
	k=0
	while k-b/2<0:
		k+=a[j]
		j+=1
	if k-b/2>b/2+a[j-1]-k:
		k-=a[j-1]
	x=min(k,b-k)
	y=max(k,b-k)
	j=-1
	k=0
	while k-c/2<0:
		k+=a[j]
		j-=1
	if k-c/2>c/2+a[j+1]-k:
		k-=a[j+1]
	z=min(k,c-k)
	w=max(k,c-k)
	t=max(y,w)-min(x,z)
	if ans>t:
		ans=t
print(ans)

# Example run
# Input:
-2458
18985674
# Output:
18985674
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]
            f.sort()
            kari = f[-1]-f[0]
            if kari < res:
                res = kari
print(res)


# Example run
# Input:
-35784
55692629
# Output:
20000000002
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:
        next
    q = 0
    for j in range(i+1, n-2):
        q = q + a[j]
        if 3*q < sm - p:
            next
        r = 0
        for k in range(j+1, n-1):
            r = r + a[k]
            if 2*r < sm - p - q:
                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])

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:
-28
187
# Output:
0
def main():
    n = int(input())
    ai = [int(i) for i in input().split()]

    vs = []
    for i in range(1,n):
        p = sum(ai[0:i])
        for j in range(i+1,n):
            q = sum(ai[i:j])
            for k in range(j+1,n):
                r = sum(ai[j:k])
                s = sum(ai[k:])
                vs.append(abs(max(p,q,r,s) - min(p,q,r,s)))

    print(min(vs))
    
if __name__ == '__main__':
    main()


# Example run
# Input:
84
-121
# Output:
121
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
    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:
224
-3753932
# Output:
3753932
from statistics import *

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

ss = sum(a)
s = ss / 4

b1 = 0
j1 = 0

while b1 < s:
    b1 += a[j1]
    j1 += 1

b3 = 0
j3 = n - 1

while b3 < s:
    b3 += a[j3]
    j3 -= 1

b2 = 0
j2 = j1 + 1

while b2 < 2*s-b1:
    b2 += a[j2]
    j2 += 1


sum1_1 = b1
sum1_2 = b2
sum1_3 = ss - b1 - b2 - b3
sum1_4 = b3

sumlist1 = sorted([sum1_1, sum1_2, sum1_3, sum1_4])
range1 = sumlist1[3] - sumlist1[0]

sum2_1 = sum1_1 - a[j1-1]
sum2_2 = sum1_2 + a[j1-1]
sumlist2 = sorted([sum2_1, sum2_2, sum1_3, sum1_4])
range2 = sumlist2[3] - sumlist2[0]

sum3_3 = sum1_3 + a[j3+1]
sum3_4 = sum1_4 - a[j3+1]
sumlist3 = sorted([sum1_1, sum1_2, sum3_3, sum3_4])
range3 = sumlist3[3] - sumlist3[0]

sumlist4 = sorted([sum2_1, sum2_2, sum3_3, sum3_4])
range4 = sumlist4[3] - sumlist4[0]

sum5_2 = sum1_2 - a[j2-1]
sum5_3 = sum1_3 + a[j2-1]
sumlist5 = sorted([sum1_1, sum5_2, sum5_3, sum1_4])
range5 = sumlist5[3] - sumlist5[0]

sum6_1 = sum1_1 - a[j1-1]
sum6_2 = sum5_2 + a[j1-1]
sumlist6 = sorted([sum6_1, sum6_2, sum5_3, sum1_4])
range6 = sumlist6[3] - sumlist6[0]

sum7_3 = sum5_3 + a[j3+1]
sum7_4 = sum1_4 - a[j3+1]
sumlist7 = sorted([sum1_1, sum5_2, sum7_3, sum7_4])
range7 = sumlist7[3] - sumlist7[0]

sumlist8 = sorted([sum6_1, sum6_2, sum7_3, sum7_4])
range8 = sumlist8[3] - sumlist8[0]

print(min([range1,range2, range3, range4, range5, range6, range7,range8]))


# Example run
# Input:
-1
-21
# Output:
21
n = int(input())
ans = []
lis = list(map(int,input().split()))
num = sum(lis[:2]) - sum(lis[2:])
cou = n-1
for i in range(3,n-1):
  if abs(num) < abs(sum(lis[:i])-sum(lis[i:])):
    cou = i-1
    break
  else:
    num = sum(lis[:i])-sum(lis[i:])

li = lis[:cou]
n = len(li)
nu = sum(li[:1]) - sum(li[1:])
co = n-1
for i in range(2,n):
  if abs(nu) < abs(sum(li[:i])-sum(li[i:])):
    co = i-1
    break
  else:
    nu = sum(li[:i])-sum(li[i:])
ans.append(sum(li[:co]))
ans.append(sum(li[co:]))

l = lis[cou:]
n = len(l)
nm = sum(l[:1]) - sum(l[1:])
c = n-1
for i in range(2,n):
  if abs(nm) < abs(sum(l[:i])-sum(l[i:])):
    c = i-1
    break
  else:
    nm = sum(l[:i])-sum(l[i:])
ans.append(sum(l[:c]))
ans.append(sum(l[c:]))

  #print(lis[co:cou+c])
lisa = lis[co:cou+c]
numb = sum(lisa[:1]) - sum(lisa[1:])
coun = n-1
for i in range(2,n):
    #print(abs(numb), abs(sum(lisa[:i])-sum(lisa[i:])))
  if abs(numb) <= abs(sum(lisa[:i])-sum(lisa[i:])):
    coun = i-1
    break
  else:
    numb = sum(lisa[:i])-sum(lisa[i:])
ans[1] = sum(lisa[:coun])
ans[2] = sum(lisa[coun:])
  #print(lisa[:coun],lisa[coun:])
print(max(ans)-min(ans))

# Example run
# Input:
18
-15915
# Output:
15915
n=int(input())
A=list(map(int,input().split()))
S=[0] #累積和
cnt=0
for i in range(n):
    cnt+=A[i]
    S.append(cnt)

ans=S[n]
d1=1 #しきり1(i個目の右で切る)
d3=2 #しきり3

for d2 in range(2,n-1): #しきり2を全探索
    L=S[d2]
    R=S[n]-S[d2]
    for i in range(d1,d2): #しきり1を右に動かす
        if S[i]>=L/2:
            break
    if abs(S[i]-L/2)>=abs(S[i-1]-L/2): #しきり1を更新
        d1=i-1
    else:
        d1=i
    for i in range(d3,n): #しきり3を右に動かす
        if S[i]-S[d2]>=R/2:
            break
    if abs(S[i]-S[d2]-R/2)>=abs(S[i-1]-S[d2]-R/2): #しきり3を更新
        d3=i-1
    else:
        d3=i
    s=[S[d1],S[d2]-S[d1],S[d3]-S[d2],S[n]-S[d3]] #4分割の結果
    ans=min(max(s)-min(s),ans) #解答の更新
    #print(d1,d2,d3,s)
print(ans)

# Example run
# Input:
0
-14125092
# Output:
0
#  整数列の長さ N と整数列 listA を標準入力
N = int(input())
listA = list(map(int,input().split()))

def printData():
    print("max: " + str(max(p, q, r, s)) + " min: " + str(min(p, q, r, s)) + " p,q,r,s: " + str(p) + " " + str(q) + " "  + str(r) + " "  + str(sum(listA[right:])))
    print("left: " + str(left) + " center: " + str(center) + " right: " + str(right))
    print("minDiff_1: " + str(minDiff))

def calculation():
    p = sum(listA[0:left])
    q = sum(listA[left:center])
    r = sum(listA[center:right])
    s = sum(listA[right:])
    
    
    

left = 1
center = 2
right = 3

minDiff = 10**20

for center in range(2, N - 1):
    p = sum(listA[0:left])
    q = sum(listA[left:center])
    r = sum(listA[center:right])
    s = sum(listA[right:])
    
    minDiff = min(max(p, q, r, s) - min(p, q, r, s), minDiff)
             
    for right in range(center + 1, N):
        p = sum(listA[0:left])
        q = sum(listA[left:center])
        r = sum(listA[center:right])
        s = sum(listA[right:])
        
        minDiff = min(max(p, q, r, s) - min(p, q, r, s), minDiff)
             
        for left in range(1, center):
            p = sum(listA[0:left])
            q = sum(listA[left:center])
            r = sum(listA[center:right])
            s = sum(listA[right:])
            
            minDiff = min(max(p, q, r, s) - min(p, q, r, s), minDiff)

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

print(minDiff)

# Example run
# Input:
96
47
# Output:
47
from itertools import *
input()
s=list(accumulate(map(int,input().split())))
l,r,t=0,2,s[-1];a=t
for m in s[1:-2]:
    while abs(t-2*s[r]+m)>abs(t-2*s[r+1]+m):r+=1
    while abs(m-2*s[l])>abs(m-2*s[l+1]):l+=1
    x=sorted([s[l],m-s[l],s[r]-m,t-s[r]])
    a=min(a,x[3]-x[0])
print(a)

# Example run
# Input:
U4-K~gbBF
-25
# Output:
-25
N = int(input())
A = input().strip().split(' ')
A = [int(x) for x in A]

def returnsub(p,q,r,s):
    ls = [sum(p),sum(q),sum(r),sum(s)]
    return max(ls) - min(ls)

def bfs():
    minsub = -1
    for i1 in range(1,N-2):
        for i2 in range(i1+1,N-1):
            for i3 in range(i2+1,N):
                P = A[0:i1]
                Q = A[i1:i2]
                R = A[i2:i3]
                S = A[i3:]

                tmpsub =returnsub(P,Q,R,S)
                if minsub == -1:
                    minsub = tmpsub
                else:
                    if tmpsub < minsub:
                        minsub = tmpsub
    return minsub
print(bfs())

# Example run
# Input:
124
-63
# Output:
63
def calc_score(b, c, d, e):
    p = sum(b)
    q = sum(c)
    r = sum(d)
    s = sum(e)

    max_sum = max([p, q, r, s])
    min_sum = min([p, q, r, s])
    return abs(max_sum - min_sum)

def split_med(num_list, offset):
    cum_list = [sum(num_list[:i]) for i in range(len(num_list)+1)]
    med_ind = 0
    e = pow(10, 10)
    med = cum_list[len(cum_list)-1] // 2
    for i in range(len(cum_list)):
        if abs(med - cum_list[i]) < e:
            med_ind = i
            e = abs(med - cum_list[i])
    if med_ind + offset <= 0:
        return num_list[:1], num_list[1:]
    elif med_ind + offset >= len(num_list):
        return num_list[:len(num_list)-1], num_list[len(num_list)-1:]
    return num_list[:med_ind+offset], num_list[med_ind+offset:]

def split_and_calc_scores(num_list):
    offsets = [-2, -1, 0, 1, 2]
    scores = []
    for offset_1 in offsets:
        a_1, a_2 = split_med(num_list, offset_1)
        for offset_2 in offsets:
            b, c = split_med(a_1, offset_2)
            for offset_3 in offsets:
                d, e = split_med(a_2, offset_3)
                scores.append(calc_score(b, c, d, e))
    return scores

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

scores = split_and_calc_scores(a_list)

print(min(scores))

# Example run
# Input:
-368743555
4753830
# Output:
4753830
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:
-75096
47048984
# Output:
47048984
N = int(input())
A_list = [int(i) for i in input().split()]

Total_list = []
s = 0
for num in A_list:
  s += num
  Total_list.append(s)

L = 0
R = 2
ans = Total_list[N-1]+1
for Mid in range(1, N-2):
  sum_L = Total_list[Mid]
  sum_R = Total_list[N-1] - sum_L

  while abs(sum_L - 2*Total_list[L]) > abs(sum_L - 2*Total_list[L+1]):
    L += 1
  
  while abs((Total_list[N-1]-Total_list[R])-(Total_list[R] - sum_L)) > abs((Total_list[N-1]-Total_list[R+1])-(Total_list[R+1] - sum_L)):
    R += 1
  
  P = Total_list[L]
  Q = sum_L - Total_list[L]
  S = Total_list[R] - sum_L
  T = Total_list[N-1] - Total_list[R]

  ans = min(ans, max(P, Q, S, T) - min(P, Q, S, T))

print(ans)


# Example run
# Input:
0
-901502927
# Output:
-901502926
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:
-173725423
-1
# Output:
-1
from itertools import accumulate
N=int(input())
s=list(accumulate(map(int,input().split())))
def f(N,s):
    ans=s[-1]
    p=0
    q=2
    for i in range(2,N-1):
        while abs(s[i-1]-2*s[p])>abs(s[i-1]-2*s[p+1]):
            p+=1
        while abs(s[q]*2-s[i-1]-s[-1])>abs(s[q+1]*2-s[i-1]-s[-1]):
            q+=1
        t=[s[i-1]-s[p],s[p],s[q]-s[i-1],s[-1]-s[q]]
        ans=min(ans,max(t)-min(t))
    print(ans)
    return 
f(N,s)

# Example run
# Input:
-1
-3439167
# Output:
-3439167