#P6797. 「StOI-2」不朽的逃亡者

「StOI-2」不朽的逃亡者

Problem Description

Balboa wants to escape to an immortal cause—discovering the Pacific Ocean.

Now Balboa is at position (1,1)(1,1) in a matrix, and the Pacific Ocean is at (n,m)(n,m). The danger value at position (i,j)(i,j) is di,jd_{i,j}. He has captured kk Indians. The ii-th person knows about the range [axi,ayi][bxi,byi][ax_i,ay_i] [bx_i,by_i] (a rectangle with (axi,ayi)(ax_i,ay_i) as the top-left corner and (bxi,byi)(bx_i,by_i) as the bottom-right corner). If Balboa brings this person along, there will be no danger within this range.

Because time is tight, Balboa moves in 4-connected directions, and he must visit exactly n+m1n+m-1 positions to reach the Pacific Ocean.

Now Balboa hopes to bring at most ww people, and at the same time minimize the sum of danger values. Find the minimum possible value.

Input Format

The first line contains 44 positive integers nn, mm, kk, ww, with meanings as described above.

Next is an nn-row mm-column matrix, with meanings as described above.

Next are kk lines, each containing 44 positive integers axiax_i, bxibx_i, ayiay_i, byiby_i, with meanings as described above.

Output Format

One positive integer, the minimum value.

4 4 3 1
1 2 3 3
3 2 1 4
2 1 3 3
3 4 2 1
3 4 2 4
1 4 1 2
1 2 2 4
3

Hint

Sample Explanation

Choose the second person.

Path: (1,1)->(2,1)->(3,1)->(4,1)->(4,2)->(4,3)->(4,4)

Danger values: the unprotected (4,3) and (4,4), which is 2+1=32+1=3.

Constraints

This problem uses bundled testdata.

Subtask 11 (1010 points): 1n,m,k,w41 \leq n,m,k,w \leq 4.
Subtask 22 (2020 points): 1n,m,k,w201 \leq n,m,k,w \leq 20.
Subtask 33 (2020 points): 1n,m,k,w501 \leq n,m,k,w \leq 50.
Subtask 44 (2020 points): all di,jd_{i,j} are the same.
Subtask 55 (3030 points): no special properties.

For all data: 1n,m,k2001 \leq n,m,k \leq 200, 1w1001 \leq w \leq 100, 0di,j1080 \leq d_{i,j} \leq 10^8, 1axibxin1 \leq ax_i \leq bx_i \leq n, 1ayibyim1 \leq ay_i \leq by_i \leq m.

Note: the input order is slightly different from the statement.

Output Format

一个正整数,表示最小值。

Hint

Translated by ChatGPT 5