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.42.0) (judge verdict: Wrong Answer)

use std::cmp::{max, min};
use std::io::{stdin, Read};

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: ty),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize, a: [usize; n]);
    let s: Vec<_> = a
        .iter()
        .scan(0, |s, &ai| {
            *s += ai;
            Some(*s)
        })
        .collect();
    let diff = |a, b| if a > b { a - b } else { b - a };
    let mut ab = vec![(std::usize::MAX / 10, 0, 0); n];
    let mut i = 1usize;
    let l = |i, _| s[i - 1];
    let r = |i, j| s[j - 1] - s[i - 1];
    for j in 2..(n - 2) {
        let mut previ = i;
        while r(i, j) > l(i, j) && j - 1 > i {
            previ = i;
            i += 1;
        }
        let mut x = diff(r(i, j), l(i, j));
        let y = diff(r(previ, j), l(previ, j));
        if x > y {
            i = previ;
            x = y;
        }
        ab[j] = (x, r(i, j), l(i, j));
    }

    let mut cd = vec![(std::usize::MAX / 10, 0, 0); n];
    let mut i = 3usize;
    let l = |i, j| s[i - 1] - s[j - 1];
    let r = |i, _| s[n - 1] - s[i - 1];
    for j in 2..(n - 2) {
        let mut previ = i;
        while r(i, j) > l(i, j) && n - 1 > i {
            previ = i;
            i += 1;
        }
        let mut x = diff(r(i, j), l(i, j));
        let y = diff(r(previ, j), l(previ, j));
        if x > y {
            i = previ;
            x = y;
        }
        cd[j] = (x, r(i, j), l(i, j));
    }
    let mut ret = std::usize::MAX;
    for i in 2..(n - 2) {
        let (_, a, b) = ab[i];
        let (_, c, d) = cd[i];
        let m0 = min(a, min(b, min(c, d)));
        let m1 = max(a, max(b, max(c, d)));
        ret = min(ret, diff(m0, m1));
        assert_eq!(a + b + c + d, s[n - 1]);
    }
    println!("{}", ret);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}