A series of highways connect n cities numbered from 0 to n - 1. You are given a 2D integer array highways where highways[i] = [city1i, city2i, tolli] indicates that there is a highway that connects city1i and city2i, allowing a car to go from city1i to city2i and vice versa for a cost of tolli.
You are also given an integer k. You are going on a trip that crosses
exactlyk highways. You may start at any city, but you may only visit each city at most once during your trip.
Return _themaximum cost of your trip. If there is no trip that meets the requirements, return _-1.

Input: n =5, highways =[[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], k =3Output: 17Explanation:
One possible trip is to go from 0->1->4->3. The cost of this trip is4+11+2=17.Another possible trip is to go from 4->1->2->3. The cost of this trip is11+3+3=17.It can be proven that 17is the maximum possible cost of any valid trip.Note that the trip 4->1->0->1is not allowed because you visit the city 1 twice.
Example 2:
1
2
3
4

Input: n =4, highways =[[0,1,3],[2,3,2]], k =2Output: -1Explanation: There are no valid trips of length 2, so return-1.
Since the number of cities is small (≤ 15), we can use bitmask DP to track which cities have been visited. For each state (current city, visited cities, steps taken), we try all possible next cities via available highways, maximizing the total cost.
classSolution {
funmaximumCost(n: Int, highways: Array<IntArray>, k: Int): Int {
val g = Array(n) { mutableListOf<Pair<Int, Int>>() }
for (h in highways) {
g[h[0]].add(h[1] to h[2])
g[h[1]].add(h[0] to h[2])
}
val dp = Array(n) { Array(1 shl n) { IntArray(k+1) { -1 } } }
for (start in0 until n) dp[start][1 shl start][0] = 0for (steps in0 until k) {
for (u in0 until n) {
for (mask in0 until (1 shl n)) {
if (dp[u][mask][steps] == -1) continuefor ((v, cost) in g[u]) {
if (mask and (1 shl v) ==0) {
val nmask = mask or (1 shl v)
dp[v][nmask][steps+1] = maxOf(dp[v][nmask][steps+1], dp[u][mask][steps] + cost)
}
}
}
}
}
var ans = -1for (u in0 until n)
for (mask in0 until (1 shl n))
ans = maxOf(ans, dp[u][mask][k])
return ans
}
}
classSolution:
defmaximumCost(self, n: int, highways: list[list[int]], k: int) -> int:
from collections import defaultdict
g = defaultdict(list)
for u, v, cost in highways:
g[u].append((v, cost))
g[v].append((u, cost))
dp = [[[-1] * (k+1) for _ in range(1<<n)] for _ in range(n)]
for start in range(n):
dp[start][1<<start][0] =0for steps in range(k):
for u in range(n):
for mask in range(1<<n):
if dp[u][mask][steps] ==-1:
continuefor v, cost in g[u]:
ifnot (mask & (1<<v)):
nmask = mask | (1<<v)
dp[v][nmask][steps+1] = max(dp[v][nmask][steps+1], dp[u][mask][steps] + cost)
ans =-1for u in range(n):
for mask in range(1<<n):
ans = max(ans, dp[u][mask][k])
return ans
impl Solution {
pubfnmaximum_cost(n: i32, highways: Vec<Vec<i32>>, k: i32) -> i32 {
let n = n asusize;
let k = k asusize;
letmut g =vec![vec![]; n];
for h in highways {
let (a, b, c) = (h[0] asusize, h[1] asusize, h[2]);
g[a].push((b, c));
g[b].push((a, c));
}
letmut dp =vec![vec![vec![-1; k+1]; 1<<n]; n];
for start in0..n {
dp[start][1<<start][0] =0;
}
for steps in0..k {
for u in0..n {
for mask in0..(1<<n) {
if dp[u][mask][steps] ==-1 { continue; }
for&(v, cost) in&g[u] {
if (mask & (1<<v)) ==0 {
let nmask = mask | (1<<v);
dp[v][nmask][steps+1] = dp[v][nmask][steps+1].max(dp[u][mask][steps] + cost);
}
}
}
}
}
letmut ans =-1;
for u in0..n {
for mask in0..(1<<n) {
ans = ans.max(dp[u][mask][k]);
}
}
ans
}
}