The kindergarten attached to the University of Aizu is a kindergarten where children who love programming gather. Yu, one of the kindergarten children, loves rectangles as much as programming. Yu-kun decided to write a program to calculate the maximum score that can be obtained, thinking of a new play to get points using three rectangles.
Problem
Given the rectangles A and B of the H × W cells and the rectangle C of the h × w cells. (H and W represent the number of vertical and horizontal squares of rectangles A and B, respectively, and h and w represent the number of vertical and horizontal squares of rectangle C, respectively.)
As shown in Fig. 1, an integer is written in each cell of A, and each cell of B is colored white or black. Each cell in C is also colored white or black.
Figure 1
Figure 1
If there is a rectangle in B that has exactly the same pattern as C, you can get the sum of the integers written in the corresponding squares in A as a score.
For example, when the rectangles A, B, and C as shown in Fig. 1 are used, the same pattern as C is included in B as shown by the red line in Fig. 2, so 193 points can be obtained from that location.
Figure 2
Figure 2
If there is one or more rectangles in B that have exactly the same pattern as C, how many points can be obtained in such rectangles?
However, if there is no rectangle in B that has the same pattern as C, output "NA" (excluding "). Also, the rectangle cannot be rotated or inverted.
Constraints
The
input satisfies the following conditions.
All inputs are integers
1 ≤ H, W ≤ 50
1 ≤ h ≤ H
1 ≤ w ≤ W
-100 ≤ a (i, j) ≤ 100
b (i, j), c (i, j) is 0 or 1
Input
The input format is as follows.
H W
a (1,1) a (1,2) ... a (1, W)
a (2,1) a (2,2) ... a (2, W)
::
a (H, 1) a (H, 2) ... a (H, W)
b (1,1) b (1,2) ... b (1, W)
b (2,1) b (2,2) ... b (2, W)
::
b (H, 1) b (H, 2) ... b (H, W)
h w
c (1,1) c (1,2) ... c (1, w)
c (2,1) c (2,2) ... c (2, w)
::
c (h, 1) c (h, 2) ... c (h, w)
a (i, j) is the integer written in the square (i, j) of the rectangle A, b (i, j) is the color of the square (i, j) of the rectangle B, and c (i, j) Represents the color of the square (i, j) of the rectangle C. As for the color of the square, 0 is white and 1 is black.
Output
If there is a place in rectangle B that has exactly the same pattern as rectangle C, output the one with the highest score among such places.
If no such location exists, print “NA” (excluding “).
We need to parse the problem carefully. We have rectangle A (HxW) with integers. Rectangle B (HxW) with values 0/1 (white/black). Rectangle C (hxw) with values 0/1. We need to find all positions (top-left corners) (i,j) within B such that the sub-rectangle of B of size hxw starting at (i,j) exactly matches C (i.e., B[i+dx][j+dy] == C[dx+1][dy+1] for all 0<=dx<h, 0<=dy<w). For each such position, we compute the sum of integers in A over the same sub-rectangle (i.e., sum of A[i+dx][j+dy] for same dx,dy). Among these sums (scores), we want the maximum score. If there are no matches, output "NA".
Constraints: H,W up to 50, h<=H, w<=W. So we can brute force all possible positions O(HWh*w) which is up to 50^4 = 6.25 million, which is fine.
We need to be careful with indexing: The input format: first H W, then H lines of A, then H lines of B, then h w, then h lines of C. So we can read accordingly.
Edge cases: Negative values possible. So max might be negative. That is fine; we output that negative number. But if there is at least one match, we output the max, even if negative.
If no match, output NA.
Implementation straightforward.
But let's think: Are there any special nuances? "Exactly the same pattern as C" means each cell matches exactly. No rotations or inversions.
We need to output the score of the one with the highest score. If multiple have same highest, just output that number.
Example 1 matches expected.
Example 2: H=3,W=3; A: 5 1 3; 2 5 9; 0 1 5; B: 1 0 0; 0 1 1; 0 1 1; C: 1x1; 1. So we find positions where B cell =1. The scores: at (1,1) A[1][1]=5? Wait careful: indexing from 1? In problem, they use 1-indexed presumably. But we can store 0-indexed arrays. For each cell (i,j) with B[i][j]==1, the score is A[i][j]. The max among those: A[?] B has 1 at (1,1) (value 5), (2,2)=5? Actually B: row1: 1 0 0 => (0,0)=1; row2: 0 1 1 => (1,1)=1, (1,2)=1; row3: 0 1 1 => (2,1)=1, (2,2)=1. So A: row1: 5 1 3; row2: 2 5 9; row3: 0 1 5. Scores: (0,0)=5, (1,1)=5, (1,2)=9, (2,1)=1, (2,2)=5. Max=9, output 9. Good.
Example 3: H=3,W=4; A: 4 1 9 1; 9 1 -1 3; 2 -4 1 10. B: 1 1 0 1; 0 1 1 1; 1 1 0 0. C: 1x4: 1 1 1 1. We need to find row where B row has [1,1,1,1] exactly. Check each row: row1: 1,1,0,1 -> not all 1. row2: 0,1,1,1 -> not. row3:1,1,0,0 -> not. So no match, output NA.
So algorithm:
Read H,W.
Read A: H lines of W ints.
Read B: H lines of W ints (0/1).
Read h,w.
Read C: h lines of w ints (0/1).
Now iterate over all possible top-left positions i in [0, H-h], j in [0, W-w] (0-indexed). For each, check if for all di in [0,h), dj in [0,w): B[i+di][j+dj] == C[di][dj]. If yes, compute sum of A[i+di][j+dj] over the same region. Keep track of max sum (initialize to a very low number, maybe -inf). After loops, if we found at least one match, output max sum; else output "NA".
Complexity: O(HWh*w) up to 6.25e6, each iteration simple.
We can also precompute prefix sums for A to get O(1) sum per region, but not necessary.
But careful: H,W up to 50, h,w up to 50, so worst-case 505050*50=6.25e6, which is fine. Also each comparison is simple.
Implementation details: Use int arrays.
One nuance: Input might have spaces and newlines; just read using typical scanner.
Potential pitfalls: The input lines for B are integers (0/1) but may be separated by spaces. We'll read as ints.
We need to watch out for trailing spaces.
Now, produce final answer.
But we should consider: In the problem, "rectangle C of the h × w cells" and "If there is a rectangle in B that has exactly the same pattern as C" means the pattern must match exactly, not necessarily the entire B? Yes, sub-rectangle of same size.
We also need to consider that B and A are the same size HxW, but the pattern C is smaller. So we slide over all possible positions.
Edge: If h==H and w==W, then only one position (0,0) to check.
Ok.
Now we output the maximum score.
But the problem statement says: "If there is a place in rectangle B that has exactly the same pattern as rectangle C, output the one with the highest score among such places." It doesn't specify if there are ties, any is fine.
Implementation in Python? The problem might be from AIZU or similar, but language not specified. The assistant can output code in Python. But since this is a text response, we need to provide solution? Actually the user hasn't explicitly asked for code; they've presented a problem statement. Typically on OpenAI platform, we answer with solution explanation and perhaps code. But the instruction: "You are an AI programming assistant" and "Solve the following problem". Usually we need to output the final answer? Actually the problem is a programming problem; they likely expect the code. The conversation: "Background ... Input ... Output ..." and then "Solve the following problem". So we should produce a solution, i.e., write code to solve it. Possibly the answer should be the code, or we can also provide explanation. However, typical OpenAI tasks: "Solve the following problem" means produce code that can be run to solve based on input. I think we should output a Python solution. But to be safe, we can give explanation and code.
Given the format, I'll provide Python code that reads from stdin and prints the answer.
Let's implement.
Pseudo:
import sys
def main():
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
H = int(next(it))
W = int(next(it))
A = [[int(next(it)) for _ in range(W)] for _ in range(H)]
B = [[int(next(it)) for _ in range(W)] for _ in range(H)]
h = int(next(it))
w = int(next(it))
C = [[int(next(it)) for _ in range(w)] for _ in range(h)]
code
max_score = None
found = False
for i in range(H - h + 1):
for j in range(W - w + 1):
# check match
match = True
for di in range(h):
if not match: break
for dj in range(w):
if B[i+di][j+dj] != C[di][dj]:
match = False
break
if match:
# compute sum
score = 0
for di in range(h):
for dj in range(w):
score += A[i+di][j+dj]
if max_score is None or score > max_score:
max_score = score
found = True
if not found:
print("NA")
else:
print(max_score)
if name == "main":
main()
That's it.
Potential optimization: Use early break for match.
Check positions: The matching region is i=1 (0-indexed? actually from description, top-left (2,2) 1-indexed? Let's compute: B rows: row1: 1 0 0 1; row2:0 0 1 0; row3:0 1 0 1; row4:0 0 1 0. C: 2x3:
1 0 1
0 1 0
We need to find submatrix 2x3. The one shown in figure: likely rows 2-3, columns 2-4? Let's test: starting (i=1,j=1) (0-index): B[1][1]=0, B[1][2]=1, B[1][3]=0; B[2][1]=1, B[2][2]=0, B[2][3]=1? Wait B[2][1] row3 col2 = 1? Actually B rows: row3: 0 1 0 1 => (2,1)=1, (2,2)=0, (2,3)=1? Actually indices: row3: index2: [0,1,0,1] so j=0->0,1->1,2->0,3->1. So at (i=1,j=1): B[1][1]= B row2 col2 =? row2: [0,0,1,0] -> col2 = 0? Actually col index 1? Wait 0-index: row2 (index1): [0,0,1,0] so col0=0,col1=0,col2=1,col3=0. So (i=1,j=1) => B[1][1]=0. C[0][0]=1 -> mismatch. So not that. Let's find match manually: We'll iterate all possible 2x3. The pattern C: first row: 1,0,1; second row:0,1,0. Check each top-left. Possibly (i=0,j=0): B[0][0]=1, B[0][1]=0, B[0][2]=? B[0][2]=0? Actually row1: [1,0,0,1] so (0,0)=1, (0,1)=0, (0,2)=0 -> third should be 1 but is 0 -> mismatch. (i=0,j=1): (0,1)=0? C[0][0]=1 -> fail. (i=1,j=0): row2: [0,0,1,0]; row3: [0,1,0,1]; check (i=1,j=0): B[1][0]=0, need 1 -> fail. (i=1,j=1): we already did fail. (i=2,j=0): row3: [0,1,0,1]; row4: [0,0,1,0]; (2,0)=0 need 1 -> fail. (i=2,j=1): row3col1=1? row3col1=1? row3: col1=1, row3col2=0, row3col3=1? Actually (2,1): row3 col1=1, (2,2): row3col2=0, (2,3): row3col3=1; row4 col1=0? row4 col1=0, row4col2=1? row4: [0,0,1,0] so col1=0, col2=0? Wait row4: index3: [0,0,1,0] so col0=0,col1=0,col2=1,col3=0. So (i=2,j=1): row3col1=1, row3col2=0, row3col3=1; row4col1=0, row4col2=1? Actually row4col2? row4 at col2 = 1? row4col2=1? row4 indices: j=0->0, j=1->0, j=2->1, j=3->0. So row4col1 (j=1) is 0, row4col2 (j=2) is 1. For pattern second row: [0,1,0], we need row4col1=0, row4col2=1, row4col3=0. row4col3 (j=3) is 0. So that matches: row4col1=0, row4col2=1, row4col3=0. And first row from row3: need [1,0,1] row3col1=1, row3col2=0, row3col3=1 -> matches. So (i=2,j=1) works (0-index). That's rows 3-4, columns 2-4? Actually i=2 => third row (index2+1=3), j=1 => second column (index1+1=2). So region is rows 3-4, columns 2-4 (3x3 region? Actually 2 rows, 3 columns). So the matching region is bottom-right corner. Then the score from A: A[2][1] =? A rows: row1:10 2 -1 6; row2:8 1 -100 41; row3:22 47 32 11; row4:-41 99 12 -8. So (2,1)=47, (2,2)=32, (2,3)=11; (3,1)=99, (3,2)=12, (3,3)=-8. Sum = 47+32+11+99+12-8 = 193. Yes.
Our code will find that and compute max (the only match likely). So output 193.
Works.
Now we can also add small optimizations, but not needed.
Edge: max_score may be negative, so we need to handle initialization properly. Use None or flag.
Implementation fine.
Now answer: Provide code.
But the problem may be from AIZU online judge; they accept solutions in C, C++, Java, etc. But we can provide Python.
Thus final answer: Provide Python solution.
I'll include explanation as well, but mainly code.
Thus output. ```python
import sys
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
code
H = int(next(it))
W = int(next(it))
# rectangle A (integers)
A = [[int(next(it)) for _ in range(W)] for _ in range(H)]
# rectangle B (0/1)
B = [[int(next(it)) for _ in range(W)] for _ in range(H)]
h = int(next(it))
w = int(next(it))
# rectangle C (0/1)
C = [[int(next(it)) for _ in range(w)] for _ in range(h)]
max_score = None
found = False
# Try all top-left positions of the h×w sub‑rectangle in B
for i in range(H - h + 1):
for j in range(W - w + 1):
# check pattern match
match = True
for di in range(h):
if not match:
break
row_B = B[i + di]
row_C = C[di]
for dj in range(w):
if row_B[j + dj] != row_C[dj]:
match = False
break
if match:
# compute score from A
score = 0
for di in range(h):
row_A = A[i + di]
for dj in range(w):
score += row_A[j + dj]
if max_score is None or score > max_score:
max_score = score
found = True
if not found:
print("NA")
else:
print(max_score)