JOI-kun took a picture of the night view. The picture consists of pixels, i.e. pixels along 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 of the picture shows building, night sky, or star. Its color is white, black, or yellow, respectively. For each with , in the -th column, the pixels from the bottommost row 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 the other pixels are black pixels showing night sky.
We say a rectangular region in the picture shows a constellation if the following two conditions are satisfied.
- There is no white pixel in the rectangular region.
- There are two or more yellow pixels in the rectangular region.
JOI-kun is tired with watching constellations. By painting some yellow pixels into black, he wants to make a picture such that no rectangular region shows a constellation. However, the picture becomes unnatural if he paints many yellow pixels. More precisely, if he paints the -th yellow pixel into black, then the unnatural level of the picture is increased by . Initially the picture has unnatural level .
Write a program which, given the information of the picture and the integer for each yellow pixel which describes how much the unnatural level is increased if the pixel is painted into black, calculates the minimum unnatural level of the picture after painting some yellow pixels such that no rectangular region shows a constellation.
Input Specification
Output Specification
Output the minimum unnatural level of the picture after painting some yellow pixels such that no rectangular region shows a constellation.
Constraints
.
.
.
.
.
.
.
.
Subtasks
- (14 points) , .
- (21 points) , .
- (65 points) No additional constraints.
Sample Input 1
5
1 3 4 2 3
3
1 5 3
4 3 2
2 4 2
Sample Output 1
2
Explanation for Sample 1
In this sample input, the rectangular region whose left-top vertex is the pixel and right-bottom vertex is the pixel shows a constellation. If JOI-kun paints the third yellow pixel into black, then the unnatural level of the picture is increased by and no rectangular region in the picture shows a constellation. Since this is the minimum value, output . Figure 1 is the picture of this sample input.
Sample Input 2
7
5 6 2 3 6 7 6
5
7 7 5
3 3 7
3 7 10
1 7 6
4 7 8
Sample Output 2
16
Explanation for Sample 2
In this sample input, it is optimal to paint the third yellow pixel and the fourth one.
Sample Input 3
8
6 8 5 7 3 4 2 1
10
8 2 9
6 6 7
8 3 18
5 8 17
8 5 3
5 5 3
5 4 8
1 8 13
1 7 5
7 4 13
Sample Output 3
44
Comments