Rat Attack
Time limit2sMemory limit128 MB
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 that is the square "radius" of its diffusion area: a bomb dropped at destroys a nest at if and only if

The city is a discrete grid whose coordinates run from to on each axis. You are given the strength 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 . 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 coordinate and then by its coordinate.
Input
The first line contains the number of scenarios.
Each scenario is described as follows:
- The first line contains the bomb strength ().
- The second line contains the number of rat populations ().
- Each of the next lines contains three space-separated integers , and : the position of a population and its size (). Every coordinate satisfies , and no position appears more than once.
Output
For each scenario, print a single line with three space-separated integers: the and coordinates of the chosen explosion location, followed by the total size of the rat populations destroyed there.