Pizza
Time limit2sMemory limit128 MB
Given store positions on a circular road and delivery points, sum the distance from each point to its nearest store.
- Level
Medium4 of 10
- Topics
- Binary search, Array, Sorting
- Solved
- No attempts yet
Problem
JOI Pizza delivers pizzas along a ring road of total length meters that runs through the center of the city.
JOI Pizza has stores on the ring road; the main store is . Let be the distance, in meters, traveled clockwise along the ring road from to . Each of is an integer between and , and they are all distinct.
When an order arrives, the pizza is baked and delivered from the store whose travel distance to the delivery location is smallest, so that the pizza does not get cold.
A delivery location is given by an integer with : it is the point reached by traveling meters clockwise along the ring road from the main store . Deliveries must follow the ring road and no other route is allowed, but you may travel either clockwise or counterclockwise. Hence the distance from a store to a delivery location is the shorter of the two arc lengths between them.
For example, suppose the stores and delivery locations are arranged as in the figure below.

The store nearest the first delivery location is , so it is delivered from ; the travel distance from that store is . The store nearest the second delivery location is the main store , so it is delivered from ; the travel distance is .
Given the total length of the ring road, the number of stores , the number of orders , the integers giving the positions of the stores other than the main store, and the integers giving the delivery locations, write a program that computes the sum, over all orders, of the delivery travel distance (the distance from the nearest store to the delivery location).
Input
The input is given in the following format.
- Line 1: an integer , the total length of the ring road ().
- Line 2: an integer , the number of stores ().
- Line 3: an integer , the number of orders ().
- The next lines: the positions () of the stores other than the main store, one per line in this order. These values are all distinct.
- The following lines: the delivery locations (), one per line in this order.
Output
Output a single integer on one line: the sum of the delivery travel distances.