a = gets.split.map(&:to_i) b = [] a[0].times do b << gets.split.map(&:to_i) end init_memo = b.first memo = [init_memo] 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 end memo.each {|n| memo.delete(n) if n.empty?} p memo.count == a[0] ? a[2] - memo.last.max : -1