#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

bool Debug;

const int mod = 1e9+7;
const int MAXN = 1e5+7;

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define iter iterator
#define sp3r(a) ((a)*(a))
#define abs(a) ((a)>0?(a):(-(a)))
#define max(a, b) ((a)>(b)?(a):(b))
#define min(a, b) ((a)<(b)?(a):(b))
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
#define lop(i,a,b) for(int i = (a); i <= (b); ++i)
#define dlop(i,a,b) for(int i = (a); i >= (b); --i)
#define debu(...) if (Debug) fprintf(stderr, __VA_ARGS__)
#define debug(x) if (Debug) cerr << "" #x " = " << x << endl
#define ergo(a) for(auto it = a.begin(); it != a.end(); ++it)

template<typename T> T gcd(T a, T b) {while(T t = a % b) a = b, b = t; return b;}
template<typename T> T Pow(T a, T b, T mod) {T ret = 1; for( ; b; b >>= 1) {if (b & 1) ret = ret * 1ull * a % mod; a = a * 1ull * a % mod;} return ret%mod;}
template<typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template<typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template<typename T> void amod(T &x, T y) {x += y; if (x >= mod) x -= mod;}
template<typename T> void smod(T &x, T y) {x -= y; if (x < 0) x += mod;}

typedef long long ll; 
typedef long double ld;
typedef unsigned int uint;
typedef pair<int ,int> pii;
typedef unsigned long long ull;

struct IO {
  static const int S = 5 << 20;int c, f;
  char wbuf[S], *p2, buf[S], *p1, *end;
  IO() {p1 = end = buf, p2 = wbuf;}
  int getc() {
    if(p1 == end)
      end = (p1 = buf) + fread(buf, 1, S, stdin);
    return p1==end?-1:*p1++;
  }
  template<typename T>void read(T &x) {
    f = 1, c = getc(), x = 0;
    while(c <= 32) (c=='-')&&(f = -1), c = getc();
    while(isdigit(c)) x = x * 10 + c - '0', c = getc();
    x *= f;
  }
  void get(char *s) {
    c = getc();
    while(c <= 32) c = getc();
    while(c > 32) *s++ = c, c = getc();
    *s = 0;
  }
  void put(int x) {
    if(p2-wbuf == S) 
      fwrite(wbuf, 1, S, stdout), p2 = wbuf;
    *p2++ = x;
  }
  template<typename T> void out(T x, int y) {
    if(x < 0) put('-'), x = -x;
    static int sta[24]; int n = 0;
    while(x || !n) sta[++n] = x % 10 + '0', x /= 10;
    while(n) put(sta[n--]); put(y);
  }
  void print(char *s, int y) {while(*s) put(*s++); put(y);}
  ~IO() {fwrite(wbuf, 1, p2-wbuf, stdout), p2 = wbuf;}
}io;

#define put io.put
#define get io.get
#define out io.out
#define print io.print
#define read(a) (io.read(a))
#define read2(a, b) (read(a), read(b))
#define read3(a, b, c) (read(a), read(b), read(c))
int n, a[200005], p1, p2, p3, p[4];
ll ans = 0xfffffffffffffff;
ll a1, b1, c1, d1, pre[200005];
#define Get(l, r) (pre[r] - pre[l-1])
inline ll Get1() {return (Get(1, p1-1));}
inline ll Get2() {return (Get(p1, p2-1));}
inline ll Get3() {return (Get(p2, p3-1));}
inline ll Get4() {return (Get(p3, n));}

int main() {
  #ifdef LOCAL_DEBUG
    Debug = 1;
    freopen("data.in", "r", stdin);
  #endif  
  read(n);
  lop(i,1,n) read(a[i]), pre[i] = pre[i-1] + a[i];
  p1 = 1; //p1左边是第一段 p1是第二块的第一个
  p3 = 3; //p3左边是第二段 p3是第4块的第一个
  for(p2 = 3; p2 <= n; ++p2) { //p2 第三块的第一个 p2-1;
    debu("%d %d %d\n", p1, p2, p3);
    while (p1 <= p2-3 && abs(pre[p1]-(pre[p2-1]-pre[p1])) >= abs(pre[p1+1]-(pre[p2-1]-pre[p1+1]))) ++p1;
    while (p3 <= n-2 && abs(pre[p3]-pre[p2-1]-(pre[n]-pre[p3])) >= abs(pre[p3+1]-pre[p2-1]-(pre[n]-pre[p3+1]))) ++p3;
    ll tmp = max(pre[p1], max(pre[p2-1]-pre[p1], max(pre[p3] - pre[p2-1], pre[n] - pre[p3])));
    ll tmp1 = min(pre[p1], min(pre[p2-1]-pre[p1], min(pre[p3] - pre[p2-1], pre[n] - pre[p3])));
    ans = min(ans, tmp - tmp1);
  }
  cout << ans;
  return 0;
}


Measured memory_bytes: 8448