#include #include #include #include bool less(std::pair lhs, std::pair rhs) { if(lhs.first == rhs.first) { return lhs.second < rhs.second; } return lhs.first < rhs.first; } std::pair plus(std::pair lhs, std::pair rhs) { return std::make_pair(lhs.first + rhs.first, lhs.second + rhs.second); } static int const INF{100000000}; int main(int, char**) { int N, C, V; std::cin >> N >> C >> V; std::vector ss(N), ts(N), ys(N),ms(N); for(int i{0}; i < N; ++i) { std::cin >> ss[i]; } for(int i{0}; i < N; ++i) { std::cin >> ts[i]; } for(int i{0}; i < N; ++i) { std::cin >> ys[i]; } for(int i{0}; i < N; ++i) { std::cin >> ms[i]; } std::vector> vs(N); for(int i{0}; i < N; ++i) { vs[i] = std::make_pair(ms[i], ys[i]); } std::vector>> ns(N, std::vector>(1, std::make_pair(INF, INF))); ns[0] = std::vector>(1, std::make_pair(0, 0)); for(int i{0}; i < V; ++i) { int const s = ss[i] - 1; int const t = ts[i] - 1; for(auto c: ns[s]) { auto new_cost = plus(c, vs[i]); ns[t].push_back(new_cost); } auto& n = ns[t]; auto it = std::remove_if(begin(n), end(n), [C](auto x) { return x.second > C; }); n.erase(it, end(n)); } auto& n = ns[N - 1]; std::sort(begin(n), end(n), less); if(begin(n) == end(n) || n[0].first == INF) { std::cout << -1 << std::endl; } else { std::cout << n[0].first << std::endl; } return 0; }