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 Ruby (2.3.3) (judge verdict: Runtime Error)

def main
  n = gets.chomp.to_i
  list = gets.chomp.split(' ').map(&:to_i)
  solve(list)
end

def dev(list)
  n = list.size
  min_i = -1
  min_d = pre_d = 99999999999
  min_v1 = min_v2 = -1

  i = (list.size / 2).to_i
  p = 0
  loop do
    v1 = list[0...i].inject(:+)
    v2 = list[i...n].inject(:+)
    d = (v1 - v2).abs
    # puts "#{i} #{v1} #{v2} #{d}"
    if min_d > d
      min_d = d
      min_i = i
      min_v1 = v1
      min_v2 = v2
    end
    next_i = if v1 > v2
               i - ((i - p) / 2).to_i
             else
               i + ((n - i) / 2).to_i
             end
    if v1 < v2
      p = i
    end
    if i == next_i
      break
    end
    i = next_i
  end
  # (1...n).to_a.each do |i|
  #   v1 = list[0...i].inject(:+)
  #   v2 = list[i...n].inject(:+)
  #   d = (v1 - v2).abs
  #   # puts "#{i} #{v1} #{v2} #{d}"
  #   if pre_d <= d
  #     break
  #   end
  #   if min_d > d
  #     min_d = d
  #     min_i = i
  #     min_v1 = v1
  #     min_v2 = v2
  #   end
  #   pre_d = d
  # end
  [list[0...min_i], list[min_i...n], min_v1, min_v2]
end

def solve(list)
  l1, l2 = dev(list)
  c1, c2 = dev(l1)
  c3, c4 = dev(l2)
  # puts "#{c1}"
  # puts "#{c2}"
  # puts "#{c3}"
  # puts "#{c4}"
  v1 = c1.inject(:+)
  v2 = c2.inject(:+)
  v3 = c3.inject(:+)
  v4 = c4.inject(:+)
  l = [v1,v2,v3,v4]
  # puts "#{l}"
  # puts (l.min - l.max).abs
  a1 = (l.min - l.max).abs

  p1 = c1
  p2 = c2.size > 1 ? c2[0...c2.size-1] : c2
  p3 = c2.size > 1 ? c3 + [c2[c2.size-1]] : c3
  p4 = c4
  l2 = [p1.inject(:+),p2.inject(:+),p3.inject(:+),p4.inject(:+)]
  a2 = (l2.min - l2.max).abs

  q1 = c1
  q2 = c3.size > 1 ? c2 + [c3[0]] : c2
  q3 = c3.size > 1 ? c3[1..c3.size] : c3
  q4 = c4
  l3 = [q1.inject(:+),q2.inject(:+),q3.inject(:+),q4.inject(:+)]
  a3 = (l3.min - l3.max).abs

  puts [a1,a2,a3].min
end

main if $0 == __FILE__