#![allow(non_snake_case)]

#[allow(unused_macros)]
macro_rules! debug {
    ( $x:ident [ $i:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}] = {:?}", stringify!($x), $i, $x[$i]);
        }
    };
    ( $x:ident [ $i:expr ][ $j:expr ] ) => {
        if cfg!(debug_assertions) {
            println!("{}[{}][{}] = {:?}", stringify!($x), $i, $j, $x[$i][$j]);
        }
    };
    ( $x:expr ) => {
        if cfg!(debug_assertions) {
            println!("{}:{} {} = {:?}", line!(), column!(), stringify!($x), $x);
        }
    };
}

fn main() {
    let stdin = std::io::stdin();
    let mut stdin = my::io::InputReader::new(stdin.lock());

    let N: usize = stdin.read();
    let A: Vec<i64> = (0..N).map(|_| stdin.read()).collect();

    let mut acc_A = vec![0_i64; N + 1];
    for i in 0..N {
        acc_A[i + 1] = acc_A[i] + A[i];
    }

    let mut result = std::i64::MAX;

    let mut sep_l = vec![0_usize; N + 1];
    sep_l[2] = 1;
    for sep_c in 3..N - 2 + 1 {
        let mut i = sep_l[sep_c - 1];
        let mut diff = (acc_A[i] - (acc_A[sep_c] - acc_A[i])).abs();
        while i + 1 <= sep_c - 1 {
            let diff_new = (acc_A[i + 1] - (acc_A[sep_c] - acc_A[i + 1])).abs();
            if diff >= diff_new { // ここのイコール
                diff = diff_new;
                i += 1;
            } else {
                break;
            }
        }
        sep_l[sep_c] = i;
    }

    let mut sep_r = vec![0_usize; N + 1];
    sep_r[N - 2] = N - 1;
    for sep_c in (2..N - 2).rev() {
        let mut i = sep_r[sep_c + 1];
        let mut diff = ((acc_A[i] - acc_A[sep_c]) - (acc_A[N] - acc_A[i])).abs();
        while i - 1 >= sep_c + 1 {
            let diff_new = ((acc_A[i - 1] - acc_A[sep_c]) - (acc_A[N] - acc_A[i - 1])).abs();
            if diff >= diff_new {
                diff = diff_new;
                i -= 1;
            } else {
                break;
            }
        }
        sep_r[sep_c] = i;
    }

    for sep_c in 2..N - 2 + 1 {
        let mut p = vec![0; 5];
        p[0] = 0;
        p[1] = sep_l[sep_c];
        p[2] = sep_c;
        p[3] = sep_r[sep_c];
        p[4] = N;
        debug!(p[1]);
        debug!(p[2]);
        debug!(p[3]);
        let s: Vec<i64> = (0..4).map(|i| acc_A[p[i + 1]] - acc_A[p[i]]).collect();
        debug!(s);
        result = std::cmp::min(result, *s.iter().max().unwrap() - *s.iter().min().unwrap());
    }
    println!("{}", result);
}

#[allow(dead_code, unused_must_use)]
mod my {
    pub mod io {
        use std::io::{BufRead, ErrorKind, Result};
        use std::str::{FromStr, from_utf8};
        use std::fmt::Debug;

        pub fn read_word<R: BufRead>(r: &mut R, buf: &mut Vec<u8>)
                                              -> Result<usize> {
            let mut read = 0;
            loop {
                let (done, used) = {
                    let available = match r.fill_buf() {
                        Ok(n) => n,
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e)
                    };
                    match available.iter().position(|&x| x == b'\n' || x == b' ') {
                        Some(i) => {
                            buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };
                r.consume(used);
                read += used;
                if done || used == 0 {
                    return Ok(read);
                }
            }
        }

        pub struct InputReader<R: BufRead> {
            reader: R,
            buf: Vec<u8>,
        }

        impl<R: BufRead> InputReader<R> {
            pub fn new(reader: R) -> InputReader<R> {
                InputReader {
                    reader: reader,
                    buf: Vec::<u8>::new(),
                }
            }

            pub fn read_word(&mut self) {
                read_word(&mut self.reader, &mut self.buf);
            }
            
            pub fn read<T>(&mut self) -> T
                where T: FromStr,
                      T::Err: Debug
            {
                self.buf.clear();
                self.read_word();
                from_utf8(&self.buf).unwrap().trim().parse().unwrap()
            }
        }
    }
}

Measured memory_bytes: 12540