### subroutines

def lower_bound(arr, key, i0 = 0, i1 = arr.length)
  lb = i0 - 1
  ub = i1

  while lb + 1 < ub
    m = (lb + ub) / 2
    if arr[m] <= key
      lb = m
    else
      ub = m
    end
  end
  lb
end

def div_three?(k)
  #puts "k=#{k}"
  for i in (0...$n)
    imax = i + $n
    #puts " i=#{i}, imax=#{imax}"
    i0 = lower_bound($asums, k + $asums[i] - 1, i, imax) + 1
    #puts "  i0=#{i0}"
    if i0 < imax
      i1 = lower_bound($asums, k + $asums[i0] - 1, i0, imax) + 1
      #puts "  i1=#{i1}"
      if i1 < imax &&
          $asums[imax] - $asums[i1] >= k
        return true
      end
    end
  end

  false
end

### main

$n = gets.strip.to_i

$ais = $n.times.map{gets.strip.to_i}
$ais += $ais
#p $ais

$asums = [0]
(1..(2 * $n)).each{|i| $asums[i] = $asums[i - 1] + $ais[i - 1]}
#p $asums

k0 = 0
k1 = $asums[$n] / 3

while k0 + 1 < k1
  km = (k0 + k1) / 2
  if div_three?(km)
    k0 = km
  else
    k1 = km
  end
end

puts k0