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: Wrong Answer)

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

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")
}

struct App {
    // input
    n: usize,
    a: Vec<i64>,
    // solve
    thr: i64,
    sums: Vec<i64>,
    ans: i64,
}

impl App {
    fn input() -> App {
        let n: usize = read();
        let a: Vec<i64> = (0..n).map({|_| read::<i64>()}).collect();
        return App {
            n: n,
            a: a,
            thr: 0,
            sums: vec![0; 4],
            ans: 0,
        };
    }

    fn search_m(&mut self, l: usize, r: usize) {
        let sum_mid = self.a[l..r].iter().sum::<i64>();

        let mut sum: i64 = 0;
        for i in l..r {
            // [l, i), [i, r)
            self.sums[2] = sum;
            self.sums[3] = sum_mid - sum;
            // println!("{} {} {}", l, i, r);

            self.ans = std::cmp::min(
                self.ans,
                self.sums.iter().max().unwrap() - self.sums.iter().min().unwrap()
            );

            // for next iter
            sum += self.a[i];
        }
    }

    fn search_r(&mut self, l: usize) {
        let mut sum: i64 = 0;
        let mut j: usize = self.n;  // [j, self.n)

        while l < j {
            let x = self.a[j - 1];
            if sum + x >= self.thr {
                break;
            }

            j -= 1;
            sum += x;
        }

        // Pattern 1: not including a[j - 1]
        self.sums[1] = sum;
        self.search_m(l, j);

        // Pattern 2: including a[j - 1]
        if l <= j - 1 {
            self.sums[1] = sum + self.a[j - 1];
            self.search_m(l, j - 1);
        }
    }

    fn search_l(&mut self) {
        let mut sum: i64 = 0;
        let mut j: usize = 0;  // [0, j)

        while j < self.n {
            let x = self.a[j];

            if sum + x >= self.thr {
                break;
            }

            j += 1;
            sum += x;
        }use std::io::*;
        use std::str::FromStr;

        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")
        }

        struct App {
            // input
            n: usize,
            a: Vec<i64>,
            // solve
            thrL: i64,
            thrR: i64,
            sums: Vec<i64>,
            ans: i64,
        }

        impl App {
            fn input() -> App {
                let n: usize = read();
                let a: Vec<i64> = (0..n).map({|_| read::<i64>()}).collect();
                return App {
                    n: n,
                    a: a,
                    thrL: 0,
                    thrR: 0,
                    sums: vec![0; 4],
                    ans: 0,
                };
            }

            fn search_m(&mut self, l: usize, r: usize) {
                let sum_mid = self.a[l..r].iter().sum::<i64>();

                let mut sum: i64 = 0;
                for i in l..r {
                    // [l, i), [i, r)
                    self.sums[2] = sum;
                    self.sums[3] = sum_mid - sum;
                    // println!("{} {} {}", l, i, r);

                    self.ans = std::cmp::min(
                        self.ans,
                        self.sums.iter().max().unwrap() - self.sums.iter().min().unwrap()
                    );

                    // for next iter
                    sum += self.a[i];
                }
            }

            fn search_r(&mut self, l: usize) {
                let mut sum: i64 = 0;
                let mut j: usize = self.n;  // [j, self.n)

                while l < j {
                    let x = self.a[j - 1];
                    if sum + x >= self.thrR {
                        break;
                    }

                    j -= 1;
                    sum += x;
                }

                // Pattern 1: not including a[j - 1]
                self.sums[1] = sum;
                self.search_m(l, j);

                // Pattern 2: including a[j - 1]
                if l <= j - 1 {
                    self.sums[1] = sum + self.a[j - 1];
                    self.search_m(l, j - 1);
                }
            }

            fn search_l(&mut self) {
                let mut sum: i64 = 0;
                let mut j: usize = 0;  // [0, j)

                while j < self.n {
                    let x = self.a[j];

                    if sum + x >= self.thrL {
                        break;
                    }

                    j += 1;
                    sum += x;
                }

                // Pattern 1: not including a[j]
                self.sums[0] = sum;
                self.search_r(j);

                // Pattern 2: including a[j]
                if j < self.n {
                    self.sums[0] = sum + self.a[j];
                    self.search_r(j + 1);
                }
            }

            fn solve(&mut self) {
                self.ans = self.a.iter().sum();

                let thr = self.a.iter().sum::<i64>() / (4 as i64);
                self.thrL = thr;
                self.thrR = thr;
                self.search_l();

                let thr = (self.a.iter().sum::<i64>() - self.a.iter().max().unwrap()) / (4 as i64);
                self.thrL = thr;
                self.thrR = thr;
                self.search_l();

                let thr = self.a.iter().sum::<i64>() / (3 as i64);
                self.thrL = thr;
                self.thrR = thr;
                self.search_l();

                let thr = (self.a.iter().sum::<i64>() - self.a.iter().max().unwrap()) / (3 as i64);
                self.thrL = thr;
                self.thrR = thr;
                self.search_l();

                let max_i = (0..self.n).max_by_key(|&i| self.a[i]).unwrap() as usize;
                self.thrL = self.a[..max_i].iter().sum::<i64>();
                self.thrR = self.a[max_i + 1..].iter().sum::<i64>() / 2;
                self.search_l();

                let max_i = (0..self.n).max_by_key(|&i| self.a[i]).unwrap() as usize;
                self.thrL = self.a[..max_i].iter().sum::<i64>() / 2;
                self.thrR = self.a[max_i + 1..].iter().sum::<i64>();
                self.search_l();

                println!("{}", self.ans)
            }
        }


        fn main() {
            let mut app = App::input();
            app.solve();
        }


        // Pattern 1: not including a[j]
        self.sums[0] = sum;
        self.search_r(j);

        // Pattern 2: including a[j]
        if j < self.n {
            self.sums[0] = sum + self.a[j];
            self.search_r(j + 1);
        }
    }

    fn solve(&mut self) {
        self.ans = self.a.iter().sum();

        self.thr = self.a.iter().sum::<i64>() / (4 as i64);
        self.search_l();

        self.thr = (self.a.iter().sum::<i64>() - self.a.iter().max().unwrap()) / (4 as i64);
        self.search_l();

        self.thr = self.a.iter().sum::<i64>() / (3 as i64);
        self.search_l();

        self.thr = (self.a.iter().sum::<i64>() - self.a.iter().max().unwrap()) / (3 as i64);
        self.search_l();

        println!("{}", self.ans)
    }
}


fn main() {
    let mut app = App::input();
    app.solve();
}