#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
vector<long long> pref(n + 1);
for (int i = 0; i < n; i++)
{
pref[i + 1] = pref[i] + a[i];
}
auto sum = [&pref](int l, int r) // [l, r)
{
return pref[r] - pref[l];
};
int i = 1;
long long ans = pref[n];
for (int j = 2; j < n; j++)
{
while (i + 1 < j && abs(sum(0, i + 1) - sum(i + 1, j)) <= abs(sum(0, i) - sum(i, j)))
i++;
long long x = sum(0, i);
long long y = sum(i, j);
long long z = sum(j, n);
ans = min(ans, max({x, y, z}) - min({x, y, z}));
}
cout << ans << "\n";
return 0;
}