Hot Dog Proliferation (Small)

Vendors sharing a corner split one step east and one step west per move, and the task asks for the fewest moves that leave every vendor on a distinct corner.

Medium7Dynamic programmingSortingNo attempts yetTime limit5sMemory limit512 MB

Problem

Hot dog vendors have set up at the corners (intersections) of a very long east-west street. The trouble is that several vendors can end up at the same corner, and then they take business from each other. There is still a way out. The vendors have a plan.

Whenever a corner holds two or more vendors, exactly two of them can perform a move, which means:

  • One vendor goes one corner further east along the street.
  • The other vendor goes one corner further west along the street.

The street is really long, so the vendors never run out of corners. Given the starting positions of all hot dog vendors, find the minimum number of moves they need to perform before the vendors are all separated, meaning they are all on different corners.

For example, suppose the number of hot dog vendors on each corner, listed in order from west to east, starts like this.

... 0 0 2 1 2 0 0 ...

Then the vendors can be separated in three moves, as shown below.

... 0 0 2 1 2 0 0 ...
        |
        +--- Do a move here

... 0 1 0 2 2 0 0 ...
          |
          +--- Do a move here

... 0 1 1 0 3 0 0 ...
            |
            +--- Do a move here

... 0 1 1 1 1 1 0 ...

Input

Each street corner is labeled with an integer, positive or negative. For each ii, corner i+1i+1 is the next corner to the east from corner ii. The input uses this labeling to describe corners.

The first line contains the number of cases TT. TT test cases follow. Each case begins with the number of corners CC that have at least one hot dog vendor in the starting configuration. The next CC lines each contain a pair of space-separated integers PP and VV, indicating that there are VV vendors at corner PP.

Limits

  • 1T501 \le T \le 50
  • 1C2001 \le C \le 200
  • 1000000P1000000-1000000 \le P \le 1000000
  • Within each test case all PP values are distinct and listed in increasing order.
  • Each VV is a positive integer, and the sum of all VV values in one test case, the total number of vendors, is at most 200.
  • It is always possible to separate the hot dog vendors in a finite number of moves.

Output

For each test case, output one line containing Case #x: M, where xx is the case number starting from 1 and MM is the minimum number of moves that need to be performed before the vendors all end up at different corners from each other.