#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;
  debug(a);

  auto make_pairs = [&] (auto 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>>> pairs(n);
    rep(c,2,n-1) {
      lint now = a.at(c);
      auto lb = std::lower_bound(all(a), now / 2);
      for (auto it : {std::prev(lb), lb}) {
        pairs.at(c).emplace_back(*it, now - *it);
      }
    }
    debug(pairs);
    return pairs;
  };
  auto left = make_pairs(a);
  std::reverse(all(a));
  auto right = make_pairs(a);
  debug(left,right);

  lint ans = std::numeric_limits<lint>::max();
  rep(c,2,n-1) {
    auto&& v0 = left.at(c);
    auto&& v1 = right.at(n-c);
    debug(v0,v1);
    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(p0,p1,now);
      cmn(ans,now);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

/*
大グループの境界を全探索です。(2<=i<=n-2)
それぞれで、小グループの境界は 2 通りまで絞れます。
累積和をしておいて、lb(sum[0,i[ / 2) が右側、それの 1 個前が左側です。
*/

Measured memory_bytes: 33096