
"use strict";

process.stdin.resume();
process.stdin.setEncoding('utf8');

var g_input = '';
process.stdin.on('data', function(chunk) {
    g_input += chunk;
});

process.stdin.on('end', function() {
    Main(g_input.split(/\r?\n/));
});

function Main(input) {

    const n = parseInt(input[0], 10);
    const a = input[1].split(' ').map(function(value) { return parseInt(value, 10); });
    const sum = a.reduce(function(prev, current, index) {
        if (index === 0) {
            prev.push(current);
        }
        else {
            prev.push(current + prev[index - 1]);
        }
        return prev;
    }, []);

    let ans = Number.MAX_SAFE_INTEGER;
    let left = 0;
    let right = 2;
    for (let center = 1; center < n - 2; center++) {
        
        while (left < center - 1 && Math.abs(sum[left] - (sum[center] - sum[left])) > Math.abs(sum[left + 1] - (sum[center] - sum[left + 1]))) {
            left++;
        }

        if (right <= center) {
            right = center + 1;
        }

        while (right < n - 1 && Math.abs((sum[right] - sum[center]) - (sum[n - 1] - sum[right])) > Math.abs((sum[right + 1] - sum[center]) - (sum[n - 1] - sum[right + 1]))) {
            right++;
        }

        const p = sum[left];
        const q = sum[center] - sum[left];
        const r = sum[right] - sum[center];
        const s = sum[n - 1] - sum[right];
        const max = Math.max(p, q, r, s);
        const min = Math.min(p, q, r, s);

        ans = Math.min(ans, max - min);
    }

    console.log(ans);

    return 0;
}
