This page is still under construction.

Parts of this page are still being built. What you see may change.

Commuter train

Time limit2sMemory limit64 MB

Summary
Pick the train stopping position inside the platform that maximizes the total nearest-door distance over all passengers, and print twice that maximum.
Level

Medium6 of 10

Topics
Brute force, Sorting
Solved
No attempts yet

Problem

Bus drivers sometimes roll past the people waiting at a stop and park where the walk to the nearest door is as long as it can be. Nobody knows why. Probably not out of spite: while the boarding crowd walks over, the passengers already on board get more room to step off.

In one far away country the government decided to put an automatic driving system on its commuter railroad. One job of that system is stopping trains at stations. A radar reports where every passenger stands on the platform, and the on-board computer picks the stopping position that maximizes the sum over all passengers of the distance from that passenger to the door closest to them. The hardware is ready and the software is late. Write that function.

The platform has length LL. There are MM passengers on it, and passenger pp stands at distance PpP_p from the start of the platform, where 0≤P1≤⋯≤PM≤L0 \le P_1 \le \dots \le P_M \le L. The train has NN doors, and door dd sits at distance DdD_d from door 1, where 0=D1<D2<⋯<DN≤L0 = D_1 < D_2 < \dots < D_N \le L. Door widths and passenger sizes are ignored.

The stopping position SS of the train is the distance from the start of the platform to door 1. With the train at SS, the distance between passenger ii and door jj is ∣Dj+S−Pi∣|D_j + S - P_i|. No door may hang off the platform, so 0≤S0 \le S and S+DN≤LS + D_N \le L. SS does not have to be an integer. It can be any real number in that range.

Input

The input holds integers separated by spaces and line breaks. The platform description comes first: LL, then MM, then P1…PMP_1 \dots P_M. The train description follows: NN, then D2…DND_2 \dots D_N. D1D_1 is always 0 and is left out of the input, so the train description is NN integers in total.

0<L≤50000 < L \le 5000, 0<M≤3000 < M \le 300, 0<N≤3000 < N \le 300.

Output

Write F(S)=∑i=1Mmin⁡1≤j≤N∣Dj+S−Pi∣F(S) = \sum_{i=1}^{M} \min_{1 \le j \le N} |D_j + S - P_i| for the total the computer wants to maximize. Take the largest value of F(S)F(S) over every legal stopping position SS, multiply it by 2, and print the result.

That largest value is always a multiple of 12\frac{1}{2}, so twice it is an integer. Print that one integer.

Examples2

  1. Example 1

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

    Input
    1
    1
    0
    1
    
    Expected output
    2