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 solve1(n: usize, s: &[usize]) -> usize {
    let rest = s[n];
    let target = rest / 4;
    let idx = s.lower_bound(&target);
    let mut res = usize::max_value();
    for d in -5..5 {
        let nidx = idx as isize + d;
        if nidx >= 0 as isize && nidx < n as isize - 2 {
            res = min(res, solve2(n, s, s[nidx as usize], nidx as usize));
        }
    }
    res
}
fn solve2(n: usize, s: &[usize], p: usize, b: usize) -> usize {
    let rest = s[n] - p;
    let target = p + rest / 3;
    let idx = s.lower_bound(&target);
    let mut res = usize::max_value();
    for d in -5..5 {
        let nidx = idx as isize + d;
        if nidx > b as isize && nidx < n as isize - 1{
            res = min(res, solve3(n, s, p, s[nidx as usize] - p, nidx as usize));
        }
    }
    res
}
fn solve3(n: usize, s: &[usize], p: usize, q: usize, b: usize) -> usize {
    let rest = s[n] - p - q;
    let target = p + q + rest / 2;
    let idx = s.lower_bound(&target);
    let mut res = usize::max_value();
    for d in -5..5 {
        let nidx = idx as isize + d;
        if nidx > b as isize && nidx < n as isize {
            res = min(res, solve4(n, s, p, q, s[nidx as usize] - p - q));
        }
    }
    res
}
fn solve4(n: usize, s: &[usize], p: usize, q: usize, r: usize) -> usize {
    let rest = s[n] - p - q - r;
    let a = min(min(p, q), min(r, rest));
    let b = max(max(p, q), max(r, rest));
    b - a
}
fn main() {
    let n = readln();
    let a: Vec<usize> = readln();
    let mut s = Vec::new();
    s.push(0);
    for i in 0..n {
        let ns = s[i] + a[i];
        s.push(ns);
    }

    println!("{}", solve1(n, &s));
}

use std::cmp::Ordering;
#[doc = " Equivalent to std::lowerbound and std::upperbound in c++"]
pub trait BinarySearch<T> {
    fn lower_bound(&self, &T) -> usize;
    fn upper_bound(&self, &T) -> usize;
}
impl<T: Ord> BinarySearch<T> for [T] {
    fn lower_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less => {
                    low = mid + 1;
                }
                Ordering::Equal | Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
    fn upper_bound(&self, x: &T) -> usize {
        let mut low = 0;
        let mut high = self.len();
        while low != high {
            let mid = (low + high) / 2;
            match self[mid].cmp(x) {
                Ordering::Less | Ordering::Equal => {
                    low = mid + 1;
                }
                Ordering::Greater => {
                    high = mid;
                }
            }
        }
        low
    }
}


#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet, VecDeque};

pub trait FromLn {
    fn fromln(s: &str) -> Self;
}
pub fn readln<T: FromLn>() -> T {
    let mut buf = String::new();
    let _ = ::std::io::stdin().read_line(&mut buf).unwrap();
    T::fromln(buf.trim())
}
pub fn readlns<T: FromLn>(n: usize) -> Vec<T> {
    let mut vs = vec![];
    for _ in 0..n {
        vs.push(readln());
    }
    vs
}
macro_rules! fromln_primitives {
    ($($t:ty),*) => { $(
        impl FromLn for $t {
            fn fromln(s: &str) -> $t {
                s.parse().unwrap()
            }
        }
    )* }
}
fromln_primitives!(
    String,
    bool,
    f32,
    f64,
    isize,
    i8,
    i16,
    i32,
    i64,
    usize,
    u8,
    u16,
    u32,
    u64
);
impl<T> FromLn for Vec<T>
where
    T: FromLn,
{
    fn fromln(s: &str) -> Vec<T> {
        s.split_whitespace().map(T::fromln).collect()
    }
}
impl FromLn for Vec<char> {
    fn fromln(s: &str) -> Vec<char> {
        s.chars().collect()
    }
}
macro_rules! fromln_tuple {
    ($($t:ident),*) => {
        impl<$($t),*> FromLn for ($($t),*) where $($t: FromLn),* {
            fn fromln(s: &str) -> ($($t),*) {
                let mut it = s.split_whitespace();
                let t = ($($t::fromln(it.next().unwrap())),*);
                assert_eq!(it.next(), None);
                t
            }
        }
    }
}
fromln_tuple!(A, B);
fromln_tuple!(A, B, C);
fromln_tuple!(A, B, C, D);
fromln_tuple!(A, B, C, D, E);
fromln_tuple!(A, B, C, D, E, F);