using System; using System.Collections.Generic; using System.Linq; namespace No1_2 { public class Program { public struct Input { public int Before { get; } public int After { get; } public int Cost { get; } public int Time { get; } public Input(int before, int t, int cost, int time) { Before = before; After = t; Cost = cost; Time = time; } } public struct Dp { public int Town { get; } public int Cost { get; } public int Time { get; } public Dp(int town, int cost, int time) { Town = town; Cost = cost; Time = time; } } public static void Main(string[] args) { var n = Read(Convert.ToInt32);//街の数 var c = Read(Convert.ToInt32);//所持金 var v = Read(Convert.ToInt32);//道の数 var sn = ReadList(Convert.ToInt32);//街A var tn = ReadList(Convert.ToInt32);//街B var yn = ReadList(Convert.ToInt32);//街A → B へのコスト var mn = ReadList(Convert.ToInt32);//街A → B への時間 var inputList = new List(); for(var i = 0; i < v; i++) { inputList.Add(new Input(sn[i], tn[i], yn[i], mn[i])); } inputList.Sort((x, y) => x.After - y.After); var dpList = new List { new Dp(1, 0, 0) }; foreach(var linq in inputList.Select(input => (from dp in dpList where (dp.Town == input.Before) && (dp.Cost + input.Cost <= c) select new Dp(input.After, dp.Cost + input.Cost, dp.Time + input.Time)))){ dpList.AddRange(linq.ToList()); } var result = dpList.Where(dp => dp.Town == n).Select(reach => reach.Time).Concat(new[] {int.MaxValue}).Min(); Console.WriteLine(result == int.MaxValue ? -1 : result); } public static TOutput Read(Converter converter) { return converter(Console.ReadLine()); } public static List ReadList(Converter converter, char c = ' ') { return Console.ReadLine()?.Split(c).ToList().ConvertAll(converter); } } }