Problem: 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.

Submission attempts (in order):
import statistics
import math

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


ureshimi = []
k = 0
l = 2

B = 0
C = 0
D = 0
E = 0

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

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

print(min(ureshimi))

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))

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))

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))

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))

import statistics
import math

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


ureshimi = []
k = 0
l = 2

B = 0
C = 0
D = 0
E = 0

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

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

print(min(ureshimi))

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted