Gold Mines
Time limit3sMemory limit256 MB
Pick an axis-aligned rectangle over weighted points to maximize the sum of enclosed weights.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum, Sorting
- Solved
- No attempts yet
Problem
A country is nicknamed the land of gold. Many of its gold mines are still undeveloped. On a map each gold mine is drawn as a point in the plane.

Figure 1
Each point carries an integer that is positive or negative. is the profit or the loss produced by developing that mine. If is positive, developing the mine earns . If is negative, developing the mine costs .
A mine developer buys a rectangular plot whose sides are parallel to the axis and the axis, then develops every mine contained in . A mine lying on a side of the rectangle counts as contained in . The development profit is the sum of over the mines contained in .
The developer looks for the rectangle with the largest development profit. For the mines in Figure 1, the region with the largest development profit is the one drawn in Figure 2, and that profit is 7.
Given the coordinates of the gold mines and the profit or loss of developing each one, write a program that prints the largest development profit obtainable by buying one rectangular plot.

Figure 2
Input
The first line contains the number of gold mines (). Each of the next lines contains two non-negative integers and () giving the coordinates of a mine, and an integer () giving the profit or the loss of developing it, separated by spaces. All mine coordinates are distinct, and at least one mine has .
Output
Print on one line the largest development profit the developer can obtain by buying a rectangular plot . The value can exceed the range of a 32-bit integer during the computation, so you may need a 64-bit integer type.