Energy Generation
Time limit1sMemory limit1024 MB
Given N towers on a plane, choose each tower's 90-degree orientation to maximize passive energy plus pairwise interaction energy for towers within radius R.
Problem
You are given a particle collision energy generator. The generator is a two-dimensional field with towers. The th tower is located at , and all towers have the same field of effect with a radius of .
Each tower radiates four types of particles in a fixed configuration. Each tower radiates particles A, B, C, and D in clockwise order, one on each quadrant (an area separated by its x-axis and y-axis). A tower does not radiate any particle on its x-axis or y-axis.

Initially, each tower is oriented at a multiple of 90 degrees. The particles on the upper right, lower right, lower left, and upper left quadrants are listed in that order.
- 0 degrees: (A, B, C, D), as shown in the figure.
- 90 degrees (rotated clockwise from 0): (D, A, B, C).
- 180 degrees: (C, D, A, B).
- 270 degrees: (B, C, D, A).
An interesting phenomenon can occur between tower and tower when all of the following hold:
- The Euclidean distance between them is at most .
Let be the particle that tower radiates on the quadrant where tower is located, and let be the particle that tower radiates on the quadrant where tower is located. The pair is the pair of facing particles.
- If the pair is , , , or , tower generates interaction energy of .
- If the pair is , , , or , tower generates , which means it consumes energy.
- For the other 8 combinations, the two towers do not interact and no energy comes from this pair.
The phenomenon applies from each tower's perspective. Every qualifying pair adds its effect, so the effects stack.
Examples of the energy generated by a pair of towers are shown below.
Each tower also generates energy by itself at its initial orientation. Each 90 degree rotation in either direction reduces that passive energy by . A tower rotated 90 degrees produces 0 passive energy, and a tower rotated 180 degrees produces . A tower can be rotated only by multiples of 90 degrees.
Find the maximum total energy that can be generated by changing the orientation of zero or more towers. The total energy of a configuration is the sum of each tower's passive energy and the interaction energy from all qualifying pairs.
Input
The first line contains four integers , , , and (, ), the number of towers, the radius of effect, the interaction energy constant, and the initial passive energy of each tower. Each of the next lines contains three integers , , and (, ), the position and initial orientation of the th tower. No two towers share the same position.
Output
Print one integer on a line, the maximum total energy over all possible orientation configurations of the towers.



