Given segment lengths of a planar robotic arm and a target point, find where the tip lands when bent as close to the target as possible.
Medium5GeometryMathImplementationNo attempts yetTime limit2sMemory limit512 MBJoao wants to join his university's robot soccer team. He knows little about robotics or mathematics yet, so he decided to build a two dimensional robotic arm first and learn from it.
The arm is made of N segments. One end of the first segment is pinned to the origin, and one end of segment i is joined to the far end of segment i−1. Any angle between two neighboring segments is allowed, including angles that make the arm look like it overlaps itself when seen from above. Bending the arm never changes a segment length.
The free end of segment N is the tip of the arm. Given the segment lengths and a target point, find where the tip sits when the arm is bent so that the tip is as close to the target as possible.
Several configurations can tie for the smallest distance, but they all put the tip on the same point. There is one exception. If the target is the origin and the tip cannot reach the origin, the closest points form a full circle, and then the answer is the configuration that puts the tip on the positive x axis, so the answer has y=0 and x>0.
The first line contains N, the number of segments. Each of the next N lines contains Li, the length of the i-th segment counted from the origin toward the tip. The last line contains the coordinates x and y of the target, separated by a space.
Print the coordinates xt and yt of the tip on one line, rounded to three decimal places and separated by a space. Print a coordinate that rounds to zero as 0.000, not as -0.000.
Every input value is an integer.

The figure shows the case with segment lengths 5, 3, 4 and target (5,3). The circled cross marks the target, and the square marks the end pinned to the origin.