use std::io::*;
use std::str::FromStr;
use std::cmp;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

/*
　0-originの配列の、i番目からj-1番目までの数列の和
 */
fn section_sum(cumsum: &Vec<i64>, i: usize, j: usize) -> i64 {
    let ans = match j - i > 0 {
        true => cumsum[j] - cumsum[i],
        false => cumsum[i] - cumsum[j],
    };
    ans
}

fn main() {
    let n = read::<i64>();
    let mut a = vec![0; n as usize];
    let mut cumsum = vec![0; (n + 1) as usize];
    for i in 0..n {
        a[i as usize] = read::<i64>();
        cumsum[(i+1) as usize] = cumsum[i as usize] + a[i as usize];
    }
    //println!("{:?}", a);
    //println!("{:?}", cumsum);
    //println!("{}", section_sum(&cumsum, 0, 3));
    // 真ん中の仕切りを固定した時の左側配列の切れ目
    // i番目とi+1番目の間で仕切る→leftにはi+1が入る
    let mut left: Vec<i64> = Vec::new();
    //println!("left : {:?}", left);
    let mut right: Vec<i64> = Vec::new();
    let mut l1 = 0;
    let mut l2 = 0;
    let mut r1 = 0;
    let mut r2 = 0;
    let mut ans = 100000000000;
    // i-1とiの間で切る
    for i in 2..n-1 {
        // 左の仕切りは単調増加
        let mut new_left = 1;
        if left.len() > 0 {
            new_left = left[left.len() - 1];
        }
        let mut sum1 = section_sum(&cumsum, 0, new_left as usize);
        //println!("{:?}", sum1);
        let mut sum2 = section_sum(&cumsum, new_left as usize, i as usize);
        let mut diff = (sum2-sum1).abs();
        //println!("{:?}", sum2);
        loop {
            //println!("{}", new_left);
            sum1 = section_sum(&cumsum, 0, (new_left+1) as usize);
            sum2 = section_sum(&cumsum, (new_left+1) as usize, i as usize);
            if diff  < (sum2-sum1).abs() {
                break;
            } 
            //println!("{}", sum1);
            //println!("{}", sum2);
            diff = (sum2-sum1).abs();
            new_left += 1;
        }
        l1 = section_sum(&cumsum, 0, new_left as usize);
        l2 = section_sum(&cumsum, new_left as usize, i as usize);
        left.push(new_left);
        //println!("{} {}", new_left, i);
        let mut new_right = i + 1;
        if right.len() > 0 {
            new_right = right[right.len() - 1];
        }
        let mut sum1 = section_sum(&cumsum, i as usize, new_right as usize);
        //println!("{:?}", sum1);
        let mut sum2 = section_sum(&cumsum, new_right as usize, n as usize);
        let mut diff = (sum2-sum1).abs();
        //println!("{:?}", sum2);
        loop {
            //println!("{}", new_left);
            sum1 = section_sum(&cumsum, i as usize, (new_right+1) as usize);
            sum2 = section_sum(&cumsum, (new_right+1) as usize, n as usize);
            if diff  < (sum2-sum1).abs() {
                break;
            } 
            //println!("{}", sum1);
            //println!("{}", sum2);
            diff = (sum2-sum1).abs();
            new_right += 1;
        }
        right.push(new_right);
        r1 = section_sum(&cumsum, i as usize, new_right as usize);
        r2 = section_sum(&cumsum, new_right as usize, n as usize);
        let tmp = cmp::max(cmp::max(l1,l2), cmp::max(r1,r2)) - cmp::min(cmp::min(l1,l2), cmp::min(r1,r2));
        ans = cmp::min(ans, tmp);
        //println!("{} {}", new_right, i);
    }
    //println!("left : {:?}", left);
    //println!("right : {:?}", right);
    println!("{}", ans);
}

Measured memory_bytes: 14588