#define DEBUG 0
#include <bits/stdc++.h>
#define loop(n) for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto&& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto&& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#if DEBUG
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) {}
#endif

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple)
  { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple)
 { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n; std::cin >> n;
  std::vector<lint> a(n); std::cin>>a;

  auto make_pairs = [&] (auto a) {
    debug(a);
    int n = a.size();
    std::partial_sum(all(a), a.begin());
    a.insert(a.begin(), 0);
    std::vector<std::pair<lint,lint>> ans(2);
    int l = 0;
    rep(r,2,n) {
      while (a.at(l+1) * 2 < a.at(r)) {
        l++;
      }
      lint full = a.at(r);
      ans.emplace_back();
      lint dif = full+1;
      for (lint x : {a.at(l), a.at(l+1)}) {
        lint y = full - x;
        lint d = x - y;
        if (d < 0) d = -d;
        if (cmn(dif, d)) {
          ans.back() = {x, y};
        }
      }
    }
    return ans;
  };
  auto left = make_pairs(a);
  std::reverse(all(a));
  auto right = make_pairs(a);
  debug(left); debug(right);

  lint ans = std::numeric_limits<lint>::max();
  rep(i,2,n-1) {
    auto&& p0 = left.at(i);
    auto&& p1 = right.at(n-i);
    lint a,b,c,d; std::tie(a,b) = p0; std::tie(c,d) = p1;
    std::vector<lint> v{a,b,c,d};
    lint now = *std::max_element(all(v)) - *std::min_element(all(v));
    debug(v);
    cmn(ans, now);
  }
  std::cout << ans << std::endl;
  return 0;
}

/*
大グループ 2 つの境界を全探索です。
小グループの境界は、半々を挟んだ 2 通りを試せばよいです。
そこで、a([0,l[) * 2 < a([0, r[) なる最大の l を尺取り法で求めて、
a([0,l[), ([0,l+1[) を入れておきます。
*/

Measured memory_bytes: 16964