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: Accepted)

use std::str::FromStr;
use std::fmt::Debug;
use std::cmp;

#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()
}

#[allow(dead_code)]
fn read_as_string() -> String{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn acc_vec(v: &[i64]) -> Vec<i64>{
    let mut res = vec![0;v.len()+1];
    let mut acc: i64 = 0;
    for i in 0..v.len(){
        acc += v[i];
        res[i+1] = acc;
    }
    res
}

//search the best position to cut equally
//return $pos: sum of [left..pos) and sum of [pos..right) are the nearest
fn cut_equally(v: &[i64], left: usize, right: usize) -> usize {
    if right > v.len() || right - left <= 1{
        panic!("wrong params");
    }

    let (mut l, mut r) = (left, right);

    let mut cur_res = (0 as usize, std::i64::MAX);
    while l < r {
        //println!("l = {}, r = {}", l, r);
        let mid = (l+r)/2;
        // left sum = [left..mid)
        // right sum = [mid..right)
        let left_sum = v[mid-1] - v[left-1];
        let right_sum = v[right-1] - v[mid-1];
        //println!("left sum = {}, right sum = {}", left_sum, right_sum);
        if (right_sum - left_sum).abs() < cur_res.1{
            cur_res = (mid, (right_sum - left_sum).abs());
        }
        if right_sum > left_sum{
            l = mid+1;
        }else{
            r = mid;
        }
    }

    return cur_res.0;
}

fn sumup(v: &[i64], left: usize, right: usize, mid: usize) -> (i64, i64){
    return (v[mid-1] - v[left-1], v[right-1] - v[mid-1]);
}

fn main(){
    let n = read_as_vec::<usize>()[0];

    let a = read_as_vec::<i64>();
    let a = acc_vec(&a);

    let mut ans = std::i64::MAX;
    for i in 3..n{
        //split to [0..i) and [i..n)

        //binary search for each left and right
        //cut the vector equally as possible
        //left side
        let left_border = cut_equally(&a, 1, i);

        //right side
        let right_border = cut_equally(&a, i, n+1);

        let pq = sumup(&a, 1, i, left_border);
        let rs = sumup(&a, i, n+1, right_border);
        let (p, q) = (pq.0, pq.1);
        let (r, s) = (rs.0, rs.1);

        let max_val = cmp::max(p, cmp::max(q, cmp::max(r, s)));
        let min_val = cmp::min(p, cmp::min(q, cmp::min(r, s)));

        if (max_val - min_val).abs() < ans{
            ans = (max_val - min_val).abs();
        }
    }

    println!("{}", ans);
}