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

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    static int N;
    static int[] A;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);

        N = sc.nextInt();
        A = sc.nextIntArray(N);

        System.out.println( solve() );
    }

    static long solve() {
        long[] SUM = new long[N];
        SUM[0] = A[0];
        for (int i = 1; i < N; i++) {
            SUM[i] = SUM[i-1] + A[i];
        }
        long TOTAL = SUM[N-1];

        long ans = Integer.MAX_VALUE;
        for (int y = 2; y <= N-2 ; y++) {
            long PQ = SUM[y-1];
            long RS = TOTAL - PQ;

            int low_x = 1;
            int high_x = y-1;
            while(low_x < high_x) {
                int mid = (low_x + high_x) / 2;

                long P = SUM[mid-1];
                long Q = PQ - P;
                if( P < Q ) {
                    low_x = mid + 1;
                } else {
                    high_x = mid;
                }
            }
            int x = low_x;
            if( low_x != 1 ) {
                int x1 = x-1;

                if( Math.abs(SUM[x-1] - (PQ - SUM[x-1])) > Math.abs(SUM[x1-1] - (PQ - SUM[x1-1])) ) {
                    x = x1;
                }
            }
            if( low_x != y-1 ) {
                int x1 = x+1;
                if( Math.abs(SUM[x-1] - (PQ - SUM[x-1])) > Math.abs(SUM[x1-1] - (PQ - SUM[x1-1])) ) {
                    x = x1;
                }
            }

            int low_z = y+1;
            int high_z = N-1;
            while(low_z < high_z) {
                int mid = (low_z + high_z) / 2;

                long R = SUM[mid-1] - PQ;
                long S = RS - R;
                if( R < S ) {
                    low_z = mid + 1;
                } else {
                    high_z = mid;
                }
            }
            int z = low_z;
            if( low_z != y+1 ) {
                int z1 = z-1;
                long R = SUM[z-1] - PQ;
                long R1 = SUM[z1 -1] - PQ;

                if( Math.abs(R - (RS- R)) > Math.abs(R1 - (RS - R1)) ) {
                    z = z1;
                }
            }
            if( low_z != N-1 ) {
                int z1 = z+1;
                long R = SUM[z-1] - PQ;
                long R1 = SUM[z1 -1] - PQ;

                if( Math.abs(R - (RS- R)) > Math.abs(R1 - (RS - R1)) ) {
                    z = z1;
                }
            }

            long P = SUM[x-1];
            long Q = PQ - P;
            long R = SUM[z-1] - PQ;
            long S = RS - R;

            ans = Math.min(max(P, Q, R, S) - min(P, Q, R, S), ans);
        }

        return ans;
    }

    static long max(long... values) {
        long max = values[0];
        for (int i = 1; i < values.length; i++) {
            max = Math.max(values[i], max);
        }
        return max;
    }

    static long min(long... values) {
        long min = values[0];
        for (int i = 1; i < values.length; i++) {
            min = Math.min(values[i], min);
        }
        return min;
    }

    static class FastScanner {

        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            return tokenizer.nextToken("\n");
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }
}