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.util.*;
import java.io.*;
import java.math.*;
 
 
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main{
    static final long mod=1000000007;
    
    public  static void main(String[] args)   throws Exception, IOException{
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);
        // int n=sc.nextInt();
        // char c[][] = new char[h][w];
        // char c[]=sc.nextString().toCharArray();
        // for(int i=0; i<n; i++) {a[i]=sc.nextInt();}
        int n=sc.nextInt();
        int a[] =new int[n];
        int b[] =new int[n];
        long l[] = new long[n+1];
        long r[] = new long[n+1];
        for(int i=0; i<n; i++) {
            a[i]=sc.nextInt();
            b[n-i-1] = a[i];
        }
        for(int i=1; i<=n; i++) {
            l[i] = l[i-1] + a[i-1];
            r[i] = r[i-1] + b[i-1];
        }
        db(l);
        db(r);
        long ans = 1L<<62;
        for(int i=1; i+2<n; i++) {
            long h = l[i+1]/2;
            int  e=n-i-1;
            int w = binarySearch(l,h); 
            if(w<0) w=~w;
            h = r[e]/2;
            int q = binarySearch(r,h);
            if(q<0) q=~q;
            long d[] = {l[w],l[i+1]-l[w], r[q], r[e]-r[q]};
            // db(d);
            sort(d);
            ans= min(ans, abs(d[0]-d[3]));

            if(w-1>0) {
                d[0]=l[w-1];
                d[1]=l[i+1]-l[w-1];
                d[2]=r[q]; 
                d[3]=r[e]-r[q];
                db(d);
                sort(d);
                ans= min(ans, abs(d[0]-d[3]));
                if(q-1>0) {
                    d[0]=l[w-1];
                    d[1]=l[i+1]-l[w-1];
                    d[2]=r[q-1]; 
                    d[3]=r[e]-r[q-1];
                    // db(d);
                    sort(d);
                    ans= min(ans, abs(d[0]-d[3]));
                }
            }
            if(q-1>0) {
                d[0]=l[w];
                d[1]=l[i+1]-l[w];
                d[2]=r[q-1]; 
                d[3]=r[e]-r[q-1];
                // db(d);
                sort(d);
                ans= min(ans, abs(d[0]-d[3]));
            }

                // if(w-1>0)
            // d = {l[w],l[i+1]-l[w], r[q], r[e]-r[q]};

            //             db(i+1,e);

        }


        out.println(ans);
        out.flush();
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
     
    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }
    
    static boolean bit(long x,int k){
        // weather k-th bit (from right) be one or zero
        return  ( 0 < ( (x>>k) & 1 )  )  ? true:false;
    }    

    class P implements Comparable<P>{
        long x;
        double d;
        P(long x, double d) {
            this.x=x;
            this.d=d;
        } 
          
        public int compareTo(P p){
            return (d - p.d)>0?1:-1;//des
        } 
    }
}
class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}