Problem:
Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.
The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints

4 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.
Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.
We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#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() + 1, 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() + 1, 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;
}