def main n = gets.to_i - 1 c = gets.to_i v = gets.to_i s = gets.split.map { |val| val.to_i - 1 }.freeze t = gets.split.map { |val| val.to_i - 1 }.freeze y = gets.split.map(&:to_i).freeze m = gets.split.map(&:to_i).freeze dp = Array.new(n + 1) { Array.new(c + 1, Float::INFINITY) } dp[0][0] = 0 [*0..(n + 1)].product([*0..(c + 1)]) do |cur, total| v.times do |i| next if cur != s[i] || c < total + y[i] dp[t[i]][total + y[i]] = [dp[t[i]][total + y[i]], dp[cur][total] + m[i]].min end end dp[n].min.infinite? ? -1 : dp[n].min end puts main