def lower_bound(arr, v)
  l = -1; r = arr.size
  while l + 1 < r
    m = (l + r) / 2
    if arr[m] <= v then l = m
    else r = m
    end
  end
  l
end
n = gets.to_i
arr = []
n.times{ arr << gets.to_i }
sum = Array.new(2*n+1, 0)
(2*n).times{ |i| sum[i+1] += sum[i] + arr[i%n] }
l = 0; r = sum[n] / 3
while l + 1 < r
  m = (l + r) / 2
  f = nil
  n.times do |i|
    a = lower_bound(sum[i,n], m + sum[i] - 1) + i + 1
    if a < i+n
      b = lower_bound(sum[a...i+n], m + sum[a] - 1) + a + 1
      if b < i+n && sum[i+n] - sum[b] >= m
        l = m
        f = true
        break
      end
    end
  end
  r = m unless f 
end
p l