using System; using System.Collections.Generic; using System.Linq; namespace YukiCoderNo1 { class Program { private static int[] townStart = new int[0]; private static int[] townEnd = new int[0]; private static int[] cost = new int[0]; private static int[] time = new int[0]; private static int roadCount = 0; private static int targetTown = 0; private static int money = 0; static void Main() { targetTown = int.Parse(Console.ReadLine()); money = int.Parse(Console.ReadLine()); roadCount = int.Parse(Console.ReadLine()); townStart = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).ToArray(); townEnd = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).ToArray(); cost = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).ToArray(); time = (Console.ReadLine()).Split(' ').Select(value => int.Parse(value)).ToArray(); List timeList = GetTimeList(1, money, 0); if (timeList.Count == 0) { timeList.Add(-1); } Console.WriteLine("{0}", timeList.Min()); } private static List GetTimeList(int nowTown, int nowMoney, int nowTime) { List result = new List(); if (nowTown == targetTown) { result.Add(nowTime); } else { for (int i = 0; i < roadCount; i++) { if (townStart[i] == nowTown) { if ((nowMoney - cost[i]) >= 0) { result.AddRange(GetTimeList(townEnd[i], (nowMoney - cost[i]), (nowTime + time[i]))); } } } } return result; } } }