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)

static LEN: isize = 100;

fn Asolve1(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 -LEN..LEN {
        let nidx = idx as isize + d;
        if nidx >= 0 as isize && nidx < n as isize - 2 {
            res = min(res, Asolve2(n, s, s[nidx as usize], nidx as usize));
        }
    }
    res
}
fn Asolve2(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 -LEN..LEN {
        let nidx = idx as isize + d;
        if nidx > b as isize && nidx < n as isize - 1{
            res = min(res, Asolve3(n, s, p, s[nidx as usize] - p, nidx as usize));
        }
    }
    res
}
fn Asolve3(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 -LEN..LEN {
        let nidx = idx as isize + d;
        if nidx > b as isize && nidx < n as isize {
            res = min(res, Asolve4(n, s, p, q, s[nidx as usize] - p - q));
        }
    }
    res
}
fn Asolve4(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 solve1(n: usize, s: &[usize]) -> usize {
    let rest = s[n];
    let target = rest / 2;
    let idx = s.lower_bound(&target);
    let mut res = usize::max_value();
    for d in -LEN..LEN {
        let nidx = idx as isize + d;
        if nidx > 0 as isize && nidx < n as isize {
            res = min(res, solve2(n, s, nidx as usize));
        }
    }
    res
}
fn solve2(n: usize, s: &[usize], b: usize) -> usize {
    let target = s[b] / 2;
    let idx = s.lower_bound(&target);
    let mut res = usize::max_value();
    for d in -LEN..LEN {
        let nidx = idx as isize + d;
        if nidx > 0 && nidx < b as isize {
            res = min(res, solve3(n, s, nidx as usize, b));
        }
    }
    res
}
fn solve3(n: usize, s: &[usize], a: usize, b: usize) -> usize {
    let target = (s[n] - s[b]) / 2 + s[b];
    let idx = s.lower_bound(&target);
    let mut res = usize::max_value();
    for d in -LEN..LEN {
        let nidx = idx as isize + d;
        if nidx > b as isize && nidx < n as isize {
            res = min(res, solve4(n, s, a, b, nidx as usize));
        }
    }
    res
}
fn solve4(n: usize, s: &[usize], i: usize, j: usize, k: usize) -> usize {
    if !(0 < i && i < j && j < k && k < n) {
        return usize::max_value();
    }
    let p = s[i];
    let q = s[j] - s[i];
    let r = s[k] - s[j];
    let rest = s[n] - s[k];
    let a = min(min(p, q), min(r, rest));
    let b = max(max(p, q), max(r, rest));
    b - a
}
fn naive(n: usize, s: &[usize]) -> usize {
    let mut res = usize::max_value();
    for i in 0..n {
        for j in (i+1)..n {
            for k in (j+1)..n {
                let p = s[i];
                let q = s[j] - s[i];
                let r = s[k] - s[j];
                let rest = s[n] - s[k];
                let a = min(min(p, q), min(r, rest));
                let b = max(max(p, q), max(r, rest));
                if b - a < res {
                    res = min(res, b - a);
                    println!("{} {} {}", i, j, k);
                }
            }
        }
    }
    res
}
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);
    }

    let answer = min(solve1(n, &s), Asolve1(n, &s));
    //assert_eq!(answer, naive(n, &s));
    println!("{}", answer);
}

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);