is 2400. The ride-hailing giant Suber have cracked self-driving cars and have a fleet of n of them at their disposal. All of the cars are currently on Olde st (the only street in the country, which happens to be unrealistically long and perfectly straight). The ith car is currently at location d[i] kilometres from the start of the street. Moreover, Suber have m garages along Olde street. The ith of them is at location e[i] kilometres from the start of the street. Each garage has infinite capacity and can hold infinitely many self-driving cars; any car can be kept in any garage. There’s a snowfall warning in effect and Suber have to drive all of the cars to the garages as soon as possible. In order to save electricity, they want to minimize the total distance travelled by the cars. What is the minimum total distance the cars need to travel?
Q2: Self-Driving Cars
The year is 2400. The ride-hailing giant Suber have cracked self-driving cars and have a fleet of n of them at their disposal. All of the cars are currently on Olde st (the only street in the country, which happens to be unrealistically long and perfectly straight). The ith car is currently at location d[i] kilometres from the start of the street.
Moreover, Suber have m garages along Olde street. The ith of them is at location e[i] kilometres from the start of the street. Each garage has infinite capacity and can hold infinitely many self-driving cars; any car can be kept in any garage.
There’s a snowfall warning in effect and Suber have to drive all of the cars to the garages as soon as possible. In order to save electricity, they want to minimize the total distance travelled by the cars. What is the minimum total distance the cars need to travel?
Filename
Your filename for this question must be q2.py.
Input
The input consists of two lines:
- The first line contains n integers separated by single spaces. These numbers specify the list d.
- The second line contains m integers separated by single spaces. These numbers specify the list e.
Output
Output a single integer: the minimum total distance the cars need to travel.
Constraints
- 1 ≤ n, m ≤ 105
- 0 ≤ d[i], e[i] ≤ 109
Time Limit
- Your program has to finish running within 2 seconds on any valid input.
Sample Input 1
1 0 1 2 5
Sample Output 1
4
Sample 1 Explanation
- There are 3 cars at locations 1, 0, and 1; and two garages at locations 2 and 5.
- The unique optimal solution is to drive all of the cars to the first garage. This way, the two cars at location 1 need to drive 1km each and the other car needs to drive 2km, for a total of 4km.
Sample Input 2
5 1 10 13 1 7
Sample Output 2
5
USE PYTHON

Step by step
Solved in 2 steps with 1 images









