import std.stdio; import std.conv, std.array, std.algorithm, std.string; import std.math, std.random, std.range, std.datetime; import std.bigint; class Edge{ Node from; Node to; int cost; int time; this(Node from, Node to, int cost, int time){ this.from = from; this.to = to; this.cost = cost; this.time = time; to.inedges ~= this; } } class Node{ Edge[] inedges; int[int] xs; // cost -> time this(){ } } void main(){ int n = readln.chomp.to!int; int c = readln.chomp.to!int; int v = readln.chomp.to!int; int[] ss = readln.chomp.split.map!(to!int).map!(x => x - 1).array; int[] ts = readln.chomp.split.map!(to!int).map!(x => x - 1).array; int[] ys = readln.chomp.split.map!(to!int).array; int[] ms = readln.chomp.split.map!(to!int).array; Node[] nodes; nodes ~= new Node; nodes[0].xs[0] = 0; foreach(i; 0 .. n) nodes ~= new Node; Edge[] edges; for(int i = 0; i < v; i ++){ edges ~= new Edge(nodes[ss[i]], nodes[ts[i]], ys[i], ms[i]); } foreach(no; nodes){ foreach(ed; no.inedges){ foreach(cq; ed.from.xs.keys){ if(cq + ed.cost <= c){ if(!(cq + ed.cost in no.xs) || no.xs[cq + ed.cost] < ed.from.xs[cq] + ed.time){ no.xs[cq + ed.cost] = ed.from.xs[cq] + ed.time; } } } } int thr = int.max; int[] ks = no.xs.keys; ks.sort!("a < b")(); foreach(k; ks){ if(no.xs[k] >= thr) no.xs.remove(k); else thr = no.xs[k]; } } int ans = -1; if(nodes[n - 1].xs.keys.length){ int min = int.max; foreach(t; nodes[n - 1].xs){ if(t < min) min = t; } ans = min; } ans.writeln; }