n, m, k = gets.split.map &:to_i a = n.times.map{ gets.split.map &:to_i } if k < a.sum { |v| v.min } puts -1 exit end dp = [false] * (k + 1) dp[0] = true a.each do |v| dp2 = [false] * (k + 1) (k + 1).times do |i| next if !dp[i] v.each do |j| dp2[i + j] = true if i + j < dp.size end end dp = dp2 end (k).downto(0) do |i| if dp[i] puts k - i break end end