fun main(args:Array<String>):Unit {
    val parts = Array(readLine()!!.toInt()){
        readLine()!!.toInt()
    }
    val accumulated = Array(parts.size * 2 + 1){0L}
    for (i in 1 until accumulated.size){
        accumulated[i] = accumulated[i - 1] + parts[(i - 1) % parts.size]
    }
    val wholeSize = accumulated[parts.size]
    val maxSize = wholeSize / 3
    var maxMin:Long = 0L
    for (i in 0 until parts.size){
        val greaterPoint =
                accumulated.binarySearch(accumulated[i] + maxSize).let{ if (it >= 0) it else -it - 1}
        val firstArea =
                accumulated[greaterPoint] - accumulated[i]
        assert(firstArea >= maxSize)
        val secondPoint =
                accumulated.binarySearch(accumulated[greaterPoint] + (wholeSize - firstArea) / 2).let{if (it >= 0) it else -it - 1}
        val secondMin =
                accumulated[secondPoint - 1] - accumulated[greaterPoint]
        val thirdMax = wholeSize - firstArea - secondMin
        assert(secondMin < maxSize)
        val secondMax =
                accumulated[secondPoint] - accumulated[greaterPoint]
        val thirdMin = wholeSize - firstArea - secondMax
        assert(firstArea + secondMax <= wholeSize)
        val tempMaxMin =
                listOf(listOf(secondMin, thirdMax).min()!!, listOf(secondMax, thirdMin).min()!!).max()!!
        if (maxMin < tempMaxMin) {
            maxMin = tempMaxMin
        }
    }
    println(maxMin)
}
