Under construction

Still being built. Things may change

Knob

Time limit0.5sMemory limit1024 MB

Summary
Given left and right knob directions for T beats, sum a point for each knob repeating its previous direction and a point for both knobs turning the same way on one beat.
Level

Easy2 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Knob

A voice actor has been hooked on a rhythm game that requires operating two knobs precisely on the beat. The knob system works as follows.

  • There are two knobs, a left knob and a right knob.
  • Each knob can be turned clockwise or counterclockwise. A knob can also be left still.

The song has length TT. On the tt-th beat (1≤t≤T1 \leq t \leq T), the left knob is operated according to ltl_t, and the right knob is operated according to rtr_t, as follows.

  • If the value is 00, the knob stays still.
  • If the value is −1-1, the knob is turned counterclockwise. If the value is 11, the knob is turned clockwise.

The fun score of the song is the sum of the values measured on every beat by these rules.

  • On the 2nd beat or later, if the left knob turns in the same direction as on the previous beat, the fun score increases by 11.
  • On the 2nd beat or later, if the right knob turns in the same direction as on the previous beat, the fun score increases by 11.
  • If both knobs turn on the same beat and turn in the same direction, the fun score increases by an additional 11.

Compute the fun score for the voice actor.

Input

The first line contains an integer TT, the length of the song. (1≤T≤200 0001 \leq T \leq 200\,000)

From the 2nd line to the (T+1)(T+1)-th line, the (t+1)(t+1)-th line contains two integers ltl_t and rtr_t separated by a space, which describe how to operate the knobs on the tt-th beat. Each number is one of −1-1, 00, or 11.

Output

Print the fun score of the song on the first line.

Hint

Slow input and output can cause time limit errors when reading or printing many lines. The following methods are recommended for each language.

  • C++: To use cin/cout, add the following two lines at the start of main and use \n instead of endl when reading input. After this, C input and output functions such as scanf, printf, puts, getchar, and putchar must not be used.

    • cin.tie(nullptr);
    • sync_with_stdio(false);
  • Python: Instead of input, use sys.stdin.readline.rstrip().

  • Java and Kotlin: Use BufferedReader and BufferedWriter instead of Scanner and System.out.println. Call BufferedWriter.flush once before the program ends.

Examples5

  1. Example 1

    Input
    1
    -1 -1
    
    Expected output
    1
    
  2. Example 2

    Input
    5
    0 1
    1 1
    0 0
    -1 1
    -1 0
    
    Expected output
    3
    
  3. Example 3

    Input
    8
    1 0
    1 0
    1 0
    1 0
    -1 0
    -1 0
    -1 0
    -1 0
    
    Expected output
    6
    
  4. Example 4

    Input
    2
    1 0
    1 1
    
    Expected output
    2
    
  5. Example 5

    Input
    3
    1 1
    1 1
    -1 -1
    
    Expected output
    5