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.map{ 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 + 1
while l + 1 < r
  m = (l + r) / 2
  i = 0
  while i < n
    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
      break if b < i + n && sum[i+n] - sum[b] >= m
    end
    i += 1
  end
  i == n ? r = m : l = m
end
p l