Problem:


Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.
The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.
Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.
We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994



Sample input:
5
3 2 4 1 2


Sample output:
2

Submission status: Runtime Error

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
min_t = pow(10,10)
for k1 in range(len(A_list) - 3):
    for k2 in range(k1, len(A_list) -2):
        for k3 in rannge(k2, len(A_list) -1):
            for k4 in range(K3, len(A_list)):
                if k1 != k2 and k2 != k3 and k3 != k4:
                    P = sum(A_list[:k1])
                    Q = sum(A_list[k1:k2])
                    R = sum(A_list[k2:k3])
                    S = sum(A_list[k3:k4])
                    t = max(P,Q,R,S) - min(P,Q,R,S)
                    min_t = min(t, min_t)
print(min_t)

Submission status: Runtime Error

Solution code:
def S(a, b):
    return cl[b] - (0 if a == 0 else cl[a-1])

def partial(start, end, last):
    odiff = None
    for c in range(start, end+1):
        p = S(start,c)
        q = S(c+1,end)
        diff = abs(p-q)
        if odiff is None:
            odiff = diff
        if odiff < diff:
            return c, mp, mq
        odiff = diff
        mp = p
        mq = q

if __name__ == '__main__':
    # read input
    n = int(input())
    l = list(map(int, input().split()))
    # create cumlative list first
    cl = list(accumulate(l))
    clen = len(l)

    #  0  F(i)      i i+1 G(i)      len-1
    #  |------|------|-------|-------|
    #         c0    c1       c2
    #  <-----L------> <------R------->
    # pick up a c1 (i:i+1) and evaluate
    result = None
    c1 = 1
    c3 = 3
    for i in range(1,clen-2):
        # LEFT
        (c1, mb, mc) = partial(0, i, c1)
        # RIGHT
        c3, md, me = partial(i+1, clen-1, c3)

        # evaluate
        diff = abs(max(mb, mc, md, me) - min(mb, mc, md, me))
        if result is None:
            result = diff
        else:
            if result > diff:
                result = diff

    print(result)

Submission status: Runtime Error

Solution code:
from itertools import accumulate

def S(a, b):
    return cl[b] - (0 if a == 0 else cl[a-1])

if __name__ == '__main__':
    # read input
    n = int(input())
    l = list(map(int, input().split()))
    # create cumlative list first
    cl = list(accumulate(l))
    clen = len(l)

    #  0  F(i)      i i+1 G(i)      len-1
    #  |------|------|-------|-------|
    #         c0    c1       c2
    #  <-----L------> <------R------->
    # pick up a c1 (i:i+1) and evaluate
    r = list()
    result = None
    for i in range(1,clen-2):
        #print("i={}\tL{}, R{}".format(i, l[0:i+1], l[i+1:]))

        # find C0 for each i
        odiff = 10000000000
        for c in range(i):
            B = S(0,c)
            C = S(c+1,i)
            diff1 = abs(B-C)
            if odiff < diff1:
                break
            odiff = diff1
            mb = B
            mc = C

        # find C2 for each i
        odiff = 10000000000
        for c in range(i+1,clen-1):
            D = S(i+1, c)
            E = S(c+1, clen-1)
            diff2 = abs(E-D)
            if odiff < diff2:
                break
            odiff = diff2
            md = D
            me = E

        # evaluate
        diff = abs(max(mb, mc, md, me) - min(mb, mc, md, me))
        if result is None:
            result = diff
        else:
            if result > diff:
                result = diff

    print(result)

Submission status: Runtime Error

Solution code:
from itertools import accumulate

def S(a, b):
    return cl[b] - (0 if a == 0 else cl[a-1])

if __name__ == '__main__':
    # read input
    n = int(input())
    l = list(map(int, input().split()))
    # create cumlative list first
    cl = list(accumulate(l))
    clen = len(l)

    #  0  F(i)      i i+1 G(i)      len-1
    #  |------|------|-------|-------|
    #         c0    c1       c2
    #  <-----L------> <------R------->
    # pick up a c1 (i:i+1) and evaluate
    r = list()
    result = None
    for i in range(1,clen-2):
        #print("i={}\tL{}, R{}".format(i, l[0:i+1], l[i+1:]))

        # find C0 for each i
        odiff = 1000000000
        for c in range(i):
            B = S(0,c)
            C = S(c+1,i)
            diff1 = abs(B-C)
            if odiff > diff1:
                mb = B
                mc = C
            else:
                break
            odiff = diff1

        # find C2 for each i
        odiff = 1000000000
        for c in range(i+1,clen-1):
            D = S(i+1, c)
            E = S(c+1, clen-1)
            diff2 = abs(E-D)
            if odiff > diff2:
                md = D
                me = E
            else:
                break
            odiff = diff2

        # evaluate
        diff = abs(max(mb, mc, md, me) - min(mb, mc, md, me))
        if result is None:
            result = diff
        else:
            if result > diff:
                result = diff

    print(result)

Submission status: Runtime Error

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

ans=sum(A)
for a in range(3,N):
    for b in range(2,a):
        for c in range(1,b):
            sum1=sum(A[:c])
            sum2=sum(A[c:b])
            sum3=sum(A[b:a])
            sum4=sum(A[a:])
            dif=max(sum1,sum2,sum3,sum4)-min(sum1,sum2,sum3,sum4)
            if dif<ans:
                ans=dif
                if sum2<=sum1:
                    break
    if min(sum1,sum2,sum3)==sum3:
        break
  if min(sum1,sum2,sum3,sum4)==sum4:
      break

print(ans)

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []
ureshimi = []
k = 0
l = 0

sumA = sum(A)
sumBC = sum(A[0:2])
sumDE = sum(A) - sumBC

s = 0
mannnaka = 0

for i in range(N-2):
    s += A[i]
    if i == N - 3:
        mannnaka = N - 3
    elif s + A[i + 1] > sumA / 2:
        mannnaka = i
        break



saisho = max(0, mannnaka - 7)
saigo = min(N - 3, mannnaka + 5)

sumBC = sum(A[0:saisho + 2])
sumDE = sum(A) - sumBC


for i in range(saisho, saigo):
    BC = A[0 : i + 2]
    s = sum(A[0 : k])
    for j in range(k, i + 2):
        s += BC[j]
        if j == i + 1:
            k = j 
            B = BC[0 : j ]
            C = BC[j  : N - i]
            break
        elif s + BC[j + 1] > sumBC / 2:
            k = j
            if BC[j + 1] / 2 >= sumBC / 2 - s:
                B = BC[0 : j + 1]
                C = BC[j + 1 : i + 2]
            else:
                B = BC[0 : j + 2]
                C = BC[j + 2 : i + 2]
            break
    sumBC += A[i + 2]
    DE = A[i + 2 : N + 1]
    s = sum(DE[0:l - i + 1 ])
    for j in range(l - i + 1 , N - i - 2):
        if j == N - i - 2:
            l = j + i 
            D = DE[0 : j ]
            E = DE[j  : N - i]
            break
        elif s + DE[j ] > sumDE / 2:
            l = j + i
            if DE[j ] / 2 >= sumDE / 2 - s:
                D = DE[0 : j ]
                E = DE[j : N - i]
            else:
                D = DE[0 : j + 1]
                E = DE[j + 1 : N - i]
            break
        s += DE[j]
    sumDE -= A[i + 2]

    ureshimi.append(max(sum(B), sum(C), sum(D), sum(E)) - min(sum(B), sum(C), sum(D), sum(E)))



print(min(ureshimi))

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []
ureshimi = []
k = 0
l = 0

sumA = sum(A)
sumBC = sum(A[0:2])
sumDE = sum(A) - sumBC

s = 0
mannnaka = 0

for i in range(N-2):
    s += A[i]
    if i == N - 3:
        mannnaka = N - 3
    elif s + A[i + 1] > sumA / 2:
        mannnaka = i
        break



saisho = max(0, mannnaka - 7)
saigo = min(N - 4, mannnaka + 5)

sumBC = sum(A[0:saisho + 2])
sumDE = sum(A) - sumBC


for i in range(saisho, saigo):
    BC = A[0 : i + 2]
    s = sum(A[0 : k])
    for j in range(k, i + 2):
        s += BC[j]
        if j == i + 1:
            k = j 
            B = BC[0 : j ]
            C = BC[j  : N - i]
            break
        elif s + BC[j + 1] > sumBC / 2:
            k = j
            if BC[j + 1] / 2 >= sumBC / 2 - s:
                B = BC[0 : j + 1]
                C = BC[j + 1 : i + 2]
            else:
                B = BC[0 : j + 2]
                C = BC[j + 2 : i + 2]
            break
    sumBC += A[i + 2]
    DE = A[i + 2 : N + 1]
    s = sum(DE[0:l - i + 1 ])
    for j in range(l - i + 1 , N - i - 2):
        if j == N - i - 3:
            l = j + i 
            D = DE[0 : j ]
            E = DE[j  : N - i]
            break
        elif s + DE[j ] > sumDE / 2:
            l = j + i
            if DE[j ] / 2 >= sumDE / 2 - s:
                D = DE[0 : j ]
                E = DE[j : N - i]
            else:
                D = DE[0 : j + 1]
                E = DE[j + 1 : N - i]
            break
        s += DE[j]
    sumDE -= A[i + 2]

    ureshimi.append(max(sum(B), sum(C), sum(D), sum(E)) - min(sum(B), sum(C), sum(D), sum(E)))



print(min(ureshimi))

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []
ureshimi = []
k = 0
l = 0

sumA = sum(A)
sumBC = sum(A[0:2])
sumDE = sum(A) - sumBC

s = 0
mannnaka = 0

for i in range(N-2):
    s += A[i]
    if i == N - 3:
        mannnaka = N - 3
    elif s + A[i + 1] > sumA / 2:
        mannnaka = i
        break



saisho = max(0, mannnaka - 7)
saigo = min(N - 4, mannnaka + 5)

sumBC = sum(A[0:saisho + 2])
sumDE = sum(A) - sumBC

print(saisho, saigo)

for i in range(saisho, saigo):
    BC = A[0 : i + 2]
    s = sum(A[0 : k])
    for j in range(k, i + 2):
        s += BC[j]
        if j == i + 1:
            k = j 
            B = BC[0 : j ]
            C = BC[j  : N - i]
            break
        elif s + BC[j + 1] > sumBC / 2:
            k = j
            if BC[j + 1] / 2 >= sumBC / 2 - s:
                B = BC[0 : j + 1]
                C = BC[j + 1 : i + 2]
            else:
                B = BC[0 : j + 2]
                C = BC[j + 2 : i + 2]
            break
    print(i, k)
    sumBC += A[i + 2]
    DE = A[i + 2 : N + 1]
    s = sum(DE[0:l - i + 1 ])
    for j in range(l - i + 1 , N - i - 2):
        if j == N - i - 3:
            l = j + i 
            D = DE[0 : j ]
            E = DE[j  : N - i]
            break
        elif s + DE[j ] > sumDE / 2:
            l = j + i
            if DE[j ] / 2 >= sumDE / 2 - s:
                D = DE[0 : j ]
                E = DE[j : N - i]
            else:
                D = DE[0 : j + 1]
                E = DE[j + 1 : N - i]
            break
        s += DE[j]
    sumDE -= A[i + 2]

    ureshimi.append(max(sum(B), sum(C), sum(D), sum(E)) - min(sum(B), sum(C), sum(D), sum(E)))



print(min(ureshimi))

Submission status: Runtime Error

Solution code:
from itertools import accumulate, islice
from bisect import bisect_left

n = int(input())
xs = [int(x) for x in input().split()]

ys = list(accumulate(xs))
zs, total = ys[:-1], ys[-1]
v = []
for i, z in islice(enumerate(zs), 1, n-1):
    j = bisect_left(zs, z//2)
    splits = ((zs[j+k], z - zs[j+k]) for k in (0, 1) if 0 <= j+k <= i)
    a, b = min(splits, key=lambda s:abs(s[0]-s[1]))

    j = bisect_left(zs, (total - z)//2 + z)
    splits = ((zs[j+k] - z, total - zs[j+k]) for k in (0, 1) if i <= j+k <= n-2)
    c, d = min(splits, key=lambda s:abs(s[0]-s[1]))

    minn, _, _, maxx = sorted((a,b,c,d))
    v.append(maxx - minn)

print(min(v) if v else max(xs) - min(xs))

Submission status: Runtime Error

Solution code:
from itertools import accumulate
from bisect import bisect_left, bisect_right

n = int(input())
xs = [int(x) for x in input().split()]

ys = [0] + list(accumulate(xs))

rs = range(2, n-2)
v = []
for i in rs:
    j = bisect_left(ys, ys[i]//2)
    minmax1 = sorted([ys[j], ys[i] - ys[j]])
    minmax2 = sorted([ys[j-1], ys[i] - ys[j-1]])
    minmax3 = sorted([ys[j+1], ys[i] - ys[j+1]])
    _, max1, min1 = min((x-n, x, n) for n, x in (minmax1, minmax2, minmax3))

    k = bisect_left(ys, (ys[-1] - ys[i])//2 + ys[i])
    minmax1 = sorted([ys[k] - ys[i], ys[-1] - ys[k]])
    minmax2 = sorted([ys[k-1] - ys[i], ys[-1] - ys[k-1]])
    minmax3 = sorted([ys[k+1] - ys[i], ys[-1] - ys[k+1]])
    _, max2, min2 = min((x-n, x, n) for n, x in (minmax1, minmax2, minmax3))

    minn, _, _, maxx = sorted([max1, min1, max2, min2])
    v.append(maxx - minn)

print(min(v) if v else max(xs) - min(xs))

Submission status: Runtime Error

Solution code:
from itertools import accumulate
from bisect import bisect_left, bisect_right

n = int(input())
xs = [int(x) for x in input().split()]

ys = [0] + list(accumulate(xs))

rs = range(2, n-2)
v = []
for i in rs:
    j = bisect_left(ys, ys[i]//2)
    min1, max1 = sorted([ys[j], ys[i] - ys[j]])
    min2, max2 = sorted([ys[j-1], ys[i] - ys[j-1]])
    max_, min_ = (max1, min1) if max1 - min1 < max2 - min2 else (max2, min2)

    k = bisect_left(ys, (ys[-1] - ys[i])//2 + ys[i])
    min1, max1 = sorted([ys[k] - ys[i], ys[-1] - ys[k]])
    min2, max2 = sorted([ys[k-1] - ys[i], ys[-1] - ys[k-1]])
    max__, min__ = (max1, min1) if max1 - min1 < max2 - min2 else (max2, min2)

    minn, _, _, maxx = sorted([max_, min_, max__, min__])
    v.append(maxx - minn)

print(min(v))

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    ai = [int(i) for i in input().split()]

    #累積和
    vs = [0] * n
    vs[0] = ai[0]
    for i in range(1, n):
        vs[i] = vs[i-1] + ai[i]

    ans = []
    for i in range(1,n-3):
        p = vs[i]
        for j in range(i+1, n-2):
            q = vs[j] - vs[i]
            for k in range(j+1, n-1):
                r = vs[k] - vs[j]
                s = vs[n-1] - vs[k]
                ans.append(abs(max(p,q,r,s) - min(p,q,r,s)))
                
    print(min(ans))

    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

csum = [0]

for a in A:
    csum.append(csum[-1] + a)

def csum_(a, b):
    return csum[b] - csum[a]

def search(ok, ng, n, m):
    if abs(ok-ng) <= 1:
            return ok
    mid = (ok+ng) // 2
    if csum_(m, mid) < csum_(mid, n):
        return search(mid, ng, n, m)
    else:
        return search(ok, mid, n, m)

fans = 10**9

for c in range(2, n-1):
    d = search(1, c, c, 0)
    f = search(c+1, n, n, c)
    
    ans = []

    if abs(csum_(0, d) - csum_(d, c)) > abs(csum_(0, min(d+1, c-1)) - csum_(min(d+1, c-1), c)):
        d += 1

    if abs(csum_(c, f) - csum_(f, n)) > abs(csum_(c, min(n-1, f+1)) - csum_(min(n-1, f+1), n)):
        f += 1
 
    ans.append(csum_(0, d))
    ans.append(csum_(d, c))
    ans.append(csum_(c, f))
    ans.append(csum_(f, n))

    fans.append(max(ans) - min(ans))

print(min(fans))

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

csum = [0]

for a in A:
    csum.append(csum[-1] + a)

def csum_(a, b):
    return csum[b] - csum[a]

def search(ok, ng, n, m):
    if abs(ok-ng) <= 1:
            return ok
    mid = (ok+ng) // 2
    if csum_(m, mid) < csum_(mid, n):
        return search(mid, ng, n, m)
    else:
        return search(ok, mid, n, m)

fans = 10**9

for c in range(2, n-1):
    d = search(1, c, c, 0)
    f = search(c+1, n, n, c)
    
    ans = []

    if abs(csum_(0, d) - csum_(d, c)) > abs(csum_(0, min(d+1, c-1)) - csum_(min(d+1, c-1), c)):
        d += 1

    if abs(csum_(c, f) - csum_(f, n)) > abs(csum_(c, min(n-1, f+1)) - csum_(min(n-1, f+1), n)):
        f += 1
 
    ans.append(csum_(0, d))
    ans.append(csum_(d, c))
    ans.append(csum_(c, f))
    ans.append(csum_(f, n))

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

print(fans)

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []
ureshimi = []
k = 0
l = 0

for i in range(N-3):
    BC = A[0 : i + 2]
    s = 0
    for j in range(k, i + 2):
        s += BC[j]
        if s + BC[j + 1] > sum(BC) / 2:
            k = j
            if BC[j + 1] / 2 >= sum(BC) / 2 - s:
                B = BC[0 : j + 1]
                C = BC[j + 1 : i + 2]
            else:
                B = BC[0 : j + 2]
                C = BC[j + 2 : i + 2]
            break
    DE = A[i + 2 : N + 1]
    s = sum(DE[0:l - i + 1 ])
    for j in range(l - i + 1 , N - i - 2):
        if j == N - i - 3:
            l = j + i 
            D = DE[0 : j ]
            E = DE[j  : N - i]
            break
        elif s + DE[j ] > sum(DE) / 2:
            l = j + i
            if DE[j ] / 2 >= sum(DE) / 2 - s:
                D = DE[0 : j ]
                E = DE[j : N - i]
            else:
                D = DE[0 : j + 1]
                E = DE[j + 1 : N - i]
            break
        s += DE[j]
    ureshimi.append(max(sum(B), sum(C), sum(D), sum(E)) - min(sum(B), sum(C), sum(D), sum(E)))



print(min(ureshimi))

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []
ureshimi = []
k = 0
l = 0

for i in range(N-3):
    BC = A[0 : i + 2]
    s = 0
    for j in range(k, i + 2):
        s += BC[j]
        if s + BC[j + 1] > sum(BC) / 2:
            k = j
            if BC[j + 1] / 2 >= sum(BC) / 2 - s:
                B = BC[0 : j + 1]
                C = BC[j + 1 : i + 2]
            else:
                B = BC[0 : j + 2]
                C = BC[j + 2 : i + 2]
            break
    DE = A[i + 2 : N + 1]
    s = sum(DE[0:l - i + 1 ])
    for j in range(l - i + 1 , N - i - 2):
        if j == N - i - 3:
            l = j + i 
            D = DE[0 : j ]
            E = DE[j  : N - i]
            break
        elif s + DE[j ] > sum(DE) / 2:
            l = j + i
            if DE[j ] / 2 >= sum(DE) / 2 - s:
                D = DE[0 : j ]
                E = DE[j : N - i]
            else:
                D = DE[0 : j + 1]
                E = DE[j + 1 : N - i]
            break
        s += DE[j]
    print(BC, DE, B, C, D, E)
    ureshimi.append(max(sum(B), sum(C), sum(D), sum(E)) - min(sum(B), sum(C), sum(D), sum(E)))



print(min(ureshimi))

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []
ureshimi = []
k = 0
l = 0

for i in range(N-3):
    BC = A[0 : i + 2]
    s = 0
    for j in range(k, i + 2):
        s += BC[j]
        if s + BC[j + 1] > sum(BC) / 2:
            k = j
            if BC[j + 1] / 2 >= sum(BC) / 2 - s:
                B = BC[0 : j + 1]
                C = BC[j + 1 : i + 2]
            else:
                B = BC[0 : j + 2]
                C = BC[j + 2 : i + 2]
            break
    DE = A[i + 2 : N + 1]
    s = sum(DE[0:l - i + 1])
    for j in range(l - i + 1, N - i - 2):
        s += DE[j]
        if j == N - i - 3:
            l = j + i
            D = DE[0 : j + 1]
            E = DE[j + 1 : N - i]
            break
        elif s + DE[j + 1] > sum(DE) / 2:
            l = j + i
            if DE[j + 1] / 2 >= sum(DE) / 2 - s:
                D = DE[0 : j + 1]
                E = DE[j + 1 : N - i]
            else:
                D = DE[0 : j + 2]
                E = DE[j + 2 : N - i]
            break
    ureshimi.append(max(sum(B), sum(C), sum(D), sum(E)) - min(sum(B), sum(C), sum(D), sum(E)))



print(min(ureshimi))

Submission status: Runtime Error

Solution code:
import numpy as np
from itertools import combinations
N = int(input())
A = np.array(list(map(int, input().split())), dtype= int)
X = A.sum()
A1 = np.cumsum(A)
mincost = -1
for r1, r2, r3 in combinations(range(1,N), 3):
    P = A1[r1]
    Q = Y - A1[r1]
    R = A1[r3]-Y
    S = X-A1[r3]
    cost = max(P,Q,R,S) - min(P,Q,R,S)
    if mincost < 0 or mincost >  cost:
        mincost = cost
print(mincost)

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []
ureshimi = []
k = 0
l = 0

for i in range(N-3):
    BC = A[0 : i + 2]
    s = 0
    for j in range(k, i + 2):
        s += BC[j]
        if s + BC[j + 1] > sum(BC) / 2:
            k = j
            if BC[j + 1] / 2 >= sum(BC) / 2 - s:
                B = BC[0 : j + 1]
                C = BC[j + 1 : i + 2]
            else:
                B = BC[0 : j + 2]
                C = BC[j + 2 : i + 2]
            break
    DE = A[i + 2 : N + 1]
    s = sum(DE[0:l - i + 1])
    for j in range(l - i + 1, N - i - 3):
        s += DE[j]
        if j == N - i - 4:
            l = j + i
            D = DE[0 : j + 1]
            E = DE[j + 1 : N - i]
            break
        elif s + DE[j + 1] > sum(DE) / 2:
            l = j + i
            if DE[j + 1] / 2 >= sum(DE) / 2 - s:
                D = DE[0 : j + 1]
                E = DE[j + 1 : N - i]
            else:
                D = DE[0 : j + 2]
                E = DE[j + 2 : N - i]
            break
    ureshimi.append(max(sum(B), sum(C), sum(D), sum(E)) - min(sum(B), sum(C), sum(D), sum(E)))



print(min(ureshimi))

Submission status: Runtime Error

Solution code:
import statistics
import math

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

B = []
C = []
D = []
E = []

chikai = math.floor(sum(A) / 4)
s = 0
for i in range(len(A)):
    s += A[i]
    if s + A[i + 1] > chikai:
        if (A[i + 1] ) / 2 >= chikai - s:
            B = [A[j] for j in range(i + 1)]
            del A[0:i+1]
        else:
            B = [A[j] for j in range(i + 2)]
            del A[0:i+2]
        break

chikai = math.floor(sum(A) / 3)
s = 0
for i in range(len(A)):
    s += A[i]
    if s + A[i + 1] > chikai:
        if (A[i + 1] ) / 2 >= chikai - s:
            C = [A[j] for j in range(i + 1)]
            del A[0:i+1]
        else:
            C = [A[j] for j in range(i + 2)]
            del A[0:i+2]
        break

chikai = math.floor(sum(A) / 2)
s = 0
for i in range(len(A)):
    s += A[i]
    if s + A[i + 1] > chikai:
        if (A[i + 1] ) / 2 >= chikai - s:
            D = [A[j] for j in range(i + 1)]
            del A[0:i+1]
        else:
            D = [A[j] for j in range(i + 2)]
            del A[0:i+2]
        break

E = A[:]


print((max(sum(B),sum(C),sum(D),sum(E))-min(sum(B),sum(C),sum(D),sum(E))))

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

csum = [0]

for a in A:
    csum.append(csum[-1] + a)

def csum_(a, b):
    return csum[b] - csum[a]

def search(ok, ng, n, m):
    if abs(ok-ng) <= 1:
            return ok
    mid = (ok+ng) // 2
    if csum_(m, mid) < csum_(mid, n):
        return search(mid, ng, n, m)
    else:
        return search(ok, mid, n, m)

c = search(1, n, n, 0)  #中央のindex
d = search(1, c, c, 0)
f = search(c, n, n, c)

fans = []


for d in [max(d-1, 1), d, d+1]:
    for c in [max(d, c-1), c, c+1]:
        for f in [max(c, f-1), f, min(f+1, n-1)]:
            ans = []

            ans.append(csum_(0, d))
            ans.append(csum_(d, c))
            ans.append(csum_(c, f))
            ans.append(csum_(f, n))

            fans.append(max(ans) - min(ans))

print(min(fans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
res = 20000000002
for k in range(n-2):
    for l in range(k,n-1):
        for m in range(l,n):
            f = sum(a[:k],sum[k:l],sum[l:m],sum[l:])
            kari = max(f)-min(f)
            if kari < res: res = kari
print(res)

Submission status: Runtime Error

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

a0=A[1:N-1]
a1n=A[0]
a2n=A[-1]
a1=A[0]
a2=A[-1]
while True:
    if a0==[]:break
    if a1n<a2n:
        a1n+=a0[0]
        a1.append(a0[0])
        a0.pop(0)
    else:
        a2n+=a0[-1]
        a2.insert(0,a0[0])
        a0.pop(-1)
        
a3n=a1[0]
a4n=a1[-1]
while True:
    if a1==[]:break
    elif a3n<a4n:
        a3n+=a1[0]
        a1.pop(0)
    else:
        a4n+=a1[-1]
        a1.pop(-1)
        
a5n=a2[0]
a6n=a2[-1]
while True:
    if a2==[]:break
    elif a5n<a6n:
        a5n+=a2[0]
        a2.pop(0)
    else:
        a6n+=a2[-1]
        a2.pop(-1)
        
num=[sum(a3),sum(a4),sum(a5),sum(a6)]
print(max(num)-min(num))

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

csum = [0]

for a in A:
    csum.append(csum[-1] + a)

def csum_(a, b):
    return csum[b] - csum[a]

def search(ok, ng, n, m):
    if abs(ok-ng) <= 1:
            return ok
    mid = (ok+ng) // 2
    if csum_(m, mid) < csum_(mid, n):
        return search(mid, ng, n, m)
    else:
        return search(ok, mid, n, m)

c = search(1, n, n, 0)  #中央のindex
d = search(1, c, c, 0)
f = search(c, n, n, c)

fans = []


for d in [max(d-1, 0), d, d+1]:
    for c in [max(d, c-1), c, c+1]:
        for f in [max(c, f-1), f, min(f+1, n)]:
            ans = []

            ans.append(csum_(0, d))
            ans.append(csum_(d, c))
            ans.append(csum_(c, f))
            ans.append(csum_(f, n))

            fans.append(max(ans) - min(ans))

print(min(fans))

Submission status: Runtime Error

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

answers = []
avg = sum(A)/4
temp = 0
d1 = []


def f(d, i, avg):
    global A
    temp = 0
    while True:
        temp += A[i]
        i += 1
        if temp > avg:
            d.append(i)
            return i


def g(d, i, avg):
    global A
    temp = 0
    while True:
        temp += A[i]
        i += 1
        if temp > avg:
            d.append(i-1)
            return i-1


def ans(f1, f2, f3):
    global A
    i = 0
    d = []
    i = f1(d, i, sum(A)/4)
    i = f2(d, i, sum(A[i:])/3)
    i = f3(d, i, sum(A[i:])/2)
    l = [sum(A[:d[0]]), sum(A[d[0]:d[1]]), sum(
        A[d[1]:d[2]]), sum(A[min(d[2], len(A)-1):])]
    return max(l)-min(l)


print(min([ans(f, f, f),
           ans(f, f, g),
           ans(f, g, f),
           ans(f, g, g),
           ans(g, f, f),
           ans(g, f, g),
           ans(g, g, f),
           ans(g, g, g)]))

Submission status: Runtime Error

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

a0=A[1:N-1]
a1n=A[0]
a2n=A[-1]
a1=A[0]
a2=A[-1]
while True:
    if a0=[]:break
    if a1n<a2n:
        a1n+=a0[0]
        a1.append(a0[0])
        a0.pop(0)
    else:
        a2n+=a0[-1]
        a2.insert(0,a0[0])
        a0.pop(-1)
        
a3n=a1[0]
a4n=a1[-1]
while True:
    if a1=[]:break
    elif a3n<a4n:
        a3n+=a1[0]
        a1.pop(0)
    else:
        a4n+=a1[-1]
        a1.pop(-1)
        
a5n=a2[0]
a6n=a2[-1]
while True:
    if a2=[]:break
    elif a5n<a6n:
        a5n+=a2[0]
        a2.pop(0)
    else:
        a6n+=a2[-1]
        a2.pop(-1)
        
num=[sum(a3),sum(a4),sum(a5),sum(a6)]
print(max(num)-min(num))

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
def ec(X):
  N = len(X)
  p = []
  s = 0
  for i in range(N):
    s += X[i]
    p.append(s)
  md = p[-1]
  for i in range(N-1):
    d = abs(p[i]-(p[-1]-p[i]))
    if d < md:
      md = d
      mi = i
  return mi+1

N = int(raw_input())
A = map(int, raw_input().split())
ri = ec(A)
qi = ec(A[:ri])
si = ec(A[ri:])+ri
I = [0,qi,ri,si,N]
Y = [A[:qi], A[qi:ri], A[ri:si], A[si:]]
S = map(sum, Y)
ams = abs(max(S) - min(S))

for i in range(1,4):
  for dl in [-1,1]:
    while I[i-1]<I[i]+dl and I[i]+dl<I[i+1]:
      ns = []
      for j in range(4):
        nsj = sum(A[I[j]+(dl if j==i else 0):I[j+1]+(dl if j+1==i else 0)])
        ns.append(nsj)
      nams = abs(max(ns)-min(ns))
      if nams < ams:
        I[i] += dl
        ams = nams
      else:
        break
print ams

Submission status: Runtime Error

Solution code:
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:])
			print("{} {} {} {}".format(B, C , D, E))
			P = sum(B)
			Q = sum(C)
			R = sum(D)
			S = sum(E)
			ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
print(ans)

Submission status: Runtime Error

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

answers = []
avg = sum(A)/4
temp = 0
d1 = []


def f(d, i, avg):
    global A
    temp = 0
    while True:
        temp += A[i]
        i += 1
        if temp > avg:
            d.append(i)
            return i


def g(d, i, avg):
    global A
    temp = 0
    while True:
        temp += A[i]
        i += 1
        if temp > avg:
            d.append(i-1)
            return i-1


def ans(f1, f2, f3):
    global A
    i = 0
    d = []
    i = f1(d, i, sum(A)/4)
    i = f2(d, i, sum(A[i:])/3)
    i = f3(d, i, sum(A[i:])/2)
    l = [sum(A[:d[0]]), sum(A[d[0]:d[1]]), sum(A[d[1]:d[2]]), sum(A[d[2]:])]
    return max(l)-min(l)


print(min([ans(f, f, f),
           ans(f, f, g),
           ans(f, g, f),
           ans(f, g, g),
           ans(g, f, f),
           ans(g, f, g),
           ans(g, g, f),
           ans(g, g, g)]))