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::io::stdin;
fn get_string() -> String {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

macro_rules! input {
    ($single:ty) => ( get_string().parse::<$single>().unwrap() );
    ($plural:ty,) => ({
        let line = get_string();
        line.split_whitespace().map(|v| v.parse::<$plural>().unwrap()).collect::<Vec<$plural>>()
    });
    ($($plural:ty),+) => ({
        let line = get_string();
        let mut iter = line.split_whitespace();
        ($(iter.next().unwrap().parse::<$plural>().unwrap()),+)
    });
    ($single:ty; $times:expr) => ( (0..$times).map(|_| input!($single)).collect::<Vec<$single>>() );
    ($($plural:ty),+; $times:expr) => ( (0..$times).map(|_| input!($($plural),+)).collect::<Vec<($($plural),+)>>() );
}

#[allow(dead_code)]
fn get_value<T: std::str::FromStr>() -> T {
    get_string().parse().ok().unwrap()
}
#[allow(dead_code)]
fn get_values_for<T: std::str::FromStr>(times: u64) -> Vec<T> {
    (0..times).map(|_| get_value()).collect()
}
#[allow(dead_code)]
fn get_vector<T: std::str::FromStr>() -> Vec<T> {
    get_string()
        .split_whitespace()
        .map(|v| v.parse().ok().unwrap())
        .collect()
}
#[allow(dead_code)]
fn get_vectors_for<T: std::str::FromStr>(times: u64) -> Vec<Vec<T>> {
    (0..times).map(|_| get_vector()).collect()
}

fn cumsum<T>(a: T) -> Vec<T::Item>
where
    T: IntoIterator,
    T::Item: std::ops::Add<Output = T::Item> + std::default::Default + std::marker::Copy,
{
    let mut cum = vec![Default::default()];
    cum.extend(a.into_iter().scan(Default::default(), |s, v| {
        *s = v + *s;
        Some(*s)
    }));
    cum
}

fn main() {
    let n = input!(usize);
    let mut a = input!(i64,);
    let lcm = cumsum(a.clone());
    a.reverse();
    let mut rcm = cumsum(a);
    rcm.reverse();

    let mut l = 1;
    let mut r = 3;
    let mut ans = Vec::new();
    for m in 2..n - 1 {
        if r - m == 0 {
            r += 1;
        }
        while r < n - 1 {
            let now = (rcm[m] - 2 * rcm[r]).abs();
            let next = (rcm[m] - 2 * rcm[r + 1]).abs();
            if next < now {
                r += 1;
            } else {
                break;
            }
        }
        while l < m - 1 {
            let now = (lcm[m] - 2 * lcm[l]).abs();
            let next = (lcm[m] - 2 * lcm[l + 1]).abs();
            if next < now {
                l += 1;
            } else {
                break;
            }
        }
        ans.push(calc(&lcm, &rcm, l, m, r));
    }
    println!("{}", ans.iter().min().unwrap());
}

fn calc(lcm: &Vec<i64>, rcm: &Vec<i64>, l: usize, m: usize, r: usize) -> i64 {
    let mut sum: Vec<i64> = Vec::with_capacity(4);
    sum.push(lcm[l]);
    sum.push(lcm[m] - lcm[l]);
    sum.push(rcm[m] - rcm[r]);
    sum.push(rcm[r]);
    sum.iter().max().unwrap() - sum.iter().min().unwrap()
}