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 Scala (2.11.7) (judge verdict: Accepted)

import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val A = new Array[Long](N+1)
  for(i <- 1 to N)
    A(i) = in.next().toLong


  //
  def check(mid: Int, base: Int): Long = {
    if(mid < base) {
      val l = imos(mid-1)
      val r = imos(base-1) - l
      (r-l).abs
    }
    else {
      val l = imos(mid) - imos(base-1)
      val r = imos(N) - imos(mid)
      (r-l).abs
    }
  }



  // 下に凸のグラフの最小値
  def ternarySearchInt(small: Int, big: Int, i: Int): Int = {
    if(big - small <= 2) {
      val mid = (big + small) / 2
      if(check(big, i) <= check(mid, i)) big
      else if(check(small, i) <= check(mid, i)) small
      else mid
    }
    else{
      val midl = (big + small) / 2
      val midr = midl + 1
      if (check(midl, i) >= check(midr, i))
        ternarySearchInt(midl, big, i)
      else
        ternarySearchInt(small, midr, i)
    }
  }


  val imos = new Array[Long](N+1)
  for(i <- 1 to N) {
    imos(i) = imos(i-1) + A(i)
  }

  var ans = Long.MaxValue
  for(i <- 3 to N-1) { // iが右ブロックの最初
    val l = ternarySearchInt(1, i-1, i)
    val r = ternarySearchInt(i, N, i)
    val P = imos(l-1)
    val Q = imos(i-1) - P
    val R = imos(r) - imos(i-1)
    val S = imos(N) - imos(r)
    val hoge = List(P,Q,R,S)

    ans = Math.min(ans, hoge.max - hoge.min)
  }

  println(ans)
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}