AlgoUniversity
  • Go Back
Discussion
Falling Apples :

Author

Shikhar Mehrotra

Difficulty Level : Medium

Submissions : 960

Asked In : Razorpay

Marks :20

: 11 | : 2

It is the apple harvest season. Alice and Bob are all set to pluck them from their farm. However, due to a certain incident, the apples are now falling down one after another. The farm in an infinite large 2D plane.

Given the initial coordinates of Alice $$$(A_x,A_y)$$$ and initial coordinates of Bob $$$(B_x,B_y)$$$ and also the coordinates where N apples will fall, you need to calculate the minimum Manhattan distance covered in total by Alice and Bob together in order to catch all apples.

  • Manhattan distance between 2 coordinates $$$X_1,Y_1$$$ and $$$X_2,Y_2$$$ $$$=|X_1-X_2|+|Y_1-Y_2|$$$.
  • It can be assumed that they can reach all distances without letting any apple fall down to the ground.
  • Also, you need to help them catch the apples in the sequence they fall. i.e For example They can't catch the second falling apple before the first. In particular, it is necessary that all apples before the $$$i^{th}$$$ falling apple must be caught before catching the $$$i^{th}$$$ apple for all $$$1\le i \le N$$$

Input

The first line consists of an integer $$$1\le N \le 10^{3}$$$ denoting number of apples

The second line contain two space separated integers $$$-10^{9}\le A_x \le 10^{9}$$$ and $$$-10^{9}\le A_y \le 10^{9}$$$ representing Alice's initial coordinates.

The third line contain two space separated integers $$$-10^{9}\le B_x \le 10^{9}$$$ and $$$-10^{9}\le B_y \le 10^{9}$$$ representing Bob's initial coordinates.

$$$N$$$ line follow where $$$1\le i \le N$$$ line consist of two space separated integers $$$-10^{9}\le X_i \le 10^{9}$$$ representing $$$-10^{9}\le Y_i \le 10^{9}$$$

Output

Output the minimum distance travelled to cover all the apples.

Examples

Input
2
0 5
2 2
0 3
2 3
Output
3
Input
5
-5 4
1 0
0 10
-11 5
12 1
0 9
-10 8
Output
57

Note

In first test case $$$N=2$$$ and $$$A_x=0,A_y=5$$$ and $$$B_x=2,B_y=2$$$ and first apple at $$$(0,3)$$$ and second apple at $$$(2,3)$$$

The first apple can be caught by Alice with manhattan distance $$$|0-0|+|5-3|=2$$$ units.

The second apple can be caught by Bob with manhattan distance $$$|2-2|+|3-2|=1$$$ units.

Hence total answer is $$$3$$$ units.

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.