from itertools import accumulate

from bisect import bisect



N = int(eval(input()))

C = [0]+list(accumulate(list(map(int,input().split()))))



def h(i,j):

  ci,cj = C[i],C[j]

  k = bisect(C, (ci+cj)//2, lo=i+1,hi=j)

  _,a,b = min((abs(ci+cj-2*c),cj-c,c-ci) for c in (C[k-1],C[k]))

  return a,b



def solve(x):

  L = h(0,x)+h(x,N)

  return max(L)-min(L)



print((min(solve(i) for i in range(2,N-1))))
from itertools import accumulate

from bisect import bisect



N = int(eval(input()))

C = [0]+list(accumulate(list(map(int,input().split()))))

S = C[N]



def solve(x):

  cx = C[x]

  k = bisect(C, cx//2, lo=1,hi=x)

  _,a,b = min((abs(cx-2*c),cx-c,c) for c in (C[k-1],C[k]))

  k = bisect(C, (S+cx)//2, lo=x+1,hi=N)

  _,c,d = min((abs(cx+S-2*c),S-c,c-cx) for c in (C[k-1],C[k]))

  L = (a,b,c,d)

  return max(L)-min(L)



print((min(solve(i) for i in range(2,N-1))))
from itertools import accumulate
from bisect import bisect

N = int(eval(input()))
C = [0] + list(accumulate(list(map(int, input().split()))))


def h(i, j):
    ci, cj = C[i], C[j]
    k = bisect(C, (ci + cj) // 2, lo=i + 1, hi=j)
    _, a, b = min((abs(ci + cj - 2 * c), cj - c, c - ci) for c in (C[k - 1], C[k]))
    return a, b


def solve(x):
    L = h(0, x) + h(x, N)
    return max(L) - min(L)


print((min(solve(i) for i in range(2, N - 1))))

from itertools import accumulate
from bisect import bisect

N = int(eval(input()))
C = [0] + list(accumulate(list(map(int, input().split()))))
S = C[N]


def solve(x):
    cx = C[x]
    k = bisect(C, cx // 2, lo=1, hi=x)
    _, a, b = min((abs(cx - 2 * c), cx - c, c) for c in (C[k - 1], C[k]))
    k = bisect(C, (S + cx) // 2, lo=x + 1, hi=N)
    _, c, d = min((abs(cx + S - 2 * c), S - c, c - cx) for c in (C[k - 1], C[k]))
    L = (a, b, c, d)
    return max(L) - min(L)


print((min(solve(i) for i in range(2, N - 1))))

- - -def h(i, j): -    ci, cj = C[i], C[j] -    k = bisect(C, (ci + cj) // 2, lo=i + 1, hi=j) -    _, a, b = min((abs(ci + cj - 2 * c), cj - c, c - ci) for c in (C[k - 1], C[k])) -    return a, b +S = C[N] -    L = h(0, x) + h(x, N) +    cx = C[x] +    k = bisect(C, cx // 2, lo=1, hi=x) +    _, a, b = min((abs(cx - 2 * c), cx - c, c) for c in (C[k - 1], C[k])) +    k = bisect(C, (S + cx) // 2, lo=x + 1, hi=N) +    _, c, d = min((abs(cx + S - 2 * c), S - c, c - cx) for c in (C[k - 1], C[k])) +    L = (a, b, c, d)