N=gets.to_i I = $<.read.split("\n").map{|m|m.split(" ").map{|n|n.to_i}} m=Array.new(N+1).map{Array.new(0)} m[0].push([0,0]) for x in 1..N do m[x-1].each do |p| pa = p[0] pb = p[1] ca = I[x-1][0] cb = I[x-1][1] #puts "pa:#{pa}\tpb:#{pb}\tca:#{ca}\tcb:#{cb}" m[x].push [pa+ca, pb] m[x].push [pa, pb+cb] end m[x].sort_by!{|a|[a[0],a[1]].max} m[x] = m[x].slice(0..10) end puts [m[N][0][0], m[N][0][1]].max