QOJ.ac

QOJ

Time Limit: 6 s Memory Limit: 64 MB Total points: 100

#23686. Mall

Statistics

In Zagreb, the capital of Croatia, it has been decided to build a large shopping mall in the suburbs to coincide with the holding of IOI 2007. The candidate site for constructing the shopping mall is divided into a grid of $m$ blocks horizontally by $n$ blocks vertically, as shown in the figure below.

Example of a candidate site ($m = 10$, $n = 7$)

The city of Zagreb has decided to select a rectangular region of $a$ blocks horizontally by $b$ blocks vertically from within the candidate site and build the shopping mall there. However, some blocks within the candidate site are already inhabited by people, and the shopping mall cannot be built on that land. If there are no inhabited blocks inside a rectangular region of $a$ blocks horizontally by $b$ blocks vertically, the shopping mall can be built by purchasing all the blocks within that rectangular region.

For financial reasons of the city of Zagreb, the cost required for land acquisition needs to be minimized as much as possible. In order to formulate a budget plan, it is necessary to urgently calculate the cost required for land acquisition to build the shopping mall. Therefore, the city of Zagreb has decided to ask you, a candidate representative for IOI 2007, to write a program for this purpose.

Write a program which, given as input the size of the candidate site, the size of the shopping mall, and for each block, information on whether people live there and, if no one lives there, the cost required to purchase that block, finds the minimum cost required for land acquisition to build the shopping mall.

In the following, the block in the $i$-th column from the left and the $j$-th row from the top of the candidate site is denoted by $(i, j)$.

Input

The first line of the input file mall.in contains two integers $m, n$ ($1 \le m, n \le 1000$) separated by a space. This indicates that the size of the candidate site is $m$ blocks horizontally by $n$ blocks vertically.

The second line contains two integers $a, b$ ($1 \le a, b \le 1000$) separated by a space. This indicates that the size of the shopping mall is $a$ blocks horizontally by $b$ blocks vertically.

The following $n$ lines (from line $3$ to line $n + 2$) contain information about each block within the candidate site. Line $j + 2$ ($1 \le j \le n$) represents the information of the blocks in the $j$-th row from the top, and contains $m$ integers $c_{1,j}, \dots, c_{m,j}$ ($-1 \le c_{i,j} \le 100$) separated by spaces. When $c_{i,j} = -1$, it indicates that people live in block $(i, j)$. Otherwise, it indicates that the cost required to purchase block $(i, j)$ is $c_{i,j}$.

Note that for the input data used for grading, constructing the shopping mall is always possible.

Output

The output should be written to the standard output. Output the minimum cost required for land acquisition to build the shopping mall.

Sample

Input 1

7 6
3 2
26 29 84 15 -1 1 71
45 14 38 91 62 77 35
68 -1 -1 90 63 56 70
31 2 4 74 72 41 90
100 26 21 -1 44 72 60
71 4 40 93 48 -1 50

Output 1

184

The construction site of the shopping mall in this sample input and output is illustrated below. A block marked with $\times$ means that people already live there.

Note

Pay attention to the size of the input data. In particular, since iostream in C++ is slow, it is advisable to use fscanf or similar functions as needed.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.