a = gets.split.map(&:to_i) b = [] a[0].times do b << gets.split.map(&:to_i) end memo = [b.first] a[0].times do |n| memo.last.uniq! tmp = [] a[1].times do |m| break unless b[n+1] memo.last.length.times do |l| sum = memo[n][l] + b[n+1][m] sum <= a[2] ? tmp << sum : next end end memo << tmp unless tmp.empty? end # p memo p memo.count == 1 ? -1 : a[2] - memo.last.max