#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <cstdint>
#include <cmath>
#include <bitset>
#include <string>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>

#define N_TIMES(i, n)     for ( uint64_t i = 0;     i <  n; ++i )
#define N_TIMES_REV(i, n) for (  int64_t i = n - 1; i >= 0; --i )
#define SQRTI(n) static_cast<uint64_t>(::floor(::sqrt(static_cast<double>(n))))

#ifdef __DEBUG__
template<typename T, typename U>
std::ostream& operator<<(std::ostream &os, const std::pair<T, U> &pair)
{
  os << pair.first << ": " << pair.second;
  return os;
}

template<class ForwardIterator>
std::ostream& _iterator_to_display(
  std::ostream &os, 
  ForwardIterator begin, ForwardIterator end, 
  const std::string &delim = ", "
)
{
  auto itr = begin;
  while (itr != end) {
    os << *itr;
    ++itr;
    if (itr != end) os << delim;
  }
  return os;
}

template<typename T>
std::ostream& operator<<(std::ostream &os, const std::list<T> &list)
{
  _iterator_to_display(os, list.begin(), list.end());
  return os;
}

template<typename T>
std::ostream& operator<<(std::ostream &os, const std::vector<T> &vector)
{
  _iterator_to_display(os, vector.begin(), vector.end());
  return os;
}

template<typename T, class C=std::less<T>>
std::ostream& operator<<(std::ostream &os, const std::set<T> &set)
{
  _iterator_to_display(os, set.begin(), set.end());
  return os;
}

template<typename T, typename U, class C=std::less<T>>
std::ostream& operator<<(std::ostream &os, const std::map<T, U> &map)
{
  _iterator_to_display(os, map.begin(), map.end());
  return os;
}
#endif /* __DEBUG__ */

int main()
{
  uint64_t N;
  std::cin >> N;
  
  std::vector<int64_t> A(N);
  N_TIMES(n, N) { std::cin >> A[n]; }
  
  std::vector<int64_t> T(N + 1, 0);
  N_TIMES(n, N) { T[n + 1] = T[n] + A[n]; }
  
  std::vector<uint64_t> F(N - 1, 1), G(N - 1, 3);
  
  for (uint64_t i = 2; i <= N - 2; ++i) {
#ifdef __DEBUG__
    std::cerr 
      << '{' << std::vector<uint64_t>(A.begin(), A.begin() + i) << '}'
      << ", " 
      << '{' << std::vector<uint64_t>(A.begin() + i, A.end())   << '}'
      << std::endl;
#endif /* __DEBUG__ */
  
    uint64_t j = F[i - 1];
    int64_t left_min_diff = std::abs((T[j] - T[0]) - (T[i] - T[j]));
    while (j < i) {
      int64_t diff = std::abs((T[j + 1] - T[0]) - (T[i] - T[j + 1]));
      
      if (left_min_diff < diff) {
#ifdef __DEBUG__
        std::cerr 
          << "  "
          << '{' << std::vector<uint64_t>(A.begin(), A.begin() + j) << '}'
          << ", " 
          << '{' << std::vector<uint64_t>(A.begin() + j, A.begin() + i)   << '}'
          << std::endl;
#endif /* __DEBUG__ */
        break;
      }
      
      left_min_diff = diff;
      ++j;
    }
    F[i] = j;
    
    uint64_t k = G[i - 1];
    int64_t right_min_diff = std::abs((T[k] - T[i]) - (T[N] - T[k]));
    while (k < N) {
      int64_t diff = std::abs((T[k + 1] - T[i]) - (T[N] - T[k + 1]));
      
      if (right_min_diff < diff) {
#ifdef __DEBUG__
        std::cerr 
          << "  "
          << '{' << std::vector<uint64_t>(A.begin() + i, A.begin() + k) << '}'
          << ", " 
          << '{' << std::vector<uint64_t>(A.begin() + k, A.end())   << '}'
          << std::endl;
#endif /* __DEBUG__ */
        break;
      }
      
      right_min_diff = diff;
      ++k;
    }
    G[i] = k;
  }
  
#ifdef __DEBUG__
  std::cerr << F << std::endl;
  std::cerr << G << std::endl;
#endif /* __DEBUG__ */
  
  uint64_t min = UINT64_MAX;
  for (uint64_t i = 2; i <= N - 2; ++i) {
    uint64_t P = T[F[i]] - T[0], Q = T[i] - T[F[i]];
    uint64_t R = T[G[i]] - T[i], S = T[N] - T[G[i]];
    uint64_t M = std::max(std::max(P, Q), std::max(R, S));
    uint64_t m = std::min(std::min(P, Q), std::min(R, S));
    min = std::min(min, M - m);
  }
  std::cout << min << std::endl;
  
  return 0;
}

Measured memory_bytes: 6528