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: Accepted

Solution code:
# https://atcoder.jp/contests/abc102/tasks/arc100_b

import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop

def main():
    n = int(input())
    al = list(map(int, input().split()))

    l, c, r = 1, 2, 3
    P = sum(al[:l])
    Q = sum(al[l:c])
    R = sum(al[c:r])
    S = sum(al[r:])

    ans = max(P, Q, R, S) - min(P, Q, R, S)

    while c <= n - 2:
        while True:
            if abs(P - Q) < abs((P + al[l]) - (Q - al[l])):  # これ以上差が縮まらないなら break
                break

            if l == c - 1:
                break

            P += al[l]
            Q -= al[l]
            l += 1

        while True:
            if abs(R - S) < abs((R + al[r]) - (S - al[r])): # これ以上差が縮まらないなら break
                break
            if r == n - 1:
                break

            R += al[r]
            S -= al[r]
            r += 1

        ans = min(max(P, Q, R, S) - min(P, Q, R, S), ans)

        # center を一つ右に進める
        Q += al[c]
        R -= al[c]
        c += 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_left

N = int(input())
A = [int(s) for s in input().split()]
S = [0] + list(accumulate(A))


def score(i, j, k):
    mx = max(S[i], S[j] - S[i], S[k] - S[j], S[N] - S[k])
    mn = min(S[i], S[j] - S[i], S[k] - S[j], S[N] - S[k])
    return mx - mn


ans = float("inf")
i = 1
k = 3
for j in range(2, N - 1):
    while i < j - 1 and abs(S[j] - 2 * S[i]) > abs(S[j] - 2 * S[i+1]):
        i += 1
    while k <= j or k < N - 1 and abs(S[N] - 2 * S[k] + S[j]) > abs(S[N] - 2 * S[k+1] + S[j]):
        k += 1
    mx = max(S[i], S[j] - S[i], S[k] - S[j], S[N] - S[k])
    mn = min(S[i], S[j] - S[i], S[k] - S[j], S[N] - S[k])
    ans = min(ans, mx - mn)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

#n,x = [int(i) for i in readline().split()]
#h,w = [int(i) for i in readline().split()]
n = int(input())
a = [int(i) for i in readline().split()]

from itertools import accumulate
acc = list(accumulate([0]+a))

j = 1
k = 3
ans = 10**18
for i in range(2,n):
    c = acc[i]//2
    while j < i-1 and acc[j] < c:
        j += 1

    c = (acc[-1]-acc[i])//2
    while k < n-1 and acc[k]-acc[i] < c:
        k += 1

    for jj in range(2):
        for kk in range(2):
            l = (acc[j-jj],acc[i]-acc[j-jj], acc[-1]-acc[k-kk], acc[k-kk]-acc[i])
            res = max(l) - min(l)
            ans = min(ans, res)

    
print(ans)