Felix6326727/beyond-accuracy-code-comprehension huggingface dataset:
# D - Equal Cut
import bisect

N = int(input())
A = list(int(a) for a in input().split())

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

splt = Acum[-1]//4
#print(splt)
B = bisect.bisect_left(Acum, splt)
if abs(Acum[B]-splt) > abs(Acum[B-1]-splt):
    B -= 1
splt = Acum[B] + (Acum[-1] - Acum[B])//3
#print(splt)
C = bisect.bisect_left(Acum, splt)
if abs(Acum[C] - splt) > abs(Acum[C-1] - splt):
    C -= 1
    if C == B:
        C += 1
splt = Acum[C] + (Acum[-1] - Acum[C])//2
#print(splt)
D = bisect.bisect_left(Acum, splt)
if abs(Acum[D] - splt) > abs(Acum[D-1] - splt):
    D -= 1
    if D == C:
        D += 1
#print(splt)
#print(B, C, D)
#print(Acum)
ans = list((Acum[B], Acum[C]-Acum[B], Acum[D]-Acum[C],Acum[-1]-Acum[D]))
print(abs(min(ans) - max(ans)))

# Example run
# Input:
0
155
# Output:
0
from itertools import *
n=int(input())
s=[0]+list(accumulate(map(int,input().split())))
l,r,ans=0,2,s[n]
for m in range(1,n-2):
    p,q=s[m],s[n]
    while abs(p-s[l]*2)>abs(p-s[l+1]*2):l+=1
    while abs(q-s[r]*2+p)>abs(q-s[r+1]*2+p):r+=1
    x=sorted([s[l],p-s[l],s[r]-p,q-s[r]])
    ans=min(ans,x[3]-x[0])
print(ans)

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

t = list(0 for i in range(n+1))
for i in range(1, n+1):
    t[i] = t[i-1] + a[i-1]

def sum_a(i, j):
    return t[j]-t[i]

min_dif = sum_a(0, n)

i = 0
k = 2
for j in range(2, n-1):
    u = sum_a(0, j) // 2
    while sum_a(0, i) < u:
        i = i + 1
    if abs(sum_a(0, i-1)-sum_a(i-1, j)) < abs(sum_a(0, i) - sum_a(i, j)): 
        i = i - 1
    p = sum_a(0, i)
    q = sum_a(i, j)

    u = sum_a(j, n) // 2
    while sum_a(j, k) < u:
        k = k + 1
    if abs(sum_a(j, k-1)-sum_a(k-1, n)) < abs(sum_a(j, k) - sum_a(k, n)): 
        k = k - 1
    r = sum_a(j, k)
    s = sum_a(k, n)

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

print(min_dif)


# Example run
# Input:
0
1418415203
# Output:
0
from bisect import bisect_left
N = int(input())
A = [int(x) for x in input().split(' ')]
sA = [sum(A[:i]) for i in range(1, len(A) + 1)]

def score(first, second, third):
    PQRS = [sum(A[:first]), sum(A[first:second]),
            sum(A[second:third]), sum(A[third:])]
    return max(PQRS) - min(PQRS)

def search(second):
    first = bisect_left(sA[:second], sum(A[:second]) / 2)
    third = bisect_left([sa - sA[second] for sa in sA[second:]], sum(A[second:]) / 2) + second

    return min([score(fi, second, ti) for fi, ti in \
        [[first, third], [first+1, third], [first, third+1], [first+1, third+1]]])

best = float('inf')
for second in range(2, N - 2):
    best = min(best, search(second))
print(best)

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

def df(a,b):
    mint = B[b-1]
    pr = qs = 0
    for i in range(a+1,b):
        if a == 0:
            pr = B[i-1]
            qs = B[b-1]-B[i-1]
        else:
            pr = B[i-1]-B[a-1]
            qs = B[b-1]-B[i-1]
        v = abs(pr - qs)
        # print("@",pr,qs,v)
        if v < mint:
            mint = v
            p = pr
            q = qs
        else:
            break
    return p,q


minu = s
for i in range(2,len(A)-1):
    p,q = df(0,i)
    r,s = df(i,len(A))
    # print((p,q,r,s))
    t = max(p,q,r,s) - min(p,q,r,s)
    if t < minu:
        minu = t
print(minu)


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

t = list(0 for i in range(n+1))
for i in range(1, n+1):
    t[i] = t[i-1] + a[i-1]

def sum_a(i, j):
    return t[j]-t[i]

min_dif = sum_a(0, n)

for i in range(2, n-1):
    j0 = 0
    j1 = i
    u = sum_a(0, i) // 2
    while j1 - j0 > 1:
        j = (j0+j1) // 2
        if sum_a(0, j) <= u: 
            j0 = j
        else:
            j1 = j
    if abs(sum_a(0, j0)-sum_a(j0, i)) < abs(sum_a(0, j1) - sum_a(j1, i)): 
        p = sum_a(0, j0)
        q = sum_a(j0, i)
    else:
        p = sum_a(0, j1)
        q = sum_a(j1, i)

    j0 = i
    j1 = n
    u = sum_a(i, n) // 2
    while j1 - j0 > 1:
        j = (j0+j1) // 2
        if sum_a(i, j) <= u: 
            j0 = j
        else:
            j1 = j
    if abs(sum_a(i, j0)-sum_a(j0, n)) < abs(sum_a(i, j1) - sum_a(j1, n)): 
        r = sum_a(i, j0)
        s = sum_a(j0, n)
    else:
        r = sum_a(i, j1)
        s = sum_a(j1, n)

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

print(min_dif)


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

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

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

    # right side
    r = CA[right]
    s = CA[-1]
    while right < N:
        ri = CA[right+1] - CA[c]
        si = CA[-1] - CA[right+1]
        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))
print(mindiff)


# Example run
# Input:
-1350
-161
# Output:
-161
#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]) - A[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
8064202
# Output:
8064203
def bsearch(target, ok, ng):
    while ng - ok > 1:
        mid = (ok + ng) // 2
        if cumA[mid] <= target:
            ok = mid
        else:
            ng = mid
    return ok

def tmpB1(ok, B):
    # B[1]はokまたはok + 1
    if abs((cumA[ok] - cumA[B[0]]) - (cumA[B[2]] - cumA[ok])) <= \
        abs((cumA[ok + 1] - cumA[B[0]]) - (cumA[B[2]] - cumA[ok + 1])):
        return ok
    else:
        return ok + 1

N = int(input())
A = [int(x) for x in input().split()]

cumA = [A[0]] + [0] * (N - 1)  # 累積和
for i in range(1, N):
    cumA[i] = cumA[i - 1] + A[i]

B = [-1, -1, N - 1]
S = [0, 0, cumA[N - 1], 0]
newB = [0, 0, 0]
newS = [0, 0, 0, 0]
diff = cumA[N - 1]
B0defined = False
B2defined = False

while(B[2] - B[0] > 2):
    m = min(S)
    diff = max(S) - min(S)
    if S[0] == m and not B0defined:
        newB[0] = B[0] + 1
        newB[2] = B[2]
        newS[0] = cumA[newB[0]]
        newS[3] = S[3]

        # targetを超えない最大のB[1]の位置を探す
        target = (cumA[newB[2]] + cumA[newB[0]]) / 2
        ok = newB[0]
        ng = newB[2] # B[0] < B[2]ならば
        ok = bsearch(target, ok, ng)
        newB[1] = tmpB1(ok, newB)
        newS[1] = cumA[newB[1]] - cumA[newB[0]]
        newS[2] = cumA[newB[2]] - cumA[newB[1]]

        new_diff = max(newS) - min(newS)
        if new_diff < diff:
            for i in range(4):
                S[i] = newS[i]
            for i in range(3):
                B[i] = newB[i]
        else:
            B0defined = True

    elif S[3] == m and not B2defined:
        newB[2] = B[2] - 1
        newB[0] = B[0]
        newS[3] = cumA[N - 1] - cumA[newB[2]]
        newS[0] = S[0]

        # targetを超えない最大のB[1]の位置を探す
        target = (cumA[newB[2]] + cumA[newB[0]]) / 2
        ok = newB[0]
        ng = newB[2] # B[0] < B[2]ならば
        ok = bsearch(target, ok, ng)
        newB[1] = tmpB1(ok, newB)
        newS[1] = cumA[newB[1]] - cumA[newB[0]]
        newS[2] = cumA[newB[2]] - cumA[newB[1]]

        new_diff = max(newS) - min(newS)
        if new_diff < diff:
            for i in range(4):
                S[i] = newS[i]
            for i in range(3):
                B[i] = newB[i]
        else:
            B2defined = True
    else:
        break

print(max(S) - min(S))


# Example run
# Input:
0
-5
# Output:
5
N = int(input())
A = list(map(int, input().split()))
cum_sum = [0] + [sum(A[:i + 1]) for i in range(N)]

ans = float("inf")


def get_diff(left, center, right):
    return abs((cum_sum[center] - cum_sum[left]) - (cum_sum[right] - cum_sum[center]))


l_point = 1
r_point = 3

for C in range(2, N):
    while l_point + 1 < C and get_diff(0, l_point, C) > get_diff(0, l_point + 1, C):
        l_point += 1
    while r_point + 1 < N and get_diff(C, r_point, N) > get_diff(C, r_point + 1, N):
        r_point += 1

    pqrs = [cum_sum[l_point], cum_sum[C] - cum_sum[l_point], cum_sum[r_point] - cum_sum[C], cum_sum[N] - cum_sum[r_point]]
    ans = min(ans, max(pqrs) - min(pqrs))

print(ans)


# Example run
# Input:
-153
123746462
# Output:
inf
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):
  while abs(cumA[i] - cumA[l_index] - cumA[l_index]) > abs(cumA[i] - cumA[l_index + 1] - cumA[l_index + 1]):
    l_index += 1

  while abs(cumA_max - cumA[r_index] - (cumA[r_index] - cumA[i])) > abs(cumA_max - cumA[r_index + 1] - (cumA[r_index + 1] - cumA[i])):
    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:
-31584
2421820
# Output:
2421820
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-2):
    t=func2(a,i)
    if t<m:
        m=t
print(m)

# Example run
# Input:
-4
0
# Output:
10000000000
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

for j in range(2,N-1):
    tmpS = S[j]
    l,r = 1,j
    while l+1 < r:
        i = (l+r)//2
        if S[i] > tmpS/2:
            r = i
        else:
            l = i
    i = r
    i -= (tmpS - S[i] - S[i]) < (tmpS - S[i-1] - S[i-1])
    l,r = j+1,N
    while l+1 < r:
        k = (l+r)//2
        if S[k]-tmpS > (Sn-tmpS)/2:
            r = k
        else:
            l = k
    k = r
    k -= (Sn-S[k] - S[k] + tmpS) < (Sn - S[k-1] - S[k-1] + tmpS)
    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:
-2147483648
3556893
# Output:
3556893
n = int(input())
a_list = [int(x) for x in input().split()]

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

ans = temp_list[-1]
for m in range(2, n - 2):
    gap_min = temp_list[m]
    l = 1
    while l < m:
        gap = abs((temp_list[m] - temp_list[l]) - (temp_list[l] - temp_list[0]))
        if gap < gap_min:
            gap_min = gap
            l += 1
        else:
            l -= 1
            break
    else:
        l -= 1
    
    gap_min = temp_list[n]
    r = m + 1
    while r < n:
        gap = abs((temp_list[n] - temp_list[r]) - (temp_list[r] - temp_list[m]))
        if gap < gap_min:
            gap_min = gap
            r += 1
        else:
            r -= 1
            break
    else:
        r -= 1
    
    num_list = sorted([temp_list[l] - temp_list[0], temp_list[m] - temp_list[l], temp_list[r] - temp_list[m], temp_list[n] - temp_list[r]])
    ans_temp = num_list[3] - num_list[0]
    if ans_temp < ans:
        ans = ans_temp
print(ans)

# Example run
# Input:
1
318299
# Output:
318299
N = int(input())
A = list(map(int, input().split()))
ans = pow(10,10)
for i in range(1, N):
	for j in range(i + 1, N):
		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)
			temp = max(P, Q, R, S) - min(P, Q, R, S)
			if temp < ans:
				ans = temp
print(ans)


# Example run
# Input:
-80
-7517
# Output:
10000000000
n = int(input())
a = list(map(int, input().split()))
s = [0]
for i in range(n):
    s.append(s[i] + a[i])
ans = s[n] + 1

for x in range(n):
    for y in range(x + 1, n):
        for z in range(y + 1, n):
            b = s[x] - s[0]
            c = s[y] - s[x]
            d = s[z] - s[y]
            e = s[n] - s[z]
            diff = max(b, c, d, e) - min(b, c, d, e)
            ans = min(diff, ans)

print(ans)

# Example run
# Input:
0
-31591
# Output:
1
N = int(input())
A = list(map(int,input().split()))
S = [0 for _ in range(N+1)]
for i in range(1,N+1):
    S[i] = S[i-1]+A[i-1] #strictly increasing
def binSearch(L,R):
    """
    L,R should be 1..N
    and L < R
    search for min(n) s.t. S[n]-S[L-1] >= half
    """
    total = S[R]-S[L-1]
    half = total/2 #it's float
    ok = R
    ng = L-1
    while ok >= ng+2:
        mid = (ok+ng)//2
        if S[mid]-S[L-1] >= half:
            ok = mid
        else:
            ng = mid
    deltaNG = abs(S[ng]-S[L-1]-half)
    deltaOK = abs(S[ok]-S[L-1]-half)
    if deltaNG < deltaOK:
        x = S[ng]-S[L-1]
        y = S[R] - S[ng]
    else:
        x = S[ok]-S[L-1]
        y = S[R] - S[ok]
    return(max(x,y),min(x,y))
def calcForMid(M):
    a,b = binSearch(1,M)
    c,d = binSearch(M+1,N)
    l = sorted([a,b,c,d])
    return(l[3]-l[0])
ans = S[N]
for m in range(2,N-1):
    ans = min(ans,calcForMid(m))
print(ans)

# Example run
# Input:
1
-2
# Output:
-2
def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = [0] * len(A)
    s = 0
    for i, a in enumerate(A):
        s += a
        S[i] = s 
    l = 0
    b = 2
    m = S[-1]
    for c in range(1, N - 2):
        p, q = S[l], S[c] - S[l]
        while l + 1 < c:
            pn, qn = S[l + 1], S[c] - S[l + 1]
            if abs(pn - qn) >= abs(p - q):
                break
            p, q = pn, qn
            l += 1
        if b <= c:
            b = c + 1
        r, s = S[b] - S[c], S[-1] - S[b]
        while b + 1 < N - 1:
            rn, sn = S[b + 1] - S[c], S[-1] - S[b + 1]
            if abs(rn - sn) >= abs(r - s):
                break
            r, s = rn, sn
            b += 1
        m = min(m, max(p, q, r, s) - min(p, q, r, s))
    return m


print(main())


# Example run
# Input:
1
-67092
# Output:
-67092
import bisect
N = int(input())
A = [int(i) for i in input().split()]

b = 0
B = [0]
for i in A:
    b += i
    B.append(b)


answer = b*4
for cut2 in range(3, N):

    cut1 = max(2, bisect.bisect_right(B[:cut2], B[cut2]//2))

    cut3 = max(2, bisect.bisect_right(B[cut2-1:], (b - B[cut2-1])//2+B[cut2-1])) + cut2-1

    S = [B[cut1-1], B[cut2-1]-B[cut1-1], B[cut3-1]-B[cut2-1], b-B[cut3-1]]
    answer = min(answer, max(S)-min(S))

print(answer)

# Example run
# Input:
-368743555
4753830
# Output:
19015320
def hersCode(L, x):
  ng = 0
  ok = len(L) - 1
  while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if L[mid] > x:
      ok = mid
    else:
      ng = mid
  return ok

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

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

t = hersCode(cs,(cs[N - 2] - A[0]) // 2)
b = 0
c = t
d = N - 2
e = N - 1
B = cs[b]
C = cs[c] - cs[b]
D = cs[d] - cs[c]
E = cs[e] - cs[d]

while True:
  t = C - D
  if t >= 0:
    if abs(t - 2 * A[c]) < t:
      C -= A[c]
      D += A[c]
      c -= 1
    else:
      break
  else:
    if abs(-t - 2 * A[c + 1]) < -t:
      C += A[c + 1]
      D -= A[c + 1]
      c += 1
    else:
      break

Ans = max(B, C, D, E) - min(B, C, D, E)
while True:
  if B >= C and B >= D and E >= C and E >=D:
    break
  if b == c - 1 and c == d - 1:
    break
  if b == c - 1:
    E += A[d]
    D -= A[d]
    d -= 1
  elif c == d - 1:
    B += A[b + 1]
    C -= A[b + 1]
    b += 1
  else:
    if B + A[b + 1] < E + A[d]:
      B += A[b + 1]
      C -= A[b + 1]
      b += 1
    else:
      E += A[d]
      D -= A[d]
      d -= 1
  while True:
    t = C - D
    if t >= 0:
      if abs(t - 2 * A[c]) < t:
        C -= A[c]
        D += A[c]
        c -= 1
      else:
        break
    else:
      if abs(-t - 2 * A[c + 1]) < -t:
        C += A[c + 1]
        D -= A[c + 1]
        c += 1
      else:
        break
  t = max(B, C, D, E) - min(B, C, D, E)
  if Ans > t:
    Ans = t
print(Ans)

# Example run
# Input:
1
0
# Output:
0
N = int(input())
A = [int(n) for n in input().split()]
anser_list = []
for i in range(1, N - 2):
    for j in range(i, N - 1):
        for k in range(j, N):
            divide_list = [sum(A[:i]), sum(A[i:j]), sum(A[j:k]), sum(A[k:])]
            anser_list.append(max(divide_list) - min(divide_list))
print(min(anser_list))


# Example run
# Input:
55
715035410
# Output:
715035410
# 2つの部分列に分ける
def eqcut(start, end):
    target = (cumA[start - 1] + cumA[end]) / 2  # start = 0のときcumA[-1] = 0

    ok = start
    ng = end + 1
    while ng - ok > 1:
        mid = (ok + ng) // 2
        if cumA[mid] <= target:
            ok = mid
        else:
            ng = mid
    # okかok+1
    if ok == end:
        return ok
    else:
        diff1 = abs( (cumA[end] - cumA[ok]) - (cumA[ok] - cumA[start - 1]))
        diff2 = abs( (cumA[end] - cumA[ok + 1]) - (cumA[ok + 1] - cumA[start - 1]))
        if diff1 <= diff2:
            return ok
        else:
            return ok + 1
            

N = int(input())
A = [int(x) for x in input().split()]

cumA = [A[0]] + [0] * N  # 累積和  cumA[-1] = 0となるように最後にダミー
for i in range(1, N):
    cumA[i] = cumA[i - 1] + A[i]

S = [0, 0, 0, 0]
B = [0, 0]
ans = cumA[N - 1]
# 3本の境界線のうち中央を探す
for i in range(1, N - 2):
    B[0] = eqcut(0, i)  # A[0], ..., A[i]を要素の総和が最小となる2つの部分列に分ける
    S[0] = cumA[B[0]]
    S[1] = cumA[i] - cumA[B[0]]

    B[1] = eqcut(i + 1, N - 1)
    S[2] = cumA[B[1]] - cumA[i]
    S[3] = cumA[N - 1] - cumA[B[1]]

    ans = min(ans, max(S) - min(S))

print(ans)

# Example run
# Input:
0
1950
# Output:
1950
n = int(input())
a_list = [int(x) for x in input().split()]

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

l = 1
r = 3
ans = temp_list[-1]
for m in range(2, n - 2):
    gap_min = abs((temp_list[m] - temp_list[l]) - (temp_list[l] - temp_list[0]))
    while l < m:
        l += 1
        gap = abs((temp_list[m] - temp_list[l]) - (temp_list[l] - temp_list[0]))
        if gap < gap_min:
            gap_min = gap
        else:
            l -= 1
            break
    else:
        l -= 1
    
    gap_min = abs((temp_list[n] - temp_list[r]) - (temp_list[r] - temp_list[m]))
    while r < n:
        r += 1
        gap = abs((temp_list[n] - temp_list[r]) - (temp_list[r] - temp_list[m]))
        if gap < gap_min:
            gap_min = gap
        else:
            r -= 1
            break
    else:
        r -= 1
    
    num_list = sorted([temp_list[l] - temp_list[0], temp_list[m] - temp_list[l], temp_list[r] - temp_list[m], temp_list[n] - temp_list[r]])
    ans_temp = num_list[3] - num_list[0]
    if ans_temp < ans:
        ans = ans_temp
print(ans)

# Example run
# Input:
1
126
# Output:
126
n = int(input())
A = list(map(int, input().split()))
F = [int()] * n
F[0] = A[0]
for b in range(1, n):
    F[b] = F[b - 1] + A[b]


x = F[-1] // 4
res = [0]*4
i = 0
for s in range(3):
    res[s] += A[i]
    for t in range(i + 1, n - 1):
        if abs(x - res[s]) > abs(x - res[s] - A[t]):
            res[s] += A[t]
        else:
            i = t
            break
res[3] = F[-1] - F[i - 1]
print(max(res) - min(res))

# Example run
# Input:
1
17917685
# Output:
17917685
from itertools import *
n = int(input())
a = [int(i) for i in input().split()]
ans = 999999999999999
for t in list(combinations(range(1, n), 3)):
    b = a[0:t[0]]
    c = a[t[0]:t[1]]
    d = a[t[1]:t[2]]
    e = a[t[2]:]
    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:
-2147483648
2981
# Output:
999999999999999
N=int(input())
alist=list(map(int,input().split()))

allsum=sum(alist)
#print(alist,allsum)
ptt_list=[-1,-1,-1]

dlist=[]
asum=0
for i in range(N):
  asum+=alist[i]
  dlist.append(abs(allsum-2*asum))
#print(dlist)
min_d=10**9
part2=-1
for i in range(N):
  if dlist[i]<min_d:
    min_d=dlist[i]
    part2=i
    
#print(part2)
plist1=alist[:part2+1]
plist2=alist[part2+1:]
#print(plist1,plist2)

p1sum=sum(plist1)
p2sum=sum(plist2)
dlist=[]
asum=0
for i in range(len(plist1)):
  asum+=plist1[i]
  dlist.append(abs(p1sum-2*asum))
#print(dlist)
min_d=10**9
part1=-1
for i in range(len(plist1)):
  if dlist[i]<min_d:
    min_d=dlist[i]
    part1=i

dlist=[]
asum=0
for i in range(len(plist2)):
  asum+=plist2[i]
  dlist.append(abs(p2sum-2*asum))
#print(dlist)
min_d=10**9
part3=-1
for i in range(len(plist2)):
  if dlist[i]<min_d:
    min_d=dlist[i]
    part3=i

ptt_list=[part1,part2,part2+1+part3]
#print(ptt_list)

sum1=sum(alist[:ptt_list[0]+1])
sum2=sum(alist[ptt_list[0]+1:ptt_list[1]+1])
sum3=sum(alist[ptt_list[1]+1:ptt_list[2]+1])
sum4=sum(alist[ptt_list[2]+1:])

sum_list=[sum1,sum2,sum3,sum4]
sum_list.sort()
print(sum_list[-1]-sum_list[0])

# Example run
# Input:
-696034559
-16
# Output:
16
N=int(input())
a=list(map(int,input().split()))
def check(x):
  d = [0] * 4
  p = 0
  for i in range(N):
    d[p] += a[i]
    if d[p] >= x: p += 1
    if p > 3: p -= 1
  return min(d) >= x
def solve(r):
  d=[0]*4
  p=0
  for i in range(N):
    d[p] += a[i]
    if d[p] > r:
      d[p] -= a[i]
      p+=1
      if p > 3: return False
      d[p]+=a[i]
  return True
ok = 0
ng = N * max(a)
m = 0
while ng - ok > 1:
  m = (ok + ng) // 2
  if check(m):
    ok = m
  else: ng = m
mn = ok+0
ng = max(mn, max(a)) - 1
ok = N * max(a)
m = 0
while ok - ng > 1:
  m = (ok + ng) // 2
  if solve(m):
    ok = m
  else: ng = m
print(ok-mn)



# Example run
# Input:
6
-121
# Output:
-726
N = int(input())
A = list(map(int,input().split()))
S = [0]
for i in range(N):
        S.append(S[i]+A[i])
a = 1
c = 3
ans = S[N]
for b in range(2,N-1):
        while abs(S[b]-S[a]-S[a]) > abs(S[b]-S[a+1]-S[a+1]):
                a += 1
        while abs((S[N]-S[c]) - (S[c]-S[b])) > abs((S[N]-S[c+1]) - (S[c+1]-S[b])):
                c += 1
        ans = min(ans,(max(S[N]-S[c],S[c]-S[b],S[b]-S[a],S[a]-S[0])-min(S[N]-S[c],S[c]-S[b],S[b]-S[a],S[a]-S[0])))
print(ans)


# Example run
# Input:
0
-46849
# Output:
0
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 = [-1, 0, 1]
    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:
-6607
-29366
# Output:
29366