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(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

pub trait InputValue {
    fn parse(s: &str) -> Self;
}

pub fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

pub fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

pub fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
	}
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
	($($t:ident),*) => {
		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
			fn parse(s: &str) -> ($($t),*) {
				let mut tokens = s.split_whitespace();
				let t = ($($t::parse(tokens.next().unwrap())),*);
				t
			}
		}
	}
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

// ===

fn main() {
    let n: usize = read();
    let a: Vec<i64> = readnc();

    let mut imos: Vec<i64> = vec![];
    imos.push(0);
    for i in 0..n {
        let sum = imos[i] + a[i];
        imos.push(sum);
    }

    let mut best: i64 = 1e15 as i64;
    for i in 2..n-1 {
        let ((al, bl),(cl, dl)) = split(0, i, &imos);
        let ((ar, br),(cr, dr)) = split(i, n, &imos);

        best = min(best, diff(al, bl, ar, br));
        best = min(best, diff(al, bl, cr, dr));
        best = min(best, diff(cl, dl, ar, br));
        best = min(best, diff(cl, dl, cr, dr));
    }
    println!("{}", best);
}

fn diff(a: i64, b: i64, c: i64, d: i64) -> i64 {
    let min = min(min(a, b), min(c, d));
    let max = max(max(a, b), max(c, d));
    return max - min;
}

fn split(f: usize, t: usize, imos: &Vec<i64>) -> ((i64, i64), (i64, i64)) {
    let sum = imos[t] - imos[f];
    if t - f <= 2 {
        let one = imos[f+1] - imos[f];
        let two = imos[f+2] - imos[f+1];
        return ((one, two), (one, two));
    }

    let mut low = f;
    let mut high = t;
    while (high - low > 1) {
        let med = (low + high) / 2;
        let left = imos[med] - imos[f];
        let right = imos[t] - imos[med];
        if left < sum / 2 {
            low = med;
        } else {
            high = med;
        }
    }
    let one = imos[low] - imos[f];
    let two = imos[t] - imos[low];

    if low+1 == t {
        return ((one, two), (one, two))
    } else {
        let onen = imos[low+1] - imos[f];
        let twon = imos[t] - imos[low+1];
        return ((one, two), (onen, twon));
    }
}