AAAA 1 P2 - Heavy-Light Composition

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 1G

Author:
Problem types

Daniel has found the perfect background and time of day for the perfect group photo. In this photo, he is trying out the newest photography technique: Heavy-Light Composition! To ensure the perfect composition for his photo, he has arranged his N heavy-duty lights and M friends at unique positions across the number line. The i^{th} light is located at position l_i, while the i^{th} friend is located at position f_i.

Each light initially only illuminates its own position, but Daniel can repeatedly use one additional unit of power to extend the range of a light one position to the left OR one position to the right.

Formally, the i^{th} light initially illuminates the interval [l_i,l_i]. Expanding the range of the i^{th} light to the interval [l_i-a, l_i+b], where a,b\ge0, requires a+b units of power.

Daniel wants to make sure all of his friends are illuminated by at least one light, but electricity costs are heavy and his wallet is feeling light after buying all those heavy-duty lights! Thus, he would like you to determine the minimum amount of additional power needed to illuminate all his friends. Can you help him?

Constraints

1 \le N,M \le 2 \times 10^5
1 \le l_i,f_i \le 10^9

All l_i and f_i are distinct.

Subtask 1 [30%]

N = 2

Subtask 2 [70%]

No additional constraints.

Input Specification

The first line contains two space-separated integers, N and M, the number of heavy-duty lights and the number of friends, respectively.

The second line contains N space-separated integers, l_1, l_2, \dots, l_N, the positions of the lights.

The third line contains M space-separated integers, f_1, f_2, \dots, f_M, the positions of the friends.

Output Specification

Output one line containing one integer, the minimum amount of additional power Daniel needs to illuminate all his friends.

Sample Input 1

1 3
5
3 4 7

Sample Output 1

4

Sample Input 2

2 2
2 7
1 5

Sample Output 2

3

Sample Input 3

2 4
1 10
2 5 7 8

Sample Output 3

6

Comments

There are no comments at the moment.