#BS0000136. Thăm đền và miếu (Lazy Faith)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Thăm đền và miếu (Lazy Faith)
Lazy Faith
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
There are shrines at and temples at on an east-west road. All coordinates are distances in metres from the western end. For each of independent starting locations , find the shortest total walking distance required to visit at least one shrine and at least one temple, in either order. Additional sites may be passed.
Input
The first line contains . The next lines contain , one per line. The following lines contain , one per line. The final lines contain the starting locations .
Output
Print lines; line is the shortest travel distance for starting point .
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; for every ; all shrine, temple and start coordinates are pairwise distinct.
- Subtask 2 (30 points): ; ; ; ; ; for every ; all shrine, temple and start coordinates are pairwise distinct.
- Subtask 3 (50 points): ; ; ; ; ; for every ; all shrine, temple and start coordinates are pairwise distinct.
Examples
Input
2 3 4
100
600
400
900
1000
150
2000
899
799
Output
350
1400
301
399
Explanation
Shrines are at ; temples at . From , go to ( m), then ( m): . From , go to ( m), then ( m): . From , visit ( m), then ( m): . From , visit ( m), then ( m): . All nearest predecessor/successor choices in both visit orders are considered.