#P4313. Maximizing Student Satisfaction in Arts and Science Selection

    ID: 17559 Type: Default 1000ms 256MiB

Maximizing Student Satisfaction in Arts and Science Selection

Maximizing Student Satisfaction in Arts and Science Selection

An n x m classroom is arranged in a grid where each cell represents a student's seat. Each student must choose between two subjects: Arts and Science. If the student in cell \( (i, j) \) chooses Arts, they obtain a base satisfaction value of \( art_{i,j} \); if they choose Science, they obtain \( science_{i,j} \).

In addition, if a student chooses Arts and all of their adjacent neighbors (those sharing a common side) also choose Arts, they receive an extra bonus of \( same\_art_{i,j} \). Similarly, if a student chooses Science and all of their adjacent neighbors also choose Science, they get an extra bonus of \( same\_science_{i,j} \). Note that if a student has no adjacent neighbors, the bonus condition is considered satisfied.

Your task is to determine an assignment of subjects to students (each cell assigned either Arts or Science) that maximizes the total satisfaction value. Output this maximum total value.

inputFormat

The first line contains two integers \( n \) and \( m \) (the dimensions of the grid).
The next \( n \) lines each contain \( m \) integers representing the matrix \( art \); the \( j^{th} \) integer on the \( i^{th} \) line is \( art_{i,j} \).
Following that, the next \( n \) lines describe the matrix \( science \) in the same format.
Then, the next \( n \) lines contain the matrix \( same\_art \), and the last \( n \) lines contain the matrix \( same\_science \).

It is guaranteed that each number is an integer.

outputFormat

Output a single integer, the maximum total satisfaction achievable by an appropriate assignment.

sample

1 1
5
3
2
1
7