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.*;
import java.util.*;

public class Main {
    public static final long mod = (long)1e9+7;
    public static final long INF = Long.MAX_VALUE/2;
    public static final int inf = Integer.MAX_VALUE/2;

    static void solve(InputReader in, PrintWriter out){
        int n = in.ni();
        long[] a = in.nl(n);
        for (int i = 1; i < n; i++) a[i] += a[i-1];
        long min = INF;
        for (int i = 1; i < n - 2; i++) {
            int l = -1, r = i;
            long max_l, max_r, min_l,min_r;
            while(r-l>1){
                int mid = (l+r)/2;
                if(a[mid]>a[i]-a[mid]){
                    r = mid;
                }else if(a[i]-a[mid]>a[mid]){
                    l = mid;
                }else{
                    l = mid;
                    r = mid;
                    break;
                }
            }
            if(l==r){
                max_l = a[l];
                min_l = max_l;
            }else if(l==-1){
                max_l = a[0];
                min_l = a[i]-a[0];
            }else if(r==i){
                max_l = a[i]-a[i-1];
                min_l = a[i-1];
            }else{
                if(a[i]<a[l]+a[r]){
                    max_l = a[i]-a[l];
                    min_l = a[l];
                }else{
                    max_l = a[r];
                    min_l = a[i]-a[r];
                }
            }

            l = i; r = n-1;
            while(r-l>1){
                int mid = (l+r)/2;
                if(a[mid]-a[i]>a[n-1]-a[mid]){
                    r = mid;
                }else if(a[mid]-a[i]<a[n-1]-a[mid]){
                    l = mid;
                }else{
                    l = mid;
                    r = mid;
                    break;
                }
            }
            if(l==r){
                max_r = a[l]-a[i];
                min_r = max_l;
            }else if(l==i){
                max_r = a[i+1]-a[i];
                min_r = a[n-1]-a[i+1];
            }else if(r==n-1){
                max_r = a[n-1]-a[n-2];
                min_r = a[n-2]-a[i];
            }else{
                if(a[i]+a[n-1]<a[l]+a[r]){
                    max_r = a[n-1]-a[l];
                    min_r = a[l]-a[i];
                }else{
                    max_r = a[r]-a[i];
                    min_r = a[n-1]-a[r];
                }
            }
            min = Math.min(min, Math.abs(Math.max(max_r,max_l)-Math.min(min_l,min_r)));
        }
        out.println(min);
    }

    public static void main(String[] args){
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        solve(in, out);
        out.close();
    }
    public static class InputReader{
        private BufferedReader br;
        private StringTokenizer st;
        public InputReader(InputStream is){
            br = new BufferedReader(new InputStreamReader(is));
            st = null;
        }
        public String ns(){
            if(st == null || !st.hasMoreTokens()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (Exception e){
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }
        public int ni(){
            return Integer.parseInt(ns());
        }
        public long nl(){
            return Long.parseLong(ns());
        }
        public double nd(){
            return Double.parseDouble(ns());
        }
        public char nc(){
            return ns().toCharArray()[0];
        }
        public int[] ni(int n){
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }
        public long[] nl(int n){
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nl();
            }
            return a;
        }
        public double[] nd(int n){
            double[] a = new double[n];
            for (int i = 0; i < n; i++) {
                a[i] = nd();
            }
            return a;
        }
    }
}