#include using namespace std; #define rep(i,n) for(int i=0;i<(n);++i) #define loop for(;;) #define trace(var) cerr<<">>> "<<#var<<" = "<; template inline ostream& operator<<(ostream&os, pair p) { return os << '(' << p.first << ", " << p.second << ')'; } template inline ostream& operator<<(ostream&os, tuple t) { return os << '(' << get<0>(t) << ", " << get<1>(t) << ')'; } template inline ostream& operator<<(ostream&os, tuple t) { return os << '(' << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ')'; } template inline ostream& operator<<(ostream&os, set v) { os << "(set"; for (T item: v) os << ' ' << item; os << ")"; return os; } template inline ostream& operator<<(ostream&os, vector v) { if (v.size() == 0) { return os << "(empty)"; } os << v[0]; for (int i=1, len=v.size(); i inline istream& operator>>(istream&is, vector&v) { rep (i, v.size()) is >> v[i]; return is; } // ^ > v < int dx[] = { -1, 0, 1, 0 }; int dy[] = { 0, 1, 0, -1 }; using vi = vector; using vvi = vector; using vd = vector; using vvd = vector; using vb = vector; int table[50][301]; int main() { int n, c, v; cin >> n >> c >> v; vi ss(v), ts(v), ys(v), ms(v); cin >> ss >> ts >> ys >> ms; rep (i, v) { ss[i]--; ts[i]--; } vvi rev(n); rep (i, v) { rev[ss[i]].push_back(i); } rep (i, 50) rep (j, 301) table[i][j] = inf; table[0][c] = 0; rep (u, n) { rep (k, c+1) { if (table[u][k] == inf) continue; for (int i: rev[u]) { int v = ts[i]; int y = ys[i]; // cost int m = ms[i]; // time if (k-y<0) continue; table[v][k-y] = min( table[v][k-y], table[u][k] + m); } } } int ans = -1; rep (k, c+1) { if (table[n-1][k] table[n-1][k])) ans=table[n-1][k]; } cout << ans << endl; return 0; }