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::cmp;

fn main() {
    let mut sc = Scanner::new();
    let n = sc.read();
    let a: Vec<usize> = sc.read_vec(n);
    let mut sum = vec![0; n + 1];
    for i in 0..n {
        sum[i + 1] = sum[i] + a[i];
    }

    let mut ans = sum[n];
    for suffix_head in 2..(n - 1) {
        let prefix_sum = sum[suffix_head];
        let suffix_sum = sum[n] - sum[suffix_head];

        let p = sum
            .binary_search_by_key(&(prefix_sum / 2 * 2 - 1), |&b| b * 2)
            .err()
            .unwrap();
        let s = sum
            .binary_search_by_key(&(suffix_sum / 2 * 2 + prefix_sum * 2 - 1), |&b| b * 2)
            .err()
            .unwrap();
        let p2_head_from = if p <= 4 { 1 } else { p - 4 };
        let p2_head_to = cmp::min(suffix_head, p + 4);

        let s2_head_from = cmp::max(if s >= 4 { s - 4 } else { 0 }, suffix_head + 1);
        let s2_head_to = cmp::min(n, s + 4);

        for p2_head in p2_head_from..p2_head_to {
            for s2_head in s2_head_from..s2_head_to {
                let p1_sum = sum[p2_head];
                let p2_sum = sum[suffix_head] - sum[p2_head];
                let s1_sum = sum[s2_head] - sum[suffix_head];
                let s2_sum = sum[n] - sum[s2_head];

                let sub_ans =
                    max4(p1_sum, p2_sum, s1_sum, s2_sum) - min4(p1_sum, p2_sum, s1_sum, s2_sum);
                ans = cmp::min(ans, sub_ans);
            }
        }
    }

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

fn max4(x: usize, y: usize, z: usize, w: usize) -> usize {
    cmp::max(cmp::max(x, y), cmp::max(z, w))
}

fn min4(x: usize, y: usize, z: usize, w: usize) -> usize {
    cmp::min(cmp::min(x, y), cmp::min(z, w))
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner {
        Scanner {
            ptr: 0,
            length: 0,
            buf: vec![0; 1024],
            small_cache: vec![0; 1024],
        }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool {
        b == b'\n' || b == b'\r' || b == b'\t' || b == b' '
    }

    fn read_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.read()).collect()
    }

    fn usize_read(&mut self) -> usize {
        self.read()
    }

    fn read<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)])
                    .parse()
                    .unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}