Pizza

Time limit2sMemory limit128 MB

Summary
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 dd meters that runs through the center of the city.

JOI Pizza has nn stores S1,…,SnS_1, \dots, S_n on the ring road; the main store is S1S_1. Let did_i be the distance, in meters, traveled clockwise along the ring road from S1S_1 to SiS_i. Each of d2,…,dnd_2, \dots, d_n is an integer between 11 and d−1d-1, 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 kk with 0≤k≤d−10 \le k \le d-1: it is the point reached by traveling kk meters clockwise along the ring road from the main store S1S_1. 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 S2S_2, so it is delivered from S2S_2; the travel distance from that store is 11. The store nearest the second delivery location is the main store S1S_1, so it is delivered from S1S_1; the travel distance is 22.

Given the total length dd of the ring road, the number of stores nn, the number of orders mm, the n−1n-1 integers d2,…,dnd_2, \dots, d_n giving the positions of the stores other than the main store, and the integers k1,…,kmk_1, \dots, k_m 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 dd, the total length of the ring road (2≤d≤1092 \le d \le 10^9).
  • Line 2: an integer nn, the number of stores (2≤n≤1000002 \le n \le 100000).
  • Line 3: an integer mm, the number of orders (1≤m≤100001 \le m \le 10000).
  • The next n−1n-1 lines: the positions d2,d3,…,dnd_2, d_3, \dots, d_n (1≤di≤d−11 \le d_i \le d-1) of the stores other than the main store, one per line in this order. These values are all distinct.
  • The following mm lines: the delivery locations k1,k2,…,kmk_1, k_2, \dots, k_m (0≤ki≤d−10 \le k_i \le d-1), one per line in this order.

Output

Output a single integer on one line: the sum of the delivery travel distances.

Examples5

  1. Example 1

    Input
    8
    3
    2
    3
    1
    4
    6
    
    Expected output
    3
    
  2. Example 2

    Input
    20
    4
    4
    12
    8
    16
    7
    7
    11
    8
    
    Expected output
    3
    
  3. Example 3

    Input
    2
    2
    1
    1
    0
    
    Expected output
    0
    
  4. Example 4

    Input
    10
    3
    3
    4
    7
    0
    5
    9
    
    Expected output
    2
    
  5. Example 5

    Input
    100
    2
    2
    50
    99
    1
    
    Expected output
    2