Water Pump
InterviewTime limit2sMemory limit512 MB
Place one pump in a cell, let water drain toward it from both sides, and find the cell that removes the most water.
- Level
Medium5 of 10
- Topics
- Array, Prefix sum
- Solved
- No attempts yet
Problem
walls stand in a line from west to east, numbered to starting at the west end. Wall is units tall. A wall lets no water through, its thickness is , and every wall stands on flat ground at height .
Wall and wall enclose a space of width . That space is cell , and the city has cells. One unit of water fills one cell to a depth of , so a cell whose water level is holds units of water.
Heavy rain floods the city. Water crosses a wall once its level rises above the top of that wall, and water that crosses west of wall or east of wall leaves the city. After the rain stops, the water level in cell is
The governor picks one cell and puts a pump in cell . The pump removes all water from cell and keeps removing whatever flows in. While the level in a cell is above the wall it shares with a neighboring cell, water crosses that wall into the neighbor, so water from distant cells can reach the pump. Pumping ends when no more water flows into cell .
Pick so that the pump removes as much water as possible, and print how many units it removes. When there is no cell, so the answer is .
Input
The first line has an integer , the number of test cases. ()
Each test case takes two lines. The first line has , the number of walls. () The second line has the wall heights separated by spaces, where is the height of wall . ()
Output
Print lines. Line holds the answer for the -th test case, the largest number of water units a single pump can remove.