import std.stdio; import std.string; import std.array; import std.typecons; import std.typetuple; import std.container; import std.algorithm; import std.conv; import std.math; import std.format; alias TypeTuple tie; void mdaFill(T,S)(ref T[] arry, S val) { import std.traits; static if (isIterable!(T)) { foreach(ref a; arry) mdaFill(a, val); } else { import std.algorithm; fill(arry, val); } } void readlnToken(T...)(auto ref T args) { import std.stdio; import std.conv; import std.string; import std.array; auto line = split(readln().strip); foreach(ref arg; args) { arg = to!(typeof(arg))(line[0]); line = line[1..$]; } assert(line.empty()); // got all token?? } void solve() { int N,C,V; readlnToken(N); readlnToken(C); readlnToken(V); auto S = readln.strip.split.array.map!(to!int).map!(x => x-1); auto T = readln.strip.split.array.map!(to!int).map!(x => x-1); auto Y = readln.strip.split.array.map!(to!int); auto M = readln.strip.split.array.map!(to!int); alias Edge = Tuple!(int,"to",int,"cost",int,"time"); auto edges = new Edge[][](N,0); foreach(i;0..V) { edges[S[i]] ~= Edge(T[i],Y[i],M[i]); } auto dp = new int[][](N,C+1); const inf = 1<<30; mdaFill(dp, inf); dp[0][0] = 0; foreach(i;0..N) { foreach(j;0..C) { foreach(e;edges[i]) { if ( j+e.cost <= C ) dp[e.to][j+e.cost] = min(dp[e.to][j+e.cost], dp[i][j] + e.time); } } } int ret = inf; foreach(c;0..C+1) ret = min(ret, dp[N-1][c]); if (ret == inf) writeln(-1); else writeln(ret); } void main() { solve(); }