#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define REP(i,n) for(ll i=0; i<(n); ++i) #define TEN(x) ((ll)1e##x) #define ALL(v) (v).begin(), (v).end() using Distance = ll; #define INF_DIST numeric_limits::max() using Id = ll; struct edge{Id to; Distance cost;}; using Graph = vector>; vector dijkstra(Id s, const Graph & g){ vector d(g.size(), INF_DIST); d[s] = 0; using P = pair; priority_queue, greater

> que; que.push(P(0, s)); while(!que.empty()){ auto p = que.top(); que.pop(); Id v = p.second; if(d[v] < p.first) continue; for (auto e : g[v]) if(d[e.to] > d[v] + e.cost){ d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } return d; } Id calc_id(ll i, ll c, ll n) { return i + n * c; } int main(){ ll n, c, v; cin >> n >> c >> v; vector s(v); vector t(v); vector y(v); vector m(v); for (auto&i : s) cin >> i; for (auto&i : t) cin >> i; for (auto&i : y) cin >> i; for (auto&i : m) cin >> i; for (auto&i : s) --i; for (auto&i : t) --i; ll n_extended = calc_id(n,c,n); Graph g(n_extended); REP(i, v) REP(j, c + 1) if(j >= y[i]) { g[calc_id(s[i], j, n)].push_back({ calc_id(t[i], j - y[i], n), m[i] }); } auto d = dijkstra(calc_id(0, c, n), g); ll min_d = INF_DIST; REP(i, c+1) min_d = min(min_d, d[calc_id(n - 1, i, n)]); cout << (min_d != INF_DIST ? min_d : -1) << endl; return 0; }