영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)
시간 제한 1초 · 메모리 제한 512 MB
Problem
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
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).
Output
Print the total distance the robot moves.
Constraints
- 1 ≤ n ≤ 10^5
- 1 ≤ x ≤ 10^6
예제
예제 1
5 U 2 R 3 D 1 L 5 U 2
9