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::cmp;

fn input_with_space_singleline() -> Vec<i64>{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).expect("!");
    s.trim().split(" ").map(|ref x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>()
}

pub fn main(){
    let N = input_with_space_singleline()[0] as usize;
    let A = input_with_space_singleline();
    // 累積和
    let mut _accum = 0i64;
    let mut accum = Vec::<i64>::new();
    for a in &A {
        _accum += *a;
        accum.push(_accum);
    }
    //仕切り(範囲には仕切りも含む)
    let mut upper = 0usize;
    let mut bottom = 2usize;
    let mut result: i64 = 10i64.pow(18u32);
    for middle in 1usize..(N-2) as usize {
        //尺取り法（上部
        loop{
            if upper == middle - 1 {
                break;
            }
            let u = accum[upper ];
            let b = accum[middle ] - accum[upper ];
            if (u-b).abs() > ((u + A[(upper + 1) ]) - (b - A[(upper + 1) ])).abs() {
                upper += 1;
            }else{
                break;
            }
        }
        //尺取り法（下部）
        loop{
            if bottom == N - 2 {
                break;
            }
            let u = accum[bottom ] - accum[middle ];
            let b = accum[(N-1) ] - accum[bottom ];
            if (u-b).abs() > ((u+A[(bottom+1) ])- (b-A[(bottom+1) ])).abs() {
                bottom += 1;
            }else{
                break;
            }
        }
        //最小値を求める
        let t: Vec<i64> = vec![accum[N-1] - accum[bottom], accum[bottom] - accum[middle], accum[middle] - accum[upper], accum[upper]];
        result = cmp::min(result, (*t.iter().max().unwrap() - *t.iter().min().unwrap()) as i64);
    }
    println!("{}", result);
}