100 atcoder#ABC119D. [ABC119D] Lazy Faith
[ABC119D] Lazy Faith
Score : points
Problem Statement
Along a road running in an east-west direction, there are shrines and temples. The -th shrine from the west is located at a distance of meters from the west end of the road, and the -th temple from the west is located at a distance of meters from the west end of the road.
Answer the following queries:
- Query (): If we start from a point at a distance of meters from the west end of the road and freely travel along the road, what is the minimum distance that needs to be traveled in order to visit one shrine and one temple? (It is allowed to pass by more shrines and temples than required.)
Constraints
- are all different.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the answer to the -th query.
2 3 4
100
600
400
900
1000
150
2000
899
799
350
1400
301
399
There are two shrines and three temples. The shrines are located at distances of meters from the west end of the road, and the temples are located at distances of meters from the west end of the road.
- Query : If we start from a point at a distance of meters from the west end of the road, the optimal move is first to walk meters west to visit a shrine, then to walk meters east to visit a temple.
- Query : If we start from a point at a distance of meters from the west end of the road, the optimal move is first to walk meters west to visit a temple, then to walk meters west to visit a shrine. We will pass by another temple on the way, but it is fine.
- Query : If we start from a point at a distance of meters from the west end of the road, the optimal move is first to walk meter east to visit a temple, then to walk meters west to visit a shrine.
- Query : If we start from a point at a distance of meters from the west end of the road, the optimal move is first to walk meters west to visit a shrine, then to walk meters west to visit a temple.
1 1 3
1
10000000000
2
9999999999
5000000000
10000000000
10000000000
14999999998
The road is quite long, and we may need to travel a distance that does not fit into a -bit integer.