Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(a) for a in input().split()]

def found_cut(X):
    p1 = sum(X)/2
    count1 = 0
    for i in range(len(X)):
        count1 += X[i]
        if p1 <= count1:
            if abs(count1-p1) < abs(count1-X[i]-p1):
                index = i
                break
            else:
                index = i-1
                break
    return index

def found_ans(X,Y,Z,W):
    ans_list = []
    ans_list.append(sum(X))
    ans_list.append(sum(Y))
    ans_list.append(sum(Z))
    ans_list.append(sum(W))
    return max(ans_list)-min(ans_list)

ANS = []
for x in range(1,N-2):
    A_first = A[:x+1]
    A_second = A[x+1:]
    if len(A_first) >= 2 and len(A_second) >= 2:
        wp1 = found_cut(A_first)
        wp2 = found_cut(A_second)
        pattern3_1 = A_first[:wp1+1]
        pattern3_2 = A_first[wp1+1:]
        pattern3_3 = A_second[:wp2+1]
        pattern3_4 = A_second[wp2+1:]
        ANS.append(found_ans(pattern3_1,pattern3_2,pattern3_3,pattern3_4))
print(min(ANS))

Fixed submission:
N = int(input())
A = [int(a) for a in input().split()]

S = [0]*N
S[0] = A[0]
for i in range(1, N):
    S[i] = A[i]+S[i-1]
    
ans = 2*10**14
l = 0
temp = 2*10**14
r = 2
#for i in range(2, N):
#    if abs(S[-1]-2*S[i]+S[1]) < temp:
#        temp = abs(S[-1]-2*S[r]+S[1])
#        r = i
        
for i in range(2, N-1):
    r = max(r, i)
    while l+1 < i-1 and abs(S[i-1]-2*S[l]) > abs(S[i-1]-2*S[l+1]):
        l += 1
    while r+1 < N-1 and abs(S[N-1]-2*S[r]+S[i-1])  > abs(S[N-1]-2*S[r+1]+S[i-1]):
        r += 1
    t = max(S[N-1]-S[r], S[r]-S[i-1], S[i-1]-S[l], S[l]) - min(S[N-1]-S[r], S[r]-S[i-1], S[i-1]-S[l], S[l])
    ans = min(ans, t)
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(a) for a in input().split()]

def found_cut(X):
    p1 = sum(X)/2
    count1 = 0
    for i in range(len(X)):
        count1 += X[i]
        if p1 <= count1:
            if abs(count1-p1) < abs(count1-X[i]-p1):
                index = i
                break
            else:
                index = i-1
                break
    return index

def found_ans(X,Y,Z,W):
    ans_list = []
    ans_list.append(sum(X))
    ans_list.append(sum(Y))
    ans_list.append(sum(Z))
    ans_list.append(sum(W))
    return max(ans_list)-min(ans_list)

ANS = []
for x in range(N-1):
    A_first = A[:x+1]
    A_second = A[x+1:]

    if len(A_first) >= 3:
        yp1 = found_cut(A_first)
        pattern1_1 = A_first[:yp1+1]
        pattern1_2 = A_first[yp1+1:]
        if len(pattern1_2) != 1:
            yp2 = found_cut(pattern1_2)
            pattern1_2_1 = pattern1_2[:yp2+1]
            pattern1_2_2 = pattern1_2[yp2+1:]
            ANS.append(found_ans(pattern1_1,pattern1_2_1,pattern1_2_2,A_second))
        if len(pattern1_1) != 1:
            yp2 = found_cut(pattern1_1)
            pattern1_1_1 = pattern1_1[:yp2+1]
            pattern1_1_2 = pattern1_1[yp2+1:]
            ANS.append(found_ans(pattern1_1_1,pattern1_1_2,pattern1_2,A_second))
    if len(A_second) >= 3:
        zp1 = found_cut(A_second)
        pattern2_1 = A_second[:zp1+1]
        pattern2_2 = A_second[zp1+1:]
        if len(pattern2_2) != 1:
            zp2 = found_cut(pattern2_2)
            pattern2_2_1 = pattern2_2[:zp2+1]
            pattern2_2_2 = pattern2_2[zp2+1:]
            ANS.append(found_ans(pattern2_1,pattern2_2_1,pattern2_2_2,A_first))
        if len(pattern2_1) != 1:
            zp2 = found_cut(pattern2_1)
            pattern2_1_1 = pattern2_1[:zp2+1]
            pattern2_1_2 = pattern2_1[zp2+1:]
            ANS.append(found_ans(pattern2_1_1,pattern2_1_2,pattern2_2,A_first))
    if len(A_first) >= 2 and len(A_second) >= 2:
        wp1 = found_cut(A_first)
        wp2 = found_cut(A_second)
        pattern3_1 = A_first[:wp1+1]
        pattern3_2 = A_first[wp1+1:]
        pattern3_3 = A_second[:wp2+1]
        pattern3_4 = A_second[wp2+1:]
        ANS.append(found_ans(pattern3_1,pattern3_2,pattern3_3,pattern3_4))
print(min(ANS))

Fixed submission:
N = int(input())
A = [int(a) for a in input().split()]

S = [0]*N
S[0] = A[0]
for i in range(1, N):
    S[i] = A[i]+S[i-1]
    
ans = 2*10**14
l = 0
temp = 2*10**14
r = 2
#for i in range(2, N):
#    if abs(S[-1]-2*S[i]+S[1]) < temp:
#        temp = abs(S[-1]-2*S[r]+S[1])
#        r = i
        
for i in range(2, N-1):
    r = max(r, i)
    while l+1 < i-1 and abs(S[i-1]-2*S[l]) > abs(S[i-1]-2*S[l+1]):
        l += 1
    while r+1 < N-1 and abs(S[N-1]-2*S[r]+S[i-1])  > abs(S[N-1]-2*S[r+1]+S[i-1]):
        r += 1
    t = max(S[N-1]-S[r], S[r]-S[i-1], S[i-1]-S[l], S[l]) - min(S[N-1]-S[r], S[r]-S[i-1], S[i-1]-S[l], S[l])
    ans = min(ans, t)
    
print(ans)