Race

Time limit1sMemory limit128 MB

Summary
Given n checkpoints with scores that must be visited in increasing index order from and back to the origin, find the maximum score achievable within a runner's distance budget, for multiple runners.
Level

Medium6 of 10

Topics
Dynamic programming, Geometry, Array
Solved
No attempts yet

Problem

There are n checkpoints. Each runner starts at the origin (0, 0), visits any subset of the checkpoints, and returns to the origin. The visited checkpoint numbers must be in increasing order. If the checkpoints are numbered from 1 to n, visiting 1, 2, 5 in that order is allowed, but visiting 4 and then 2 is not.

Every move must follow the straight segment between two points. If another checkpoint lies on that segment, the runner may either count it as visited or pass it by. A checkpoint's score can be earned at most once.

Each runner has a maximum distance d they can run. Compute the maximum score the runner can earn while still returning to the origin within that limit.

Input

The input consists of multiple test cases. Each test case has the following format, and the entire input ends with a single line containing 0.

  • The first line contains the number of checkpoints n (1 <= n <= 30).
  • The next n lines each contain three integers x, y, and s: the checkpoint coordinates and score (-5000 <= x, y <= 5000, 10 <= s <= 200).
  • Then several runner lines follow. Each line contains a runner name and a maximum distance d separated by a space (0 <= d <= 10000). A name has at most 60 characters and contains no spaces.
  • The line # 0 marks the end of the runner list for the current test case.

All distances are straight-line distances in the plane.

Output

For each test case, first print Race r, where r is the test case number starting from 1.

Then, for each runner in input order, print one line in the format name: score.

Examples1

  1. Example 1

    Input
    5
    750 -800 30
    1500 0 50
    750 750 60
    -1250 750 70
    -1000 -500 50
    Chris 7000
    Karl 6500
    Tania 5000
    # 0
    4
    500 0 10
    0 500 10
    -500 0 10
    0 -500 10
    Hanny 2100
    Lizzie 1800
    # 0
    0
    
    Expected output
    Race 1
    Chris: 230
    Karl: 180
    Tania: 140
    Race 2
    Hanny: 20
    Lizzie: 20