Pirate Chest
Time limit15sMemory limit128 MB
Find the largest chest with top sides bounded by a and b that rests on a pond footprint and stays just below the raised water level.
- Level
Hard8 of 10
- Topics
- Math, Matrix, Sliding window
- Solved
- No attempts yet
Problem
A retired pirate wants to hide his gold coins by locking them in a chest and sinking it in a murky pond.
The chest is a rectangular box. Its top and bottom are two equal rectangles with integer side lengths: one side is at most and the other is at most . Its height is any positive integer. The chest is always aligned with the grid, and its top stays parallel to the pond's surface.
The pond's surface is a rectangle of unit squares and completely fills a valley with high vertical rock walls. The water depth at square is .
When the chest is lowered into the pond, it sinks as far as possible until its flat bottom touches the pond floor, so it rests on the shallowest square inside its footprint. The water pushed aside by the submerged chest raises the level of the pond's surface. This rise happens even if there is no room around the chest for the displaced water to spread, and the valley walls are high enough that water never spills out.
To stay hidden, the top of the chest must end up strictly below the raised water surface. If the chest were exactly one unit taller, its top would reach the surface and become visible, which is not allowed.
Find the largest volume of a chest that can be hidden in the pond this way.
Input
The first line contains four integers , , , and (). The pond's surface is , and the top of the chest has size at most . The values and are small enough that a chest with top size can never cover the entire pond.
Each of the next lines contains integers. The -th integer on the -th of these lines is (), the water depth at square .
Output
Print one integer: the maximum volume of a rectangular chest (one top side bounded by , the other bounded by ) that can be completely submerged below the pond's surface. If no chest can be hidden, print .