AlgoUniversity
  • Go Back
Discussion
Edge Strength :

Author

Ayush Gangwani

Difficulty Level : Medium

Submissions : 191

Asked In : Razorpay

Marks :15

: 6 | : 0

We have a tree with $$$N$$$ vertices numbered $$$1,2,…,N.$$$

The $$$i^{th}$$$ edge $$$(1 \le i \le N−1)$$$ connects Vertex $$$u_i$$$​ and Vertex $$$v_i$$$​ and has a weight $$$w_i$$$.

For different vertices $$$u$$$ and $$$v$$$, let $$$f(u,v)$$$ be the greatest weight of an edge contained in the shortest path from Vertex $$$u$$$ to Vertex $$$v$$$.

Your task is to find $$$$$$\sum_{1 \le i \lt j \le N} f(i,j) $$$$$$ More formally, Please find the sum of the maximum weighted edge over all paths of the tree.

Input

The first line of input contains an integer $$$N$$$ $$$(2 \le N \le 10^5)$$$ — the number of nodes in the tree.

The next $$$N-1$$$ lines contain $$$3$$$ space separated integers each $$$u_i$$$, $$$v_i$$$ $$$(1 \le u_i​,v_i​\le N)$$$ and $$$w_i$$$ $$$(1 \le w_i​ \le 10^7)$$$ denoting an edge of weight $$$w_i$$$ between vertices $$$u_i$$$ and $$$v_i$$$.

Output

Print a single integer — the answer to the problem in a single line.

Examples

Input
3
1 2 10
1 3 2
Output
22 
Input
5
1 2 4
2 3 1
1 4 6
4 5 12
Output
75 

Note

In sample test case 1,

  • $$$f(1,2) = 10$$$
  • $$$f(1,3) = 2$$$
  • $$$f(2,3) = 10$$$
Hence the answer is $$$10+2+10=22$$$.

In sample test case 2,

  • $$$f(2,3) = 1$$$
  • $$$f(1,2) = f(1,3) = 4$$$
  • $$$f(1,4) = f(2,4) = f(3,4) = 6$$$
  • $$$f(1,5) = f(2,5) = f(3,5) = f(4,5) = 12$$$
Hence the answer is $$$1+2*4+3*6+4*12 = 75$$$

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.