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 Rust (1.15.1) (judge verdict: Wrong Answer)

// AtCoder My Practice
// author: Leonardone @ NEETSDKASU

fn main() {
	let mut stdin = String::new();
    std::io::Read::read_to_string(
    	&mut std::io::stdin(),
        &mut stdin).unwrap();
	let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    macro_rules! get {
    	($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(i64));
    }
    
    let n = get!(usize);
    let mut sums = vec![0; n+1];
    for i in 0..n {
    	let x = get!();
        sums[i+1] = sums[i]+x;
    }
    let mut ans = std::i64::MAX;
    for i in 2..n-1 {
        let hp1 = bs(&sums, 0, i);
        let hp2 = bs(&sums, i, n+1);
        if hp1 < 1 || hp1 >= i {
        	continue;
        }
        if hp2 <= i || hp2 >= n {
        	continue;
        }
        let mut xs = vec![];
        xs.push(sums[hp1]);
        xs.push(sums[i]-sums[hp1]);
        xs.push(sums[hp2]-sums[i]);
        xs.push(sums[n]-sums[hp2]);
        xs.sort();
        let tmp = xs[3]-xs[0];
        if tmp < ans {
        	ans = tmp;
        }
    }
    println!("{}", ans);
}

fn bs(sums: &Vec<i64>, mut lp: usize, mut rp: usize) -> usize {
	let lp0 = lp;
    let rp0 = rp;
    let df = sums[lp];
    let tag = (sums[rp-1] - df) / 2 + df;
    lp += 1;
    while lp < rp {
    	let hp = (rp - lp) / 2 + lp;
        if sums[hp] > tag {
        	rp = hp;
        } else {
        	lp = hp + 1;
        }
    }
    let mut ds = vec![];
    if lp > lp0 && lp < rp0 {
    	let d1 = (sums[rp0-1]-sums[lp])-(sums[lp]-sums[lp0]);
        ds.push((d1, lp));
	}
    lp += 1;
    if lp > lp0 && lp < rp0 {
    	let d1 = (sums[rp0-1]-sums[lp])-(sums[lp]-sums[lp0]);
        ds.push((d1, lp));
	}
    lp -= 1;
    if lp > lp0 && lp < rp0 {
    	let d1 = (sums[rp0-1]-sums[lp])-(sums[lp]-sums[lp0]);
        ds.push((d1, lp));
	}
    if ds.len() > 0 {
    	ds.sort();
    	ds[0].1
	} else {
    	0
    }
}