function main(): void { const input = require('fs').readFileSync(0, 'utf-8').trim().split(/\s+/); let index = 0; const N = parseInt(input[index++]); const C = parseInt(input[index++]); const V = parseInt(input[index++]); const S: number[] = []; const T: number[] = []; const Y: number[] = []; const M: number[] = []; for (let i = 0; i < V; i++) { S.push(parseInt(input[index++]) - 1); } for (let i = 0; i < V; i++) { T.push(parseInt(input[index++]) - 1); } for (let i = 0; i < V; i++) { Y.push(parseInt(input[index++])); } for (let i = 0; i < V; i++) { M.push(parseInt(input[index++])); } const graph: Array> = Array.from({length: N}, () => []); for (let i = 0; i < V; i++) { graph[S[i]].push([T[i], Y[i], M[i]]); } const INF = 1e9; const dp: number[][] = Array.from({length: N}, () => Array.from({length: C + 1}, () => INF) ); dp[0][C] = 0; for (let i = 0; i < N; i++) { for (let k = C; k >= 0; k--) { if (dp[i][k] === INF) continue; for (const [to, cost, time] of graph[i]) { if (k >= cost) { const newCost = k - cost; const newTime = dp[i][k] + time; if (newTime < dp[to][newCost]) { dp[to][newCost] = newTime; } } } } } const result = Math.min(...dp[N - 1]); console.log(result === INF ? -1 : result); } // For Node.js environment if (require.main === module) { main(); }