Backtracking Solution || Clean Java Code
Code\n\nclass Edge {\n    int to;\n    int time;\n\n    public Edge(int to, int time) {\n        this.to = to;\n        this.time = time;\n    }\n}\n\nclass Sol
# Code\n```\nclass Edge {\n    int to;\n    int time;\n\n    public Edge(int to, int time) {\n        this.to = to;\n        this.time = time;\n    }\n}\n\nclass Solution {\n    private int maxCost = 0;\n    private final Map<Integer, Set<Edge>> children = new HashMap<>();\n\n    public int maximalPathQuality(int[] values, int[][] edges, int maxTime) {\n        for (int[] edge: edges) {\n            int from = edge[0], to = edge[1], time = edge[2];\n            children.computeIfAbsent(from, e-> new HashSet<>()).add(new Edge(to, time));\n            children.computeIfAbsent(to, e -> new HashSet<>()).add(new Edge(from, time));\n        }\n        slv(0, 0, maxTime, values, new ArrayList<>(List.of(0)));\n        return maxCost;\n    }\n\n    private void slv(int node, int time, int maxTime, int[] values, List<Integer> path) {\n        if (time > maxTime) return;\n        if (node == 0) {\n            Set<Integer> visited = new HashSet<>();\n            int cost = 0;\n            for (int n: path) {\n                if (!visited.contains(n)) {\n                    cost += values[n];\n                    visited.add(n);\n                }\n            }\n            maxCost = Math.max(cost, maxCost);\n        }\n        for (Edge edge: children.getOrDefault(node, new HashSet<>())) {\n            path.add(edge.to);\n            slv(edge.to, time + edge.time, maxTime, values, path);\n            path.remove(path.size() -1);\n        }\n    }\n}\n```
['Java']