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 numpy as np
from itertools import product
N = int(input())
A = np.array(list(map(int, input().split())), dtype= int)
X = A.sum()
A1 = np.cumsum(A)

mincost = -1
r2 = 2
for i in range(2,N-1):
    L = A1[i]
    R = X - L
    cost = np.abs(R-L)
    if mincost < 0 or mincost >  cost:
        mincost = cost
        r2 = i

Y = A1[r2]
mincost = -1
r1 = 1
for i in range(1,r2):
    L = A1[i]
    R = Y - L
    cost = np.abs(R-L)
    if mincost < 0 or mincost >  cost:
        mincost = cost
        r1 = i
P = A1[r1]
Q = Y - A1[r1]

mincost = -1
r3 = N-1
for i in range(r2+1,N):
    L = A1[i] - Y
    R = X - Y - L
    cost = np.abs(R-L)
    if mincost < 0 or mincost >  cost:
        mincost = cost
        r3 = i

R = A1[r3]-Y
S = X-A1[r3]
cost = max(P,Q,R,S) - min(P,Q,R,S)
print(cost)

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)

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)
mincost1 = -1
for r2 in range(2,N-1):
    Y = A1[r2]
    mincost2 = -1
    r1 = 1
    for i in range(1,r2):
        L = A1[i]
        R = Y - L
        cost = np.abs(R-L)
        if mincost2 < 0 or mincost2 >  cost:
            mincost2 = cost
            r1 = i
    P = A1[r1]
    Q = Y - A1[r1]

    mincost3 = -1
    r3 = N-1
    for i in range(r2+1,N):
        L = A1[i] - Y
        R = X - Y - L
        cost = np.abs(R-L)
        if mincost3 < 0 or mincost3 >  cost:
            mincost3 = cost
            r3 = i

    R = A1[r3]-Y
    S = X-A1[r3]
    cost = max(P,Q,R,S) - min(P,Q,R,S)
    if mincost1 < 0 or mincost1 >  cost:
            mincost1 = cost
print(mincost1)

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)
mincost1 = -1
for r2 in range(1,N-2):
    Y = A1[r2]
    mincost2 = -1
    r1 = 1
    for i in range(0,r2):
        L = A1[i]
        R = Y - L
        cost = np.abs(R-L)
        if mincost2 < 0 or mincost2 >  cost:
            mincost2 = cost
            r1 = i
    P = A1[r1]
    Q = Y - A1[r1]

    mincost3 = -1
    r3 = N-1
    for i in range(r2+1,N):
        L = A1[i] - Y
        R = X - Y - L
        cost = np.abs(R-L)
        if mincost3 < 0 or mincost3 >  cost:
            mincost3 = cost
            r3 = i

    R = A1[r3]-Y
    S = X-A1[r3]
    cost = max(P,Q,R,S) - min(P,Q,R,S)
    print(r1,r2,r3)
    print(P,Q,R,S,cost)
    if mincost1 < 0 or mincost1 >  cost:
            mincost1 = cost
print(mincost1)

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 = A1[r2] - A1[r1]
    R = A1[r3]-A1[r2]
    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)

from itertools import accumulate
input()
*s, = accumulate(map(int, input().split()))
l,r,t = 0,2,s[-1]
c = t
for m in s[1:-2]:
    while t+m > s[r]+s[r+1]:
        r += 1
    while m > s[l]+s[l+1]:
        l += 1
    x = sorted([s[l],m-s[l],s[r]-m,t-s[r]])
    c = min(c,x[-1]-x[0])
print(c)

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted