Given up to 20 glasses with rim circles in 3D, pour champagne into the highest one at 100 ml/s and find when the whole tower fills, or report Invalid.
Medium7GeometrySimulationImplementationGreedyNo attempts yetTime limit2sMemory limit512 MBYou may have seen a champagne tower at a wedding or a party. In a typical three level tower, the lowest level holds 9 glasses touching in a square pattern, the second level holds 4 glasses touching in a square pattern centered above the first level, and the third level holds 1 glass centered above the two levels. Figure 1 shows a top down view of that tower.

Figure 1
Champagne is always poured directly into the top glass. In this example, once the top glass fills and starts to overflow, the 4 glasses below it immediately start filling. Overflowing champagne reaches the glasses below in zero time. Once the 4 glasses on the second level fill, they overflow onto the 9 glasses on the bottom level. The 4 glasses on the second level finish filling at the same moment, but the 9 glasses on the lowest level finish at different moments, so some champagne spills on the floor before the tower is done. That loss is an acceptable price for such a beautiful sight.
The new fad is to lay glasses out into a pattern or an image. These towers do not need to be structurally sound. A support system designed so as not to interfere with the overflowing champagne holds the glasses in place. Every such tower has exactly one highest glass, and all champagne is poured directly into it.
Seen from above, the rim of a glass is a circle. Overflow follows these rules.
Determine whether a proposed champagne tower fills to completion, and if it does, how long that takes.
The first line contains one integer n, the number of glasses in the tower (1≤n≤20). Each of the next n lines describes one glass with five integers x y z r v. The point (x,y,z) is the center of the glass's rim (0≤x,y≤1000, 1≤z≤1000), r is the radius of the rim (1≤r≤1000), and v is the volume of the glass in milliliters (1≤v≤1000). All input values are integers. Exactly one glass has the largest z, and champagne is poured into that glass at a constant 100 milliliters per second.
Print the number of seconds after which the tower is completely filled, or Invalid if the tower never fills completely. Round the answer to the hundredths place, always print two decimal places, and include the leading 0 for an answer between 0 and 1. Output values are always at most 106 seconds.