n = gets.to_i
f = []#Array.new()
n.times do
  f << gets.split(" ").map(&:to_i)
end
p_i = []#Array.new()
n.times do
  p_i << gets.split(" ").map(&:to_i)
end

ans = 0
ans2 = []
idxs = []
n.times do |i|
  f_i_max = f[i].inject(:+)
  focus = p_i[i][0..f_i_max]
  idx = focus.index(focus.max)
  ans += focus.sort.reverse[0]
  ans2 << focus.sort.reverse[1]

  idxs << idx
end

if idxs.all?{|i| i==0}
  ans = ans2.min
end

puts ans