#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<vector<int>> pre(n, vector<int>(2)), suf(n, vector<int>(2));
pre[1][0] = a[0], pre[1][1] = a[1];
int left = a[0], right = a[1];
int l = 0;
for (int i = 2; i < n; i++) {
right += a[i];
int cur = abs(right - left);
while (l + 1 < i && cur >= abs((right - a[l + 1] - (left + a[l + 1])))) {
l++;
right -= a[l];
left += a[l];
cur = abs(right - left);
}
pre[i][0] = left;
pre[i][1] = right;
}
suf[n - 2][0] = a[n - 2];
suf[n - 2][1] = a[n - 1];
left = a[n - 2], right = a[n - 1];
int r = n - 1;
for (int i = n - 3; i >= 2; i--) {
left += a[i];
int cur = abs(left - right);
while (r - 1 > i && cur >= abs(right + a[r - 1] - (left - a[r - 1]))) {
r--;
left -= a[r];
right += a[r];
cur = abs(left - right);
}
suf[i][0] = left;
suf[i][1] = right;
}
int ans = 1e18;
for (int i = 1; i < n - 2; i++) {
vector<int> cur;
cur.push_back(pre[i][0]);
cur.push_back(pre[i][1]);
cur.push_back(suf[i + 1][0]);
cur.push_back(suf[i + 1][1]);
sort(cur.begin(), cur.end());
ans = min(ans, abs(cur[0] - cur.back()));
}
cout << ans << '\n';
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBpbnQgbG9uZyBsb25nCnNpZ25lZCBtYWluKCkgewoJaW9zX2Jhc2UgOjogc3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKCWludCBuOyBjaW4gPj4gbjsKCXZlY3RvcjxpbnQ+IGEobik7Cglmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgY2luID4+IGFbaV07Cgl2ZWN0b3I8dmVjdG9yPGludD4+IHByZShuLCB2ZWN0b3I8aW50PigyKSksIHN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