Connecting Rods in 3D
InterviewTime limit1sMemory limit512 MB
Given a start and end point in 3D and a multiset of rod lengths, decide whether every rod can be chained from start to end, allowing overlap and at most one rod end at each of the two points.
Problem
In a three-dimensional coordinate system, we want to connect a start point and an end point with rods of various lengths.
The rods may overlap each other. Starting from the start point, we connect the rods one by one until we reach the end point.
Determine whether all the given rods, with none left unused, can be joined to form an exact connection from the start point to the end point.
Both ends of every rod must be connected to the start point, the end point, or the end of another rod. No more than one rod end may be connected to the start point or to the end point.
The thickness of the rods is small enough to ignore, so they may overlap each other.
Input
The first line gives the start point (X1, Y1, Z1) and the end point (X2, Y2, Z2) of the coordinate system.
The second line gives the number of rods N.
Starting from the third line, N lines each give an integer K, the length of one rod.
Output
Print "YES" if the rods can be used to connect the start point to the end point, and "NO" otherwise.
Constraints
- -1,000 ≤ X1, Y1, Z1, X2, Y2, Z2 ≤ 1,000
- X1, Y1, Z1, X2, Y2, Z2 are all integers
- 1 ≤ N ≤ 100
- 1 ≤ K ≤ 100