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)

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}

fn accum_sum(inputs: &Vec<i64>) -> Vec<i64> {
    let mut ret: Vec<i64> = vec![0; inputs.len()];
    ret[0] = inputs[0];
    for i in 1..inputs.len() {
        ret[i] = ret[i - 1] + inputs[i];
    }
    return ret;
}

fn main() {
    let a:Vec<i64> = vec![5, 1, 3, 5, 10, 7, 4, 9, 2, 8];
    let a:Vec<i64> = vec![3, 2, 4, 1, 2];
    let a:Vec<i64> = vec![10, 71, 84, 33, 6, 47, 23, 25, 52, 64];
    // let a:Vec<i64> = vec![1, 2, 3, 1000000000, 4, 5, 6];
    let accum = accum_sum(&a);
    // println!("{:?}", accum);

    let mut s: usize = 0;
    let mut t: usize = 1;
    let mut best_split1 = vec![0; a.len()];
    while s < a.len() {
        while t < a.len() && accum[s] > accum[t] - accum[s] {
            best_split1[t] = s;
            t += 1;
        }
        if  t == a.len() {
            break;
        }
        s += 1;
    }
    // println!("before {:?}", best_split1);
    for t in 1..best_split1.len() {
        let s = best_split1[t];
        if s == 0 {
            continue;
        }
        if (accum[t] - accum[s] - accum[s]).abs() > (accum[t] - accum[s-1] - accum[s-1]).abs() {
            best_split1[t] = s - 1;
        }
    }
    // println!("after {:?}", best_split1);

    let mut s: usize = a.len() - 1;
    let mut t: usize = a.len() - 1;
    let mut best_split2 = vec![0; a.len()];
    'outer: loop {
        while accum[a.len() - 1] - accum[s] > accum[s] - accum[t] {
            best_split2[t] = s;
            if  t == 0 {
                break 'outer;
            }
            t -= 1;
        }
        s -= 1;
    }
    // println!("before {:?}", best_split2);
    for t in 1..best_split2.len() {
        let s = best_split2[t];
        if s == 0 {
            continue;
        }
        if (accum[a.len() - 1] - accum[s] - accum[s] + accum[t]).abs() >
           (accum[a.len() - 1] - accum[s + 1] - accum[s + 1] + accum[t]).abs() {
            best_split2[t] = s + 1;
        }
    }
    // println!("after {:?}", best_split2);

    let mut min_elem:i64 = 1000000000000000000;
    for t in 1..a.len() - 1 {
        let s1 = best_split1[t];
        let s2 = best_split2[t];
        // println!("{0} {1} {2}", s1, t, s2);
        let mut sums = vec![0; 4];
        sums[0] = accum[s1];
        sums[1] = accum[t] - accum[s1];
        sums[2] = accum[s2] - accum[t];
        sums[3] = accum[a.len() - 1] - accum[s2];
        if let Some(max) = sums.iter().max() {
            if let Some(min) = sums.iter().min() {
                // println!("{:?}", sums);
                // println!("max: {0} min: {1}", max, min);
                min_elem = std::cmp::min(min_elem, max - min);
            }
        }
    }
    println!("{:?}", min_elem);
}