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?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Knapsack Problem
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education