6908.Robot Path

Time Limit: 1s Memory Limit: 512MB

You are given a description of a robot's path. The robot begins at point $(0,0)$ and performs $n$ commands. Each command moves the robot some distance up, down, left or right.

The robot will stop when it has performed all commands, or immediately when it returns to a point that it has already visited. Your task is to calculate the total distance the robot moves.

Input Format(From the terminal/stdin)

The first line has an integer $n$ : the number of commands.

After that, there are $n$ lines describing the commands. Each line has a character $d$ and an integer $x$ : the robot moves the distance $x$ to the direction $d$ . Each direction is U (up), D (down), L (left), or R (right).

  • $1 \le n \le 10^5$
  • $1 \le x \le 10^6$

Output Format(To the terminal/stdout)

Print the total distance the robot moves.

Sample Input

Copy
5
U 2
R 3
D 1
L 5
U 2
 \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output

Copy
9
 \n
Source: CSES, Geometry, 1742

Submit

请先 登录

© 2025 FAQs