If city is unreachable from city , dp[n] remains after processing all nodes. You must check this before outputting the answer.
Return "IMPOSSIBLE" or similar when dp[n] equals your sentinel value. If you forget this check, you might output as a large negative number, which is wrong.
This edge case appears in many shortest/longest path problems. Always verify reachability. In a DAG, unreachable nodes keep their initial values. The topological order guarantees you process all reachable nodes, but cannot create paths that do not exist.