Scaler Question

How to solve the first problem (Tree)? Please explain it to me.

Find the minimum value of the sum of weights from the root node to every node of the tree.

You were needed to find how much each edge would be traversed and then multiply it by the weight and then see which edge removed would decrease the most value using priority_queue till you remove k edges.

1 Like

Read this thread and my comment.

Thanks.

Thanks for the help.

I almost solved this problem but my solution failed because i didn’t handle mod value properly. How to handle (ans % 1000000007) when your ans is a large number in negative?

idea: you have to add MOD to the number enough times to make it positive
simple hack:
int f = abs(num/MOD) + 2;
num += f * MOD;
num %= MOD;