#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define sz(a) (a).size()
#define pb(a) push_back(a)
#define eb(...) emplace_back(__VA_ARGS__)
#define mp(a, b) make_pair(a, b)
#define mt(...) make_tuple(__VA_ARGS__)
using namespace std;
using pii = pair<int, int>;
constexpr int MOD = 1000000007;
constexpr int INF = 1LL << 30;
constexpr double EPS = 1e-10;

#define DUMPOUT cerr

// #define DEBUG_
// #define DETAIL_

#ifdef DEBUG_
#ifdef DETAIL_
#define dump(...) DUMPOUT << #__VA_ARGS__ << " : [" << __LINE__ << ":" << __FUNCTION__ << "]  =>  "; dump_func(__VA_ARGS__)
#else
#define dump(...) dump_func(__VA_ARGS__)
#endif
#else
#define dump(...) 
#endif

template <typename T> ostream& operator << (ostream& os, vector<T>& v) { os << "{"; rep(i, 0, sz(v)) os << v[i] << (i+1 == sz(v) ? "" : ", "); os << "}"; return os; }
template <typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T, typename U> ostream& operator << (ostream& os, map<T, U>& m) { os << "{"; for (auto v : m) { if (v != *m.begin()) os << ", "; os << "(" << v.first << ", " << v.second << ")"; } os << "}"; return os; }
template <typename T> ostream& operator << (ostream& os, set<T>& s) { os << "{"; for (auto v : s) { if (v != *s.begin()) os << ", "; os << v; } os << "}"; return os; }
template <typename T> ostream& operator << (ostream& os, pair<T*, T*> p) { os << "{"; for (auto i = p.first; i != p.second; i++) { if (i != p.first) os << ", "; os << *i; } os << "}"; return os; }
template <typename T, typename U> ostream& operator << (ostream& os, tuple<T*, T*, U> t) { os << "{"; for (auto i = get<0>(t); i != get<1>(t); i++) { if ((i-get<0>(t))%get<2>(t)) os << ", "; else if (i != get<0>(t)) os << "\n "; os << *i; } os << "}"; return os; }
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head&& head, Tail&&... tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << "  ||  "; dump_func(move(tail)...); }


int N;
int A[200010], B[200010];
int L[200010], R[200010];
int cum[200010], cumr[200010];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  cin >> N;
  rep(i, 0, N) {
    cin >> A[i];
    B[N-1-i] = A[i];
  }

  rep(i, 0, N) {
    cum[i] = A[i];
    if (i) cum[i] += cum[i-1];
  }
  fill(L, L+N, -1);
  L[1] = 0;
  rep(i, 2, N) {
    int p = L[i-1];
    int cumL = cum[p];
    int cumR = cum[i] - cum[p];
    while (p+1 < i) {
      int diff = abs(cumL - cumR + 2*A[p+1]);
      if (diff >= abs(cumL - cumR)) break;
      cumL += A[p+1];
      cumR -= A[p+1];
      p++;
    }
    L[i] = p;
  }

  rep(i, 0, N) {
    cumr[i] = B[i];
    if (i) cumr[i] += cumr[i-1];
  }
  fill(R, R+N, -1);
  R[1] = 0;
  rep(i, 2, N) {
    int p = R[i-1];
    int cumL = cumr[p];
    int cumR = cumr[i] - cumr[p];
    while (p+1 < i) {
      int diff = abs(cumL - cumR + 2*B[p+1]);
      if (diff >= abs(cumL - cumR)) break;
      cumL += B[p+1];
      cumR -= B[p+1];
      p++;
    }
    R[i] = p;
  }
  reverse(R, R+N);
  rep(i, 0, N) R[i] = N-2-R[i];

  int ans = INF;
  rep(i, 1, N-2) {
    int LL = cum[L[i]];
    int LR = cum[i] - cum[L[i]];
    int RL = cum[R[i+1]] - cum[i];
    int RR = cum[N-1] - cum[R[i+1]];
    dump(LL, LR, RL, RR);
    ans = min(ans, max({LL, LR, RL, RR}) - min({LL, LR, RL, RR}));
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 9600