Under construction

Still being built. Things may change

Button-Pressing Robots

Interview

Time limit5sMemory limit512 MB

Summary
Two robots walk through separate hallways in parallel and press one shared ordered list of buttons in the shortest total time.
Level

Easy3 of 10

Topics
Simulation, Greedy
Solved
No attempts yet

Problem

The blue robot and the orange robot are locked in two separate hallways. Each hallway holds 100 buttons numbered 1 to 100, and button kk sits kk meters from the hallway entrance. Both robots start in front of button 1.

During one second a robot does exactly one of these three things.

  • Walk one meter in either direction.
  • Press the button at its current position once.
  • Stay in place and do nothing.

The robots must press a given list of buttons in the given order. Both robots know the whole list in advance, and both act during every second. A button cannot be pressed until the press of the previous button in the list has finished. Compute the minimum number of seconds needed to press every button in the list, in order.

O 2 means button 2 in the orange robot's hallway, and B 1 means button 1 in the blue robot's hallway. For instance, the order O 2, B 1, B 2, O 4 can be finished in 6 seconds with the plan below.

SecondOrangeBlue
1Move to button 2Wait at button 1
2Press button 2Wait at button 1
3Move to button 3Press button 1
4Move to button 4Move to button 2
5Wait at button 4Press button 2
6Press button 4Wait at button 2

Blue can press B 1 only after orange has finished pressing O 2.

Input

The first line contains the number of test cases TT.

Each of the next TT lines holds one test case. The line starts with the number of buttons to press, NN, followed by NN items in the order R1R_1 P1P_1 R2R_2 P2P_2 and so on. RiR_i is the robot color, either O or B, and PiP_i is a button number. All values are separated by spaces.

Limits

  • 1≤T≤201 \le T \le 20
  • 1≤N≤101 \le N \le 10
  • 1≤Pi≤1001 \le P_i \le 100 for every ii

Output

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

Examples4

  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 B 1
    
    Expected output
    Case #1: 1
    
  3. Example 3

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

    Input
    1
    10 O 1 O 1 O 1 O 1 O 1 O 1 O 1 O 1 O 1 O 1
    
    Expected output
    Case #1: 10