class Array
	def accum
		inject([0]){|a, x| a + [a[-1] + x]}[1..-1]
	end

	def bsearch(l = 0, r = size - 1, &pred)
		if r - l < 100
			return (l..r).find{|j| pred[self[j]]}
		end

		c = (l + r) / 2 
		if pred[self[c]]
			bsearch(l, c-1, &pred) || c
		else
			bsearch(c+1, r, &pred)
		end
	end
end

n = gets.to_i
a = $<.map(&:to_i)
accum = a.accum
total = accum[-1]

subset_sum = -> i, j {
	if i <= j
		accum[j] - (i > 0 ? accum[i-1] : 0)
	else
		accum[-1] - accum[i-1] + accum[j]
	end
}

best = [
	subset_sum[0, a.size/3-1],
	subset_sum[a.size/3, a.size/3*2-1],
	subset_sum[a.size/3*2, a.size-1]
	].min

i = 0
loop {
	j = (0..n-1).to_a.bsearch(i+1, n-1){|k| subset_sum[i, k-1] >= best}
	j = (0..n-1).to_a.bsearch(0, i-2){|k| subset_sum[i, (k-1)%n] >= best} unless j

	while subset_sum[i, (j-1)%n] + 2 * best <= total
		k = (0..n-1).to_a.bsearch(j+1, n-1){|l| subset_sum[j, l-1] >= best}
		k = (0..n-1).to_a.bsearch(0, i-1){|l| subset_sum[j, (l-1)%n] >= best} unless k

		while subset_sum[k, (i-1)%n] >= best
			#p [i, j, k]
			min = [
				subset_sum[i, (j-1)%n],
				subset_sum[j, (k-1)%n],
				subset_sum[k, (i-1)%n]
				].min
			best = min if min > best
			k = (k+1)%n
		end
		j = (j + 1) % n
	end

	i += 1
	break if i == a.size
}

p best