Swift DFS
Code\n\nclass Solution {\n    var res = 0\n    var map = [Int: [Int: Int]]()\n    var values = [Int]()\n\n    func maximalPathQuality(_ values: [Int], _ edges: 
# Code\n```\nclass Solution {\n    var res = 0\n    var map = [Int: [Int: Int]]()\n    var values = [Int]()\n\n    func maximalPathQuality(_ values: [Int], _ edges: [[Int]], _ maxTime: Int) -> Int {\n        for e in edges {\n            let (a, b, c) = (e[0], e[1], e[2])\n            map[a] = map[a] ?? [:]\n            map[a]![b] = c\n            map[b] = map[b] ?? [:]\n            map[b]![a] = c\n        }\n        self.values = values\n        dfs(0, Set([0]), values[0], maxTime)\n        return res\n    }\n\n    func dfs(_ node: Int, _ visited: Set<Int>, _ gain: Int, _ cost: Int) {\n        if cost < 0 { return }\n        if node == 0 { res = max(res, gain) }\n        for (nextNode, nextCost) in map[node] ?? [:] {\n            let nextGain = gain + (visited.contains(nextNode) ? 0 : values[nextNode])\n            dfs(nextNode, visited.union([nextNode]), nextGain, cost - nextCost)\n        }\n    }\n}\n\n```
['Swift']