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.42.0) (judge verdict: Time Limit Exceeded)

use std::io;
use std::str::FromStr;

fn input<T:FromStr>() -> T {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    line.trim().parse().ok().unwrap()
}

fn input_vec() -> Vec<i64> {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    line.split_whitespace().map(|s| s.parse().unwrap()).collect()
}

// 累積和
struct Cummulation {
    sums: Vec<i64>
}

impl Cummulation {
    fn new(target: &Vec<i64>) -> Cummulation {
        let mut cumulation_vec: Vec<i64> = Vec::new();
        cumulation_vec.push(0);
        let mut pre = 0;
        for i in target {
            let n = i + pre;
            cumulation_vec.push(n);
            pre = n;
        }
        Cummulation {sums: cumulation_vec}
    }

    fn get(&self, left: usize, right: usize) -> i64 {
        self.sums[right + 1] - self.sums[left]
    }
}

fn binary_search(left_boundary: usize, right_boundary: usize, vec: &Vec<i64>) -> i64 {
    let mut left = left_boundary as i64 - 1;
    let mut right = right_boundary as i64 + 1;
    let mut mid: i64;
    let cummulation = Cummulation::new(&vec);
    loop {
        if (right - left).abs() <= 1 {
            if left < 0 {
                return right;
            }
            if right >= right_boundary as i64 + 1 {
                return left;
            }
            if (cummulation.get(left_boundary, left as usize) - cummulation.get(left as usize + 1, right_boundary)).abs() 
            < (cummulation.get(left_boundary, right as usize) - cummulation.get(right as usize + 1, right_boundary)).abs() {
                return left;
            } else {
                return right;
            }
        }

        mid = (left + right) / 2;
        if cummulation.get(left_boundary, mid as usize) < cummulation.get(mid as usize + 1, right_boundary) {
            left = mid;
        } else {
            right = mid;
        }   
    }

}

fn main() {
    let n: usize = input();
    let a_vec = input_vec();
    let mut sum_vec: Vec<i64> = Vec::new();
    sum_vec.push(0);
    for i in 0..n {
        sum_vec.push(a_vec[i] + sum_vec[i]);
    }
    let cummulation = Cummulation::new(&a_vec);
    let mut ans = std::i64::MAX;
    for half in 1..(n-2) {
        let mut candidates: Vec<i64> = Vec::new();
        let quater = binary_search(0, half, &a_vec);
        candidates.push(cummulation.get(0, quater as usize));
        candidates.push(cummulation.get(quater as usize + 1, half));

        let quater_third = binary_search(half + 1, n - 1, &a_vec);
        candidates.push(cummulation.get(half + 1, quater_third as usize));
        candidates.push(cummulation.get(quater_third as usize + 1, n - 1));
        candidates.sort();
        if candidates[3] - candidates[0] < ans {
            ans = candidates[3] - candidates[0];
        }
    }
    println!("{}", ans);
}