Under construction

Still being built. Things may change

Energy Generation

Time limit1sMemory limit1024 MB

Summary
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.
Level

Hard8 of 10

Topics
Graph, Geometry
Solved
No attempts yet

Problem

You are given a particle collision energy generator. The generator is a two-dimensional field with NN towers. The iith tower is located at (Xi,Yi)(X_i, Y_i), and all towers have the same field of effect with a radius of RR.

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 ii and tower jj when all of the following hold:

  1. Xi≠XjX_i \neq X_j
  2. Yi≠YjY_i \neq Y_j
  3. The Euclidean distance between them is at most RR.

Let pp be the particle that tower ii radiates on the quadrant where tower jj is located, and let qq be the particle that tower jj radiates on the quadrant where tower ii is located. The pair ⟨p,q⟩\langle p, q\rangle is the pair of facing particles.

  • If the pair is ⟨A,C⟩\langle A, C\rangle, ⟨C,A⟩\langle C, A\rangle, ⟨B,D⟩\langle B, D\rangle, or ⟨D,B⟩\langle D, B\rangle, tower ii generates interaction energy of GG.
  • If the pair is ⟨A,A⟩\langle A, A\rangle, ⟨B,B⟩\langle B, B\rangle, ⟨C,C⟩\langle C, C\rangle, or ⟨D,D⟩\langle D, D\rangle, tower ii generates −G-G, which means it consumes GG 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.

Radiates <A, C>Radiates <D, D>Radiates <B, C>X1=X2X_1 = X_2
Each tower generates +GG energyEach tower generates -GG energyNo interaction between these towersNo interesting phenomenon occurs between these towers

Each tower also generates PP energy by itself at its initial orientation. Each 90 degree rotation in either direction reduces that passive energy by PP. A tower rotated 90 degrees produces 0 passive energy, and a tower rotated 180 degrees produces −P-P. 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 NN, RR, GG, and PP (1≤N≤501 \le N \le 50, 1≤R,G,P≤10001 \le R, G, P \le 1000), the number of towers, the radius of effect, the interaction energy constant, and the initial passive energy of each tower. Each of the next NN lines contains three integers XiX_i, YiY_i, and OiO_i (−1000≤Xi,Yi≤1000-1000 \le X_i, Y_i \le 1000, Oi∈{0,90,180,270}O_i \in \{0, 90, 180, 270\}), the position and initial orientation of the iith 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.

Examples3

  1. Example 1

    Input
    3 10 10 15
    0 0 0
    2 2 180
    100 100 180
    
    Expected output
    35
    
  2. Example 2

    Input
    3 10 1 1000
    0 0 0
    2 2 0
    -4 4 180
    
    Expected output
    2998
    
  3. Example 3

    Input
    4 10 1000 1
    0 0 0
    0 2 90
    2 0 180
    2 2 270
    
    Expected output
    4002