Under construction

Still being built. Things may change

Find the House

Interview

Time limit1sMemory limit1024 MB

Summary
Starting from a given position, repeatedly follow the triple whose start matches the current position, then print the position reached after all triples are used.
Level

Medium4 of 10

Topics
Hash map, Simulation, Implementation
Solved
No attempts yet

Problem

Younghee is on a one-dimensional road and is looking for her friend Jisun's house. To learn the exact position of Jisun's house, Younghee sends Jisun a message with her current position (assume that all positions are represented as integers). A couple of minutes later, Younghee receives a reply: a list of nn triples from Jisun, with the following explanation attached.

  • For each triple (i,j,k)(i, j, k) in the list, ii is an integer that denotes the current position, jj denotes the direction to move from ii and is either LL (left) or RR (right), and kk is a positive integer that denotes the distance to move from ii.
  • For any two triples (i,j,k)(i, j, k) and (i′,j′,k′)(i', j', k') in the list, ii and i′i' are distinct.
  • If you are currently at position ii, a triple (i,j,k)(i, j, k) always exists in the list (unless every triple in the list has already been referred to). In this case, refer to the triple (i,j,k)(i, j, k) and move to i−ki - k (if j=Lj = \texttt{L}) or i+ki + k (if j=Rj = \texttt{R}).
  • Each triple in the list is referred to exactly once.
  • The position after referring to all the triples in the list is the position of Jisun's house.

For example, suppose Younghee is currently at position 0 with a list of four triples: (3,R,4)(3, \texttt{R}, 4), (0,L,2)(0, \texttt{L}, 2), (7,L,5)(7, \texttt{L}, 5), and (−2,R,5)(-2, \texttt{R}, 5). Then Younghee first refers to the triple (0,L,2)(0, \texttt{L}, 2) and moves to position 0−2=−20 - 2 = -2. After that, Younghee refers to the triples (−2,R,5)(-2, \texttt{R}, 5), (3,R,4)(3, \texttt{R}, 4), and (7,L,5)(7, \texttt{L}, 5) in order and moves to position 22, which is the position of Jisun's house. Given nn, Younghee's current position, and a list of nn triples, write a program to find the position of Jisun's house.

Input

Your program reads from standard input. The input starts with a line containing an integer nn (1≤n≤10,0001 ≤ n ≤ 10,000), where nn is the number of triples in the list. In the following nn lines, nn triples are given, where each triple is represented as three values ii, jj, and kk: two integers ii and jj and one character kk (−1,000,000≤i≤1,000,000-1,000,000 ≤ i ≤ 1,000,000, j∈{L,R}j \in \{\texttt{L}, \texttt{R}\}, and 1≤k≤2,000,0001 ≤ k ≤ 2,000,000). After the nn lines of triples, there is a line containing Younghee's current position as an integer between −1,000,000-1,000,000 and 1,000,0001,000,000.

Output

Your program writes to standard output. Print exactly one line. The line contains the position of Jisun's house.

Examples2

  1. Example 1

    Input
    4
    3 R 4
    0 L 2
    7 L 5
    -2 R 5
    0
    
    Expected output
    2
    
  2. Example 2

    Input
    5
    3 L 3
    -1 R 11
    5 L 6
    1 R 4
    10 L 7
    1
    
    Expected output
    0