Constellation 3
Time limit1sMemory limit512 MB
Given a histogram of white building columns and weighted yellow star pixels, find the minimum total cost of removing stars so no rectangle free of buildings contains two or more stars.
- Level
Hard9 of 10
- Topics
- Divide and conquer, Dynamic programming, Segment tree
- Solved
- No attempts yet
Statement
JOI-kun took a picture of the night view. The picture consists of pixels, that is, pixels along the horizontal and vertical directions. The pixel in the -th column from the left and the -th row from the bottom (, ) is called the pixel .
Each pixel shows a building, the night sky, or a star. Its color is white, black, or yellow, respectively.
For each with , in the -th column, the pixels from the bottommost row up to the -th row from the bottom are white pixels showing buildings. There are yellow pixels showing stars. The -th yellow pixel () is the pixel . All other pixels are black pixels showing the night sky.
A rectangular region of the picture shows a constellation if both of the following conditions hold.
- There is no white pixel in the rectangular region.
- There are two or more yellow pixels in the rectangular region.
JOI-kun is tired of watching constellations. By painting some yellow pixels black, he wants to make a picture in which no rectangular region shows a constellation. However, painting many yellow pixels makes the picture unnatural. Specifically, if he paints the -th yellow pixel () black, the unnatural level of the picture increases by . Initially the unnatural level is 0.
Given the picture and the value for each yellow pixel, find the minimum unnatural level of the picture after painting some yellow pixels so that no rectangular region shows a constellation.
Input
The input is given in the following format.
N
A_1 A_2 \cdots A_N
M
X_1 Y_1 C_1
\vdots
X_M Y_M C_M
All values are integers.
Output
Print one line containing the minimum unnatural level after painting some yellow pixels so that no rectangular region shows a constellation.
Constraints
- .
- ().
- .
- ().
- ().
- ().
- ().
- ().