#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::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();
      for (lint x : {a.at(l), a.at(l+1)}) {
        if (x < 0 || full < x) continue;
        ans.back().emplace_back(x, full - x);
      }
    }
    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(c,2,n-1) {
    auto&& v0 = left.at(c);
    auto&& v1 = right.at(n-c);
    for (auto&& p0 : v0) for (auto&& p1 : v1) {
      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: 34496