#ifndef LOCAL
#pragma GCC optimize ("O3")
#endif

#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (c it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "

using ld = long double;
using ll = long long;

constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;

void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, int b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }

template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> A(n);
  for (ll& x : A) cin >> x;
  A.insert(A.begin(), 0);
  for (int i = 1; i <= n; i++) A[i] += A[i - 1];

  auto Suma = [&](int a, int b) -> ll {
    assert(1 <= a and a <= b and b <= n + 1);
    const ll res = A[b - 1] - A[a - 1];
    //debug() << "Suma(" imie(a) imie(b) ") = " << res;
    return res;
  };

  #ifdef LOCAL
  for (int a = 2; a <= n; a++) {
    for (int b = a + 1; b <= n; b++) {
      for (int c = b + 1; c <= n; c++) {
        vector<ll> wart = {Suma(1, a), Suma(a, b), Suma(b, c), Suma(c, n + 1)};
        sort(wart.begin(), wart.end());
        if (wart.back() - wart[0] == 36) {
          debug() << imie(a) imie(b) imie(c);
        }
      }
    }
  }
  #endif

  vector<vector<pair<ll, ll>>> pref(n + 1), suf(n + 1);
  int wsk = 2;
  for (int i = 3; i <= n; i++) {
    while (wsk + 1 < i and Suma(1, wsk + 1) <= Suma(wsk + 1, i)) {
      wsk++;
    }
    pref[i].emplace_back(Suma(1, wsk), Suma(wsk, i));
    if (wsk + 1 < i) {
      pref[i].emplace_back(Suma(1, wsk + 1), Suma(wsk + 1, i));
    }
    debug() << imie(i) imie(wsk) imie(pref[i]);
  }

  wsk = n;
  for (int i = n - 1; i >= 1; i--) {
    while (i < wsk - 1 and Suma(i, wsk - 1) >= Suma(wsk - 1, n + 1)) {
      wsk--;
    }
    suf[i].emplace_back(Suma(i, wsk), Suma(wsk, n + 1));
    if (i < wsk - 1) {
      suf[i].emplace_back(Suma(i, wsk - 1), Suma(wsk - 1, n + 1));
    }
    debug() << imie(i) imie(wsk) imie(suf[i]);
  }
  auto Oblicz = [&](int i) {
    ll wyn = numeric_limits<ll>::max();
    for (auto& a : pref[i]) {
      for (auto& b : suf[i]) {
        vector<ll> rzeczy(4);
        rzeczy[0] = (a.first);
        rzeczy[1] = (a.second);
        rzeczy[2] = (b.first);
        rzeczy[3] = (b.second);
        sort(rzeczy.begin(), rzeczy.end());
        Mini(wyn, rzeczy[3] - rzeczy[0]);
      }
    }
    return wyn;
  };
  ll wyn = numeric_limits<ll>::max();
  for (int i = 3; i <= n - 1; i++) {
    Mini(wyn, Oblicz(i));
  }
  cout << wyn << endl;
  return 0;
}


Measured memory_bytes: 31332