Under construction

Still being built. Things may change

Two Button-Pressing Robots

Time limit5sMemory limit512 MB

Summary
Two robots starting at button 1 share one ordered press sequence and move or press each second, so compute the minimum time to finish it.
Level

Easy3 of 10

Topics
Simulation, Greedy
Solved
No attempts yet

Problem

Orange and Blue are friendly robots. An evil computer has locked them into separate hallways to test them, and it might give them cake afterwards.

Each hallway holds 100 buttons numbered 1 to 100. Button kk sits kk meters from the start of the hallway, and both robots start at button 1. In one second a robot walks one meter in either direction, presses the button at its own position once, or stays where it is and does nothing. To pass the test, the robots must press a given sequence of buttons in the given order. Both robots know the whole sequence in advance. Find the minimum time the two robots need to finish the sequence.

For example, suppose the sequence is O 2, B 1, B 2, O 4. Here O 2 means button 2 in Orange's hallway and B 1 means button 1 in Blue's hallway. The robots can finish this sequence in 6 seconds with the strategy below.

Time | Orange           | Blue
-----+------------------+------------------
  1  | Move to button 2 | Stay at button 1
  2  | Push button 2    | Stay at button 1
  3  | Move to button 3 | Push button 1
  4  | Move to button 4 | Move to button 2
  5  | Stay at button 4 | Push button 2
  6  | Push button 4    | Stay at button 2

Blue has to wait until Orange has finished pressing O 2 before it can press B 1.

Input

The first line holds the number of test cases TT. TT test cases follow.

Each test case is a single line. The line starts with the number of buttons to press, NN, followed by NN terms of the form Ri PiR_i\ P_i. RiR_i is a robot color, always O or B, and PiP_i is a button number.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1001 \le N \le 100
  • 1≤Pi≤1001 \le P_i \le 100 for all ii

Output

For each test case, print one line of the form Case #x: y, where xx is the test case number starting from 1 and yy is the minimum number of seconds the robots need to press the given buttons in order.

Examples5

  1. Example 1

    Input
    3
    4 O 2 B 1 B 2 O 4
    3 O 5 O 8 B 100
    2 B 2 B 1
    
    Expected output
    Case #1: 6
    Case #2: 100
    Case #3: 4
    
  2. Example 2

    Input
    1
    1 O 1
    
    Expected output
    Case #1: 1
    
  3. Example 3

    Input
    1
    1 B 100
    
    Expected output
    Case #1: 100
    
  4. Example 4

    Input
    1
    3 O 5 O 5 O 5
    
    Expected output
    Case #1: 7
    
  5. Example 5

    Input
    1
    4 O 100 B 100 O 1 B 1
    
    Expected output
    Case #1: 201