There exists an undirected tree with n nodes numbered 0 to n - 1.
You are given a 2D integer array edges of length n - 1, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree.
Your task is to remove zero or more edges such that:
Each node has an edge with at mostk other nodes, where k is given.
The sum of the weights of the remaining edges is maximized.
Return the maximum possible sum of weights for the remaining edges after making the necessary removals.
Input: edges =[[0,1,4],[0,2,2],[2,3,12],[2,4,6]], k =2Output: 22Explanation:

* Node 2 has edges with3 other nodes. We remove the edge `[0, 2, 2]`, ensuring that no node has edges with more than `k = 2` nodes.* The sum of weights is22, and we can't achieve a greater sum. Thus, the answer is22.
Input: edges =[[0,1,5],[1,2,10],[0,3,15],[3,4,20],[3,5,5],[0,6,10]], k =3Output: 65Explanation:
* Since no node has edges connecting it to more than `k = 3` nodes, we don't remove any edges.* The sum of weights is65. Thus, the answer is65.
To maximize the sum of edge weights while ensuring no node has more than k neighbors, we need to prune the smallest-weight edges from nodes with degree > k. Since the graph is a tree, we can use DFS to process each node, always keeping the k largest-weight edges at each node.
classSolution {
public:int dfs(int u, int p, int k, vector<vector<pair<int,int>>>& g) {
vector<int> childSums;
for (auto& [v, w] : g[u]) {
if (v == p) continue;
int sub = dfs(v, u, k, g) + w;
childSums.push_back(sub);
}
sort(childSums.rbegin(), childSums.rend());
int ans =0;
for (int i =0; i < min(k, (int)childSums.size()); ++i)
ans += childSums[i];
return ans;
}
intmaximizeSumOfWeights(int n, vector<vector<int>>& edges, int k) {
vector<vector<pair<int,int>>> g(n);
for (auto& e : edges) {
g[e[0]].emplace_back(e[1], e[2]);
g[e[1]].emplace_back(e[0], e[2]);
}
return dfs(0, -1, k, g);
}
};
classSolution {
publicintmaximizeSumOfWeights(int n, int[][] edges, int k) {
List<int[]>[] g =new ArrayList[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]});
}
return dfs(0, -1, k, g);
}
privateintdfs(int u, int p, int k, List<int[]>[] g) {
List<Integer> childSums =new ArrayList<>();
for (int[] e : g[u]) {
if (e[0]== p) continue;
int sub = dfs(e[0], u, k, g) + e[1];
childSums.add(sub);
}
childSums.sort(Collections.reverseOrder());
int ans = 0;
for (int i = 0; i < Math.min(k, childSums.size()); i++)
ans += childSums.get(i);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funmaximizeSumOfWeights(n: Int, edges: Array<IntArray>, k: Int): Int {
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])
}
fundfs(u: Int, p: Int): Int {
val childSums = mutableListOf<Int>()
for ((v, w) in g[u]) {
if (v == p) continue childSums.add(dfs(v, u) + w)
}
childSums.sortDescending()
return childSums.take(k).sum()
}
return dfs(0, -1)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defmaximizeSumOfWeights(self, n: int, edges: list[list[int]], k: int) -> int:
from collections import defaultdict
g = defaultdict(list)
for u, v, w in edges:
g[u].append((v, w))
g[v].append((u, w))
defdfs(u, p):
child_sums = []
for v, w in g[u]:
if v == p:
continue child_sums.append(dfs(v, u) + w)
child_sums.sort(reverse=True)
return sum(child_sums[:k])
return dfs(0, -1)