a = []

num = gets.to_i
num.times {
  a.push(gets.to_i)
}


size = a.size # calc size
sum = a.inject(:+) # calc max

max = -1
for i in (1..(size-1))
  s0 = a[0..(i-1)].inject(:+)
  for j in ((i+1)..(size-1))
    s1 = a[i..(j-1)].inject(:+)
    for k in ((j+1)..size)
      s2 = ((k < size) ? a[k..(size-1)] : [0]).inject(:+) + s0
      x = [s1, s2, (sum - s1 - s2)].sort[0]
      if x > max then
        max = x
      end
    end
  end
end

puts max