Hide

Problem E
Goat on a Rope

/problems/goatonarope/file/statement/en/img-0001.jpg
“Close-Up Shot of a Goat Tied With a Rope” by Nothing Ahead on pexels.com

A goat is tied up outside of a convex polygonal barn by a rope of length $r$. One end of the rope is tied to the easternmost corner of the barn, and the other end is tied to the goat’s collar. The goat gets freaked out by a solar eclipse and starts running around the barn counterclockwise, starting from a point directly east of the easternmost corner of the barn, and always keeping the rope stretched taut. How far will the goat run before becoming stuck against the side of the barn?

Input

The first line of input consists of a floating-point number $r$ ($1 \leq r \leq 10^4$), the length of the rope in meters, and an integer $n$ ($3 \leq n \leq 50$), the number of corners of the barn. The following $n$ lines each contain two space-separated floating-point numbers $x_ i, y_ i$ ($-10^3 \leq x_ i, y_ i \leq 10^3$) giving the $x$ and $y$ coordinates of the $i$th corner of the barn, specified in meters relative to some arbitrary origin.

The barn is guaranteed to be convex, and the corners of the barn will be given in counterclockwise order, beginning with the corner to which the goat is attached. The corner to which the goat is attached will be one of the easternmost vertices of the barn (though there may be multiple such vertices). No three vertices will be collinear.

For the purposes of this problem you may assume that the rope is infinitely thin (but infinitely strong) and that the goat occupies a single point precisely at the end of the rope.

Output

Output a single real number giving the distance, in meters, that the goat will run before becoming stuck against the side of the barn. Your output should be correct to within an absolute or relative error of at most $10^{-6}$.

Illustration of Sample Input

\includegraphics[width=\textwidth ]{goatonarope/problem_statement/diagrams/goat.png}

The goat starts out as shown on the left, attached to the easternmost point of the barn by the rope. The diagram on the right shows most of the path taken by the goat as it proceeds counterclockwise and the rope becomes wrapped around the barn. The dotted lines show various positions of the rope in the past. The diagram depicts a point in time when the goat still has more distance to go, but will soon run into the side of the barn. When it does, the total distance traveled by the goat (i.e. the length of the solid black curve shown in the diagram) will be as shown in the sample output below.

Sample Input 1 Sample Output 1
11.0 3
0.0 0.0
-4.0 3.0
-4.0 0.0
45.477180586675374

Please log in to submit a solution to this problem

Log in