#pragma region kyomukyomupurin

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-05-08 10:45:07
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
  int flag = 0;
  for (auto e : vec) os << (flag++ ? ", " : "{") << e;
  return os << (vec.empty() ? "{}" : "}");
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
  int flag = 0;
  for (auto e : st) os << (flag++ ? ", " : "{") << e;
  return os << (st.empty() ? "{}" : "}");
}

template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) {
  int flag = 0;
  for (auto e : mp) os << (flag++ ? ", " : "{") << e;
  return os << (mp.empty() ? "{}" : "}");
}

template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
  for (T& e : vec) is >> e;
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\n'; }

template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0) std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

#pragma endregion kyomukyomupurin

template <class _Tp>
class CumulativeSum {
 public:
  CumulativeSum(const std::vector<_Tp>& vec) : vec_(vec) { build(); }

  // return sum of [l, r]
  _Tp get(int l, int r) { return sum_[r + 1] - sum_[l]; }

  // return the first k where sum of [0, k] >= value
  int lower_bound(_Tp value) {
    //assert(all_of(vec_.begin(), vec_.end(), [](_Tp e) { return e >= 0; }));
    int high = vec_.size(), low = -1;
    while (high - low > 1) {
      int mid = (high + low) >> 1;
      if (this->get(0, mid) < value) {
        low = mid;
      } else {
        high = mid;
      }
    }
    return high;
  }

  // return the first k where sum of [0, k] > value
  int upper_bound(_Tp value) {
    //assert(all_of(vec_.begin(), vec_.end(), [](_Tp e) { return e >= (_Tp)0; }));
    int high = vec_.size(), low = -1;
    while (high - low > 1) {
      int mid = (high + low) >> 1;
      if (this->get(0, mid) <= value) {
        low = mid;
      } else {
        high = mid;
      }
    }
    return high;
  }

 private:
  std::vector<_Tp> vec_;
  std::vector<_Tp> sum_;

  void build() {
    sum_.assign(vec_.size() + 1, static_cast<_Tp>(0));
    for (size_t i = 0; i < vec_.size(); ++i) {
      sum_[i + 1] = sum_[i] + vec_[i];
    }
  }
};

constexpr int64 inf = 1e18;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  vector<int64> a(n); cin >> a;

  CumulativeSum<int64> cs(a);

  int64 ans = inf;

  for (int i = 0; i < n - 3; ++i) {
    int64 s1 = cs.get(0, i + 1);
    int64 s2 = cs.get(i + 2, n - 1);

    int p1 = cs.upper_bound(s1 / 2);
    int p2 = cs.upper_bound(s1 + s2 / 2);

    int64 t1 = cs.get(0, p1 - 1), t2 = cs.get(p1, i + 1);
    int64 t3 = cs.get(0, p1), t4 = cs.get(p1 + 1, i + 1);
    int64 ans1, ans2, ans3, ans4;
    if (abs(t1 - t2) < abs(t3 - t4)) {
      ans1 = t1, ans2 = t2;
    } else {
      ans1 = t3, ans2 = t4;
    }
    t1 = cs.get(i + 2, p2 - 1), t2 = cs.get(p2, n - 1);
    t3 = cs.get(i + 2, p2), t4 = cs.get(p2 + 1, n - 1);
    if (abs(t1 - t2) < abs(t3 - t4)) {
      ans3 = t1, ans4 = t2;
    } else {
      ans3 = t3, ans4 = t4;
    }
    ans = min(ans, max({ans1, ans2, ans3, ans4}) - min({ans1, ans2, ans3, ans4}));
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 4992