a = []

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

max = -1
for i in (1..(a.size-1))
  for j in ((i+1)..(a.size-1))
    for k in ((j+1)..a.size)
      x = [(((k < a.size) ? a[k..(a.size-1)] : []) \
            + a[0..(i-1)]), a[i..(j-1)], a[j..(k-1)]].collect { |l|
        l.inject(:+)
      }.sort[0]

      if x > max then
        max = x
      end
    end
  end
end

puts max