AlgoUniversity
  • Go Back
Discussion
Uber 28 Q1 :

Author

Dolton Fernandes

Difficulty Level : Hard

Submissions : 218

Asked In : Uber

Marks :10

: 2 | : 0

You are the mayor of a very old city. The city has n major tourist attractions. You are given the locations $$$(x, y, z)$$$ for each tourist attraction.

To boost tourism in your city, you can plan to create new roads that connect tourist attractions.

To create a bidirectional road between tourist attraction A (located at $$$(x_1, y_1, z_1)$$$) and B (located at $$$(x_2, y_2, z_2)$$$), you need to spend $$$min(|x_1 - x_2|, |y_1 - y_2|, |z_1 - z_2|)$$$ dollars. Here $$$|x_1 - x_2|$$$ refers to the absolute value of $$$x_1 - x_2$$$, and $$$min(x, y, z)$$$ refers to the minimum value out of $$$x$$$, $$$y$$$, and $$$z$$$.

You need to create a network of roads such that it is possible to travel between any pair of tourist attractions using some sequence of roads. What is the minimum amount of dollars you need to spend to accomplish this task?

Input

The first line contains an integer $$$n$$$ $$$(2 \le n \le 10^5)$$$, denoting the number of major tourist attractions. The following $$$n$$$ lines contain three space-separated integers $$$x_i, y_i \& z_i$$$ $$$(-10^5 \le x_i, y_i, z_i \le 10^5)$$$, the locations of these tourist attractions.

Output

The minimum number of dollars that you need to spend.

Example

Input
3
1 5 7
2 9 4
1 3 9
Output
1

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.