PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#CT0000130. Phần thưởng (Prizes)

Bài tập này chưa được xếp vào lộ trình.

Phần thưởng (Prizes)

Prizes

Source: Bộ Giáo dục và Đào tạo

Version: Phuoc Hung OJ Extended

Problem Statement

Vinh may choose exactly one of kk prize plans. The organizers place a prize in every cell of an n×nn\times n grid. The prize at row ii, column jj has value aija_{ij}; row and column indices start at 11.

Each plan contains pp squares, each covering r×rr\times r cells. Square hh in plan ss is specified by the top-left cell (xsh,ysh)(x_{sh},y_{sh}). Every square lies entirely within the grid, and squares may overlap. Selecting a plan awards the prizes in all cells contained in at least one of its squares; a cell is counted only once even if several squares cover it.

Determine the maximum total prize value obtainable by choosing exactly one plan.

Input

  • The first line contains four positive integers n,k,r,pn,k,r,p.
  • Each of the next nn lines contains nn positive integers; the jjth integer on line ii is aija_{ij}.
  • Each of the final kk lines contains 2p2p integers xs1,ys1,…,xsp,yspx_{s1},y_{s1},\ldots,x_{sp},y_{sp} identifying the top-left corners of the pp squares in plan ss.

Output

Print one integer equal to the maximum total prize value.

Subtasks

  • Subtask 1 (25 points): 1≤n≤501\le n\le50; 1≤k≤501\le k\le50; 1≤p≤51\le p\le5; 1≤r≤n1\le r\le n; 1≤aij≤1061\le a_{ij}\le10^6; 1≤xsh,ysh≤n−r+11\le x_{sh},y_{sh}\le n-r+1.
  • Subtask 2 (25 points): 1≤n≤5001\le n\le500; 1≤k≤1051\le k\le10^5; p=2p=2; 1≤r≤n1\le r\le n; 1≤aij≤1061\le a_{ij}\le10^6; 1≤xsh,ysh≤n−r+11\le x_{sh},y_{sh}\le n-r+1.
  • Subtask 3 (25 points): 1≤n≤5001\le n\le500; 1≤k≤1051\le k\le10^5; p=3p=3; 1≤r≤n1\le r\le n; 1≤aij≤1061\le a_{ij}\le10^6; 1≤xsh,ysh≤n−r+11\le x_{sh},y_{sh}\le n-r+1.
  • Subtask 4 (25 points): 1≤n≤5001\le n\le500; 1≤k≤1051\le k\le10^5; 1≤p≤51\le p\le5; 1≤r≤n1\le r\le n; 1≤aij≤1061\le a_{ij}\le10^6; 1≤xsh,ysh≤n−r+11\le x_{sh},y_{sh}\le n-r+1.

Examples

Example 1

Input

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

Output

12

Explanation: Plan 11 uses top-left corners (1,1)(1,1), (2,2)(2,2), (3,3)(3,3) and covers 1010 distinct cells. Plan 22 uses (1,1)(1,1), (1,3)(1,3), (3,1)(3,1) and covers 1212 distinct cells. Every prize has value 11, so the maximum sum is 1212.