Rat Attack

Time limit2sMemory limit128 MB

Summary
Given weighted points on a 1025x1025 grid and a Chebyshev distance d, find the integer center covering the maximum total weight, breaking ties by smallest x then y.
Level

Medium4 of 10

Topics
Prefix sum, Matrix, Brute force
Solved
No attempts yet

Problem

Manhattan is laid out as a regular grid of streets and avenues, and the sewage canals beneath them share the same layout. Rats build their nests under the street intersections, and the only effective way to wipe out a nest is to drop a gas bomb into the canals.

When a bomb explodes, the gas spreads through the canals in a square pattern. A bomb has a strength dd that is the square "radius" of its diffusion area: a bomb dropped at (x1,y1)(x_1, y_1) destroys a nest at (x2,y2)(x_2, y_2) if and only if

max⁡(∣x2−x1∣, ∣y2−y1∣)≤d\max(|x_2 - x_1|,\ |y_2 - y_1|) \le d

Diffusion area of the gas after an explosion

The city is a discrete grid whose coordinates run from 00 to 10241024 on each axis. You are given the strength dd of a single gas bomb together with the positions and sizes of the rat populations. The explosion location must itself be a grid field with integer coordinates in [0,1024][0, 1024]. Choose the location that destroys the largest total rat population.

Ties are resolved as follows:

  • The sum of the sizes of every rat population inside the diffusion area must be maximal.
  • If several locations achieve that maximum, choose the smallest one, ordered first by its xx coordinate and then by its yy coordinate.

Input

The first line contains the number of scenarios.

Each scenario is described as follows:

  • The first line contains the bomb strength dd (1≤d≤501 \le d \le 50).
  • The second line contains the number of rat populations nn (1≤n≤200001 \le n \le 20000).
  • Each of the next nn lines contains three space-separated integers xx, yy and ii: the position (x,y)(x, y) of a population and its size ii (1≤i≤2551 \le i \le 255). Every coordinate satisfies 0≤x,y≤10240 \le x, y \le 1024, and no position appears more than once.

Output

For each scenario, print a single line with three space-separated integers: the xx and yy coordinates of the chosen explosion location, followed by the total size of the rat populations destroyed there.

Examples2

  1. Example 1

    Input
    1
    1
    2
    4 4 10
    6 6 20
    
    Expected output
    5 5 30
    
  2. Example 2

    Input
    1
    1
    1
    5 5 10
    
    Expected output
    4 4 10