A creature repeats a move string K times and you report the smallest and largest squared distances from a laser over all visited points.
Medium6MathSimulationNo attempts yetTime limit1sMemory limit64 MBYerzhan built a laser that measures the distance to a distant object. For the test he uses Gorlum, a creature he caught in the forest. Gorlum is not bright and performs exactly five commands.
L: one step to the left, from (x,y) to (x−1,y).R: one step to the right, from (x,y) to (x+1,y).F: one step forward, from (x,y) to (x,y+1).B: one step backward, from (x,y) to (x,y−1).I: Gorlum takes a shiny ring with glowing letters out of his pocket and stays where he is.The laser sits at (Lx,Ly) and Gorlum starts at (Gx,Gy). The command list T is the string S concatenated K times (T=SK), and Gorlum performs the commands of T one by one from the front.
The laser measures the distance to Gorlum once at the start and once after each command, so it takes K×∣S∣+1 measurements.
Report the smallest and the largest measured distance. A distance is usually irrational, so this problem asks for the squared distance instead. The squared distance is always an integer, the point with the smallest square is the closest point, and the point with the largest square is the farthest one.
The first line contains a natural number K (1≤K≤105).
The second line contains the string S. It consists only of L, R, F, B, I, and 1≤∣S∣≤104.
The third line contains the laser coordinates Lx and Ly, separated by a space.
The fourth line contains Gorlum's starting coordinates Gx and Gy, separated by a space.
Every coordinate is an integer with absolute value at most 104.
Print the smallest and the largest squared distance on one line, separated by a space. Both values fit in a signed 64-bit integer.