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: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Atharva Nagarkar
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        JoltyScanner in = new JoltyScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DEqualCut solver = new DEqualCut();
        solver.solve(1, in, out);
        out.close();
    }

    static class DEqualCut {
        public void solve(int testNumber, JoltyScanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n];
            long[] forwardCS = new long[n];
            long[] backwardCS = new long[n];
            for (int i = 0; i < n; ++i) {
                a[i] = in.nextInt();
            }
            for (int i = 0; i < n; ++i) {
                forwardCS[i] = a[i];
                if (i > 0) forwardCS[i] += forwardCS[i - 1];
            }
            for (int i = n - 1; i >= 0; --i) {
                backwardCS[i] = a[i];
                if (i < n - 1) backwardCS[i] += backwardCS[i + 1];
            }
            long best = Long.MAX_VALUE;
            for (int mid = 1; mid < n - 3; ++mid) {
                long left = forwardCS[mid];
                long right = backwardCS[mid + 1];
                long B, C, D, E;
                B = C = D = E = -1;
                {
                    int lo = 0;
                    int hi = mid - 1;
                    long minDiff = Long.MAX_VALUE;
                    for (int i = 0; i < 30; ++i) {
                        int md = lo + hi >> 1;
                        long b = forwardCS[md];
                        long c = left - b;
                        if (c > b) lo = md;
                        else hi = md;
                    }
                    {
                        long b = forwardCS[lo];
                        long c = left - b;
                        long diff = Math.abs(b - c);
                        if (diff < minDiff) {
                            minDiff = diff;
                            B = b;
                            C = c;
                        }
                    }
                    {
                        long b = forwardCS[hi];
                        long c = left - b;
                        long diff = Math.abs(b - c);
                        if (diff < minDiff) {
                            B = b;
                            C = c;
                        }
                    }
                }
                {
                    int lo = mid + 2;
                    int hi = n - 1;
                    long minDiff = Long.MAX_VALUE;
                    for (int i = 0; i < 30; ++i) {
                        int md = lo + hi >> 1;
                        long e = backwardCS[md];
                        long d = right - e;
                        if (e > d) lo = md;
                        else hi = md;
                    }
                    {
                        long e = backwardCS[lo];
                        long d = right - e;
                        long diff = Math.abs(e - d);
                        if (diff < minDiff) {
                            minDiff = diff;
                            E = e;
                            D = d;
                        }
                    }
                    {
                        long e = backwardCS[hi];
                        long d = right - e;
                        long diff = Math.abs(e - d);
                        if (diff < minDiff) {
                            E = e;
                            D = d;
                        }
                    }
                }
                if (B == -1 && C == -1 && D == -1 && E == -1) throw new RuntimeException("WTF");
                long min = math.min(B, C, D, E);
                long max = math.max(B, C, D, E);
                best = Math.min(best, max - min);
            }
            out.println(best);
        }

    }

    static class JoltyScanner {
        public int BS = 1 << 16;
        public char NC = (char) 0;
        public byte[] buf = new byte[BS];
        public int bId = 0;
        public int size = 0;
        public char c = NC;
        public double num = 1;
        public BufferedInputStream in;

        public JoltyScanner(InputStream is) {
            in = new BufferedInputStream(is, BS);
        }

        public JoltyScanner(String s) throws FileNotFoundException {
            in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
        }

        public char nextChar() {
            while (bId == size) {
                try {
                    size = in.read(buf);
                } catch (Exception e) {
                    return NC;
                }
                if (size == -1) return NC;
                bId = 0;
            }
            return (char) buf[bId++];
        }

        public int nextInt() {
            return (int) nextLong();
        }

        public long nextLong() {
            num = 1;
            boolean neg = false;
            if (c == NC) c = nextChar();
            for (; (c < '0' || c > '9'); c = nextChar()) {
                if (c == '-') neg = true;
            }
            long res = 0;
            for (; c >= '0' && c <= '9'; c = nextChar()) {
                res = (res << 3) + (res << 1) + c - '0';
                num *= 10;
            }
            return neg ? -res : res;
        }

    }

    static class math {
        public static long max(long... a) {
            long ans = a[0];
            for (long i : a) {
                ans = Math.max(ans, i);
            }
            return ans;
        }

        public static long min(long... a) {
            long ans = a[0];
            for (long i : a) {
                ans = Math.min(ans, i);
            }
            return ans;
        }

    }
}