Akatsuki vs Leaf
Practice
4.3 (32 votes)
Approved
Bit manipulation
Dynamic programming
Easy
Matching
Ready
Problem
93% Success 6952 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Akatsuki is planning to attack the Leaf Village to capture Naruto. The Hokage ( Head of the Village ), came to know about the plan and also the number of members, N, in Akatsuki who are going to attack. So, Hokage planned an ambush.

enter image description here

Hokage selected a team of N members ( one for each Akatsuki member ) and named it Leaf. Each member of Leaf can attack exactly one Akatsuki member and an Akatsuki member is NOT attacked by more than one Leaf member. In the ambush there will be N fights ( one of each N members of Leaf and N members of Akatsuki ). You are the leader of Leaf. You know the positions ( x-coordinates and y-coordinates ) of all the Akatsuki members and Leaf members. Your task is to assign each member of Leaf to exactly one member of Akatsuki team such that the sum of the distance between them is minimum. Distance between two points \((x_1, y_1)\) and \((x_2, y_2)\) will be equal to \(|x_1 - x_2| + |y_1 - y_2|\).

Input:
First line contains one integer N, number of Akatsuki members who are going to attack the village.
Next N lines will contain two integers each, X and Y, x-coordinate and y-coordinate of the Akatsuki members.
Next N lines will contain two integers each, X and Y, x-coordinate and y-coordinate of the Leaf members.

Output:
Print the required minimum sum of the distance.

Constraints:
\(1 \le N \le 20\)
\(-10^6 \le X, Y \le 10^6\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
275 votes
Tags:
ApprovedBit ManipulationEasyOpenReady
Points:30
21 votes
Tags:
AlgorithmsDynamic ProgrammingEasy
Points:30
57 votes
Tags:
ApprovedBit ManipulationCombinatoricsEasyMathOpenReady