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)

#![allow(non_snake_case)]

#[allow(unused_macros)]
macro_rules! debug {
    ( $x:ident [ $i:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}] = {:?}", stringify!($x), $i, $x[$i]);
        }
    };
    ( $x:ident [ $i:expr ][ $j:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}][{}] = {:?}", stringify!($x), $i, $j, $x[$i][$j]);
        }
    };
    ( $x:expr ) => {
        if cfg!(debug_assertions) {
            println!("{}:{} {} = {:?}", line!(), column!(), stringify!($x), $x);
        }
    };
}

fn main() {
    let stdin = std::io::stdin();
    let mut stdin = my::io::InputReader::new(stdin.lock());

    let N: usize = stdin.read();
    let A: Vec<i64> = (0..N).map(|_| stdin.read()).collect();

    let mut acc_A = vec![0_i64; N + 1];
    for i in 0..N {
        acc_A[i + 1] = acc_A[i] + A[i];
    }

    let mut result = std::i64::MAX;

    let mut sep_l = vec![0_usize; N + 1];
    sep_l[2] = 1;
    for sep_c in 3..N - 2 + 1 {
        let mut i = sep_l[sep_c - 1];
        let mut diff = (acc_A[i] - (acc_A[sep_c] - acc_A[i])).abs();
        while i + 1 <= sep_c - 1 {
            let diff_new = (acc_A[i + 1] - (acc_A[sep_c] - acc_A[i + 1])).abs();
            if diff >= diff_new { // ここのイコール
                diff = diff_new;
                i += 1;
            } else {
                break;
            }
        }
        sep_l[sep_c] = i;
    }

    let mut sep_r = vec![0_usize; N + 1];
    sep_r[N - 2] = N - 1;
    for sep_c in (2..N - 2).rev() {
        let mut i = sep_r[sep_c + 1];
        let mut diff = ((acc_A[i] - acc_A[sep_c]) - (acc_A[N] - acc_A[i])).abs();
        while i - 1 >= sep_c + 1 {
            let diff_new = ((acc_A[i - 1] - acc_A[sep_c]) - (acc_A[N] - acc_A[i - 1])).abs();
            if diff >= diff_new {
                diff = diff_new;
                i -= 1;
            } else {
                break;
            }
        }
        sep_r[sep_c] = i;
    }

    for sep_c in 2..N - 2 + 1 {
        let mut p = vec![0; 5];
        p[0] = 0;
        p[1] = sep_l[sep_c];
        p[2] = sep_c;
        p[3] = sep_r[sep_c];
        p[4] = N;
        debug!(p[1]);
        debug!(p[2]);
        debug!(p[3]);
        let s: Vec<i64> = (0..4).map(|i| acc_A[p[i + 1]] - acc_A[p[i]]).collect();
        debug!(s);
        result = std::cmp::min(result, *s.iter().max().unwrap() - *s.iter().min().unwrap());
    }
    println!("{}", result);
}

#[allow(dead_code, unused_must_use)]
mod my {
    pub mod io {
        use std::io::{BufRead, ErrorKind, Result};
        use std::str::{FromStr, from_utf8};
        use std::fmt::Debug;

        pub fn read_word<R: BufRead>(r: &mut R, buf: &mut Vec<u8>)
                                              -> Result<usize> {
            let mut read = 0;
            loop {
                let (done, used) = {
                    let available = match r.fill_buf() {
                        Ok(n) => n,
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e)
                    };
                    match available.iter().position(|&x| x == b'\n' || x == b' ') {
                        Some(i) => {
                            buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };
                r.consume(used);
                read += used;
                if done || used == 0 {
                    return Ok(read);
                }
            }
        }

        pub struct InputReader<R: BufRead> {
            reader: R,
            buf: Vec<u8>,
        }

        impl<R: BufRead> InputReader<R> {
            pub fn new(reader: R) -> InputReader<R> {
                InputReader {
                    reader: reader,
                    buf: Vec::<u8>::new(),
                }
            }

            pub fn read_word(&mut self) {
                read_word(&mut self.reader, &mut self.buf);
            }
            
            pub fn read<T>(&mut self) -> T
                where T: FromStr,
                      T::Err: Debug
            {
                self.buf.clear();
                self.read_word();
                from_utf8(&self.buf).unwrap().trim().parse().unwrap()
            }
        }
    }
}