There is an undirected graph with n nodes numbered from 0 to n - 1
(inclusive). You are given a 0-indexed integer array values where
values[i] is the value of the ith node. You are also given a
0-indexed 2D integer array edges, where each edges[j] = [uj, vj, timej] indicates that there is an undirected edge between the nodes uj and
vj, and it takes timej seconds to travel between the two nodes. Finally, you are given an integer maxTime.
A validpath in the graph is any path that starts at node 0, ends at node 0, and takes at mostmaxTime seconds to complete. You may visit the same node multiple times. The quality of a valid path is the sum of the values of the unique nodes visited in the path (each node’s value is added at most once to the sum).
Return themaximum quality of a valid path.
Note: There are at most four edges connected to each node.

Input: values =[0,32,10,43], edges =[[0,1,10],[1,2,15],[0,3,10]], maxTime =49Output: 75Explanation:
One possible path is0->1->0->3->0. The total time taken is10+10+10+10=40<=49.The nodes visited are 0,1, and 3, giving a maximal path quality of 0+32+43=75.

Input: values =[5,10,15,20], edges =[[0,1,10],[1,2,10],[0,3,10]], maxTime =30Output: 25Explanation:
One possible path is0->3->0. The total time taken is10+10=20<=30.The nodes visited are 0 and 3, giving a maximal path quality of 5+20=25.

Input: values =[1,2,3,4], edges =[[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime =50Output: 7Explanation:
One possible path is0->1->3->1->0. The total time taken is10+13+13+10=46<=50.The nodes visited are 0,1, and 3, giving a maximal path quality of 1+2+4=7.
Since the graph is small (at most 100 nodes, each with at most 4 edges), we can use backtracking (DFS) to explore all valid paths starting and ending at node 0 within the time limit. We keep track of the set of unique nodes visited and the current path quality, updating the answer whenever we return to node 0.
classSolution {
int ans = 0;
publicintmaximalPathQuality(int[] values, int[][] edges, int maxTime) {
int n = values.length;
List<int[]>[] g =new List[n];
for (int i = 0; i < n; i++) g[i]=new ArrayList<>();
for (int[] e : edges) {
g[e[0]].add(newint[]{e[1], e[2]});
g[e[1]].add(newint[]{e[0], e[2]});
}
int[] vis =newint[n];
dfs(0, 0, 0, values, g, vis, maxTime);
return ans;
}
voiddfs(int u, int time, int quality, int[] values, List<int[]>[] g, int[] vis, int maxTime) {
if (vis[u]== 0) quality += values[u];
vis[u]++;
if (u == 0) ans = Math.max(ans, quality);
for (int[] e : g[u]) {
int v = e[0], t = e[1];
if (time + t <= maxTime) {
dfs(v, time + t, quality, values, g, vis, maxTime);
}
}
vis[u]--;
}
}
classSolution {
var ans = 0funmaximalPathQuality(values: IntArray, edges: Array<IntArray>, maxTime: Int): Int {
val n = values.size
val g = Array(n) { mutableListOf<Pair<Int, Int>>() }
for (e in edges) {
g[e[0]].add(e[1] to e[2])
g[e[1]].add(e[0] to e[2])
}
val vis = IntArray(n)
fundfs(u: Int, time: Int, quality: Int) {
var q = quality
if (vis[u] ==0) q += values[u]
vis[u]++if (u ==0) ans = maxOf(ans, q)
for ((v, t) in g[u]) {
if (time + t <= maxTime) dfs(v, time + t, q)
}
vis[u]-- }
dfs(0, 0, 0)
return ans
}
}
classSolution:
defmaximalPathQuality(self, values: list[int], edges: list[list[int]], maxTime: int) -> int:
from collections import defaultdict
g = defaultdict(list)
for u, v, t in edges:
g[u].append((v, t))
g[v].append((u, t))
n = len(values)
ans =0 vis = [0] * n
defdfs(u: int, time: int, quality: int):
nonlocal ans
if vis[u] ==0:
quality += values[u]
vis[u] +=1if u ==0:
ans = max(ans, quality)
for v, t in g[u]:
if time + t <= maxTime:
dfs(v, time + t, quality)
vis[u] -=1 dfs(0, 0, 0)
return ans