#include<bits/stdc++.h>
using namespace std;
vector< pair<int,int> > helper(vector<int> &a, int n) {
vector< pair<int,int> > ans(n);
ans[1] = {a[0], a[1]};
int fE = 0; //firstEnding -> the index at which the first part is ending
for(int i=2; i<n; i++) {
ans[i] = ans[i-1];
//include the current element in the right part
ans[i].second += a[i];
int cur = abs(ans[i].first - ans[i].second);
//expand the first part if it is profitable
while(fE+1 < i && abs(ans[i].first+a[fE+1]-(ans[i].second-a[fE+1])) < cur) {
fE++;
ans[i].first += a[fE];
ans[i].second -= a[fE];
cur = abs(ans[i].first - ans[i].second);
}
}
return ans;
}
int main() {
int n;
cin>>n;
vector<int> a(n);
for(int &x : a) cin>>x;
vector< pair<int,int> > pref, suf;
pref = helper(a, n);
reverse(a.begin(), a.end());
suf = helper(a, n);
reverse(suf.begin(), suf.end());
int ans = INT_MAX;
for(int i=1; i<n-2; i++) {
vector<int> temp{pref[i].first, pref[i].second, suf[i+1].first, suf[i+1].second};
sort(temp.begin(), temp.end());
ans = min(ans, abs(temp.front() - temp.back()));
}
cout<<ans<<"\n";
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZlY3RvcjwgcGFpcjxpbnQsaW50PiA+IGhlbHBlcih2ZWN0b3I8aW50PiAmYSwgaW50IG4pIHsKICAgIHZlY3RvcjwgcGFpcjxpbnQsaW50PiA+IGFucyhuKTsKICAgIGFuc1sxXSA9IHthWzBdLCBhWzFdfTsKICAgIGludCBmRSA9IDA7IC8vZmlyc3RFbmRpbmcgLT4gdGhlIGluZGV4IGF0IHdoaWNoIHRoZSBmaXJzdCBwYXJ0IGlzIGVuZGluZwogICAgCiAgICBmb3IoaW50IGk9MjsgaTxuOyBpKyspIHsKICAgICAgICBhbnNbaV0gPSBhbnNbaS0xXTsKICAgICAgICAvL2luY2x1ZGUgdGhlIGN1cnJlbnQgZWxlbWVudCBpbiB0aGUgcmlnaHQgcGFydAogICAgICAgIGFuc1tpXS5zZW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