This page is still under construction.

Parts of this page are still being built. What you see may change.

Space Miner

Interview

Time limit2sMemory limit512 MB

Summary
Travel in straight segments through ordered 3D waypoints; for each planet, check whether any segment passes within distance ri+D of its center, then sum the resources of all planets you can reach at least once.
Level

Medium6 of 10

Topics
Geometry, Implementation, Math, Simulation
Solved
No attempts yet

Problem

There are MM planets (1≤M≤1,0001 \le M \le 1{,}000). Each planet ii holds viv_i (1≤vi≤10,0001 \le v_i \le 10{,}000) units of resources and has radius rir_i (1≤ri≤1001 \le r_i \le 100).

Starting from the origin (0,0,0)(0, 0, 0), you travel in straight lines through NN (1≤N≤1,0001 \le N \le 1{,}000) waypoints in the given order.

At any moment during the trip, if your distance to a planet's center is at most D+riD + r_i (1≤D≤501 \le D \le 50), you may mine that planet with your tractor beam and collect its viv_i units of resources. You can still mine a planet when you are exactly DD units from its surface (that is, exactly ri+Dr_i + D from its center). If your path passes through a planet, that is fine — your ship can drill straight through it, which only makes mining easier. However, once a planet has been mined you cannot mine it again, even if you pass by it later.

Output the total amount of resources you can mine during the journey.

Hint: perform all calculations with 64-bit integers.

Input

The first line contains the number of planets MM. Each of the next MM lines contains five integers xi yi zi vi rix_i\ y_i\ z_i\ v_i\ r_i describing a planet: planet ii is located at (xi,yi,zi)(x_i, y_i, z_i) (−1,000≤xi,yi,zi≤1,000-1{,}000 \le x_i, y_i, z_i \le 1{,}000) and has viv_i resources and radius rir_i. The next line contains the number of waypoints NN. Each of the next NN lines contains a waypoint position as three integers xi yi zix_i\ y_i\ z_i (−1,000≤xi,yi,zi≤1,000-1{,}000 \le x_i, y_i, z_i \le 1{,}000). The last line contains DD, the maximum distance from a planet's surface at which your ship can still mine it.

Output

Output, on a single line, the total amount of resources you can mine during the journey.

Examples4

  1. Example 1

    Input
    3
    10 0 0 1 1
    0 10 0 2 1
    0 0 10 4 1
    3
    8 0 0
    0 7 0
    0 0 9
    1
    
    Expected output
    5
    
  2. Example 2

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

    Input
    1
    5 0 0 9 2
    1
    10 0 0
    1
    
    Expected output
    9
    
  4. Example 4

    Input
    1
    5 3 0 7 1
    1
    10 0 0
    2
    
    Expected output
    7