"use strict";
    function Main(input) {
        let cin = new Inputer(input);
        let N = cin.getInt();
        let S = new Array(N + 1);
        S[0] = 0;
        for (let i = 1; i <= N; i++) {
            S[i] = S[i - 1] + cin.getInt();
        }
        let ans = Number.MAX_SAFE_INTEGER;
        let l = 1, r = 3;
        for (let i = 2; i <= N - 2; i++) {
            while (Math.abs(S[l] - (S[i] - S[l])) > Math.abs(S[l + 1] - (S[i] - S[l + 1]))) l++;
            while (Math.abs(S[N] - S[r] - S[r] + S[i]) > Math.abs(S[N] - S[r + 1] - S[r + 1] + S[i])) r++;
            let max = Math.max(...[S[l], S[i] - S[l], S[r] - S[i], S[N] - S[r]]);
            let min = Math.min(...[S[l], S[i] - S[l], S[r] - S[i], S[N] - S[r]]);
            ans = Math.min(ans, Math.abs(max - min));
        }
        console.log(ans);
    }
class Inputer{constructor(t){if(typeof t=='undefined')throw new Error('引数（stdin）を省略できません！');this.cnt=0;this.items=t.split(/\n|\s/)}getInt(){return this.items[this.cnt++]-0}getIntArray(t){if(typeof t=='undefined')throw new Error('引数（1以上）を省略できません！');if(t<1)throw new Error('引数は1以上であること！');let r=new Array(t);for(let e=0;e<t;e++)r[e]=this.getInt();return r}getStr(){return this.items[this.cnt++]}getStrArray(t){if(typeof t=='undefined')throw new Error('引数（1以上）を省略できません！');if(t<1)throw new Error('引数は1以上であること！');let r=new Array(t);for(let e=0;e<t;e++)r[e]=this.getStr();return r}}
if (typeof window == "undefined") Main(require("fs").readFileSync("/dev/stdin", "utf8"));