/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-02-05 04:48:50
**/

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

using namespace std;

using int64 = long long;

template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) { os << '{'; size_t n = vec.size(); for (size_t i = 0; i < n; ++i) { os << vec[i]; if (i != n - 1) os << ','; } os << '}'; return os; }
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, class U> std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) { os << '{'; for (auto it = mp.begin(); it != mp.end(); ++it) { os << '{' << it->first << ':' << it->second << '}'; if (it != --mp.end()) os << ','; } os << '}'; return os; }
template<class T> std::ostream& operator<<(std::ostream& os, const std::set<T>& st) { os << '{'; for (auto it = st.begin(); it != st.end(); ++it) { os << *it; if (it != --st.end()) os << ','; } os << '}'; return os; }
template<class T> std::istream& operator>>(std::istream& is, std::vector<T>& vec) { size_t n = vec.size(); for (size_t i = 0; i < n; ++i) is >> vec[i]; return is; }

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

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...) 1728
#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)...);
}

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

  // return sum of [left, right]
  T Get(int left, int right) {
    return sum_[right + 1] - sum_[left];    
  }

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

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

constexpr int64 INF = 2e18;

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);
  vector<int64> sum(n + 1, 0LL);
  for (int i = 0; i < n; ++i) {
    sum[i + 1] = sum[i] + a[i];
  }

  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);
    //debug(s1, s2);
    int p1 = distance(sum.begin(), upper_bound(all(sum), s1 / 2)) - 1;
    int p2 = distance(sum.begin(), upper_bound(all(sum), s1 + s2 / 2)) - 1;
    //debug(p1, p2);
    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: 6528