Skip to content

Practice Maximize Sum of Weights after Edge Removals

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#3367
LeetCode Problem

Maximize Sum of Weights after Edge Removals

Problem Statement

Given a weighted tree with edges [u, v, weight] and an integer k, remove any number of edges so that every node has at most k retained neighbors. Return the largest possible sum of the retained edge weights.

Source: Maximize Sum of Weights after Edge Removals on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [[0,1,4],[0,2,2],[2,3,12],[2,4,6]], k = 2
Output
22

# Example 2

Input
edges = [[0,1,5],[1,2,10],[0,3,15],[3,4,20],[3,5,5],[0,6,10]], k = 3
Output
65

Constraints

2 <= n <= 10^5
1 <= k <= n - 1
edges.length == n - 1
edges[i].length == 3
0 <= edges[i][0] <= n - 1
0 <= edges[i][1] <= n - 1
1 <= edges[i][2] <= 10^6
The input is generated such that edges form a valid tree.

Related LeetCode Problems

Practice more problems similar to Maximize Sum of Weights after Edge Removals.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.