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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.stream.LongStream;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(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, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            long[] a = in.nextLongArray(n);

            RangeSum rangeSum = new RangeSum(a);

            long ans = Long.MAX_VALUE;
            int left = 0;
            int right = 2;
            for (int i = 1; i < n - 2; i++) {
                while (left + 1 < i && Math.abs(rangeSum.getSum(0, left) - rangeSum.getSum(left + 1, i))
                        > Math.abs(rangeSum.getSum(0, left + 1) - rangeSum.getSum(left + 2, i))) {
                    left++;
                }
                while (i == right || right < n - 2 && Math.abs(rangeSum.getSum(i + 1, right) - rangeSum.getSum(right + 1, n - 1))
                        > Math.abs(rangeSum.getSum(i + 1, right + 1) - rangeSum.getSum(right + 2, n - 1))) {
                    right++;
                }
                long[] arr = {
                        rangeSum.getSum(0, left),
                        rangeSum.getSum(left + 1, i),
                        rangeSum.getSum(i + 1, right),
                        rangeSum.getSum(right + 1, n - 1)
                };
                Arrays.sort(arr);
                ans = Math.min(ans, arr[3] - arr[0]);
            }

            out.println(ans);
        }

    }

    static class RangeSum {
        private long[] sum;

        public RangeSum(long[] arr) {
            sum = new long[arr.length + 1];
            System.arraycopy(arr, 0, sum, 1, arr.length);
            for (int i = 0; i < sum.length - 1; i++) {
                sum[i + 1] += sum[i];
            }
        }

        public RangeSum(int[] arr) {
            this(IntStream.of(arr).mapToLong(x -> x).toArray());
        }

        public long getSum(int left, int right) {
            if (left > right || left < 0
                    || right >= sum.length - 1) {
                throw new IllegalArgumentException();
            }

            return sum[right + 1] - sum[left];
        }

    }

    static class InputReader {
        private BufferedReader br;
        private StringTokenizer st;

        public InputReader(InputStream inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream));
            st = new StringTokenizer("");
        }

        public String nextString() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine(), " ");
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(nextString());
        }

        public long nextLong() {
            return Long.parseLong(nextString());
        }

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

    }
}