Animal Observation (easy version)
Contest: Codeforces Round 620 (Div. 2)
Difficulty rating: 2300
Tags: data structures, dp

The only difference between easy and hard versions is the constraint on $$$k$$$.

Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.

Gildong is going to take videos for $$$n$$$ days, starting from day $$$1$$$ to day $$$n$$$. The forest can be divided into $$$m$$$ areas, numbered from $$$1$$$ to $$$m$$$. He'll use the cameras in the following way:

- On every odd day ($$$1$$$-st, $$$3$$$-rd, $$$5$$$-th, ...), bring the red camera to the forest and record a video for $$$2$$$ days.
- On every even day ($$$2$$$-nd, $$$4$$$-th, $$$6$$$-th, ...), bring the blue camera to the forest and record a video for $$$2$$$ days.
- If he starts recording on the $$$n$$$-th day with one of the cameras, the camera records for only one day.

Each camera can observe $$$k$$$ consecutive areas of the forest. For example, if $$$m=5$$$ and $$$k=3$$$, he can put a camera to observe one of these three ranges of areas for two days: $$$[1,3]$$$, $$$[2,4]$$$, and $$$[3,5]$$$.

Gildong got information about how many animals will be seen in each area each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for $$$n$$$ days. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once.

Input format:
The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n \le 50$$$, $$$1 \le m \le 2 \cdot 10^4$$$, $$$1 \le k \le min(m,20)$$$) – the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.

Next $$$n$$$ lines contain $$$m$$$ integers each. The $$$j$$$-th integer in the $$$i+1$$$-st line is the number of animals that can be seen on the $$$i$$$-th day in the $$$j$$$-th area. Each number of animals is between $$$0$$$ and $$$1000$$$, inclusive.

Output format:
Print one integer – the maximum number of animals that can be observed.

Constraints:
Time limit: 3.0 s
Memory limit: 512.0 MB

Note:
The optimal way to observe animals in the four examples are as follows:

Example 1:

Example 2:

Example 3:

Example 4:

Example input:
4 5 2
0 2 1 1 0
0 0 3 1 2
1 0 4 3 1
3 3 0 0 4

Example output:
25

Example input:
3 3 1
1 2 3
4 5 6
7 8 9

Example output:
31

Editorial:
For simplicity, we'll assume that there is the $$$n+1$$$-st day when no animals appear at all. Let's say $$$animal[i][j]$$$ is the number of animals appearing in the $$$j$$$-th area on the $$$i$$$-th day.
Let's define $$$DP[i][j]$$$ ($$$1 \le i \le n, 1 \le j \le m - k + 1$$$) as the maximum number of animals that can be observed in total since day $$$1$$$, if Gildong puts a camera on the $$$i$$$-th day to observe area $$$[j, j+k-1]$$$ in day $$$i$$$ and $$$i+1$$$. Obviously, $$$DP[1][j]$$$ is the sum of the number of animals in area $$$[j, j+k-1]$$$ in day $$$1$$$ and day $$$2$$$. Now, for each day since day $$$2$$$, let's find three values to determine $$$DP[i][j]$$$, which is the maximum among them.
• For all $$$x$$$ ($$$1 \le x \le j-k$$$), maximum of $$$DP[i-1][x]$$$ plus $$$sum(animal[i \ldots i+1][j \ldots j+k-1])$$$.
• For all $$$y$$$ ($$$j+k \le y \le m-k+1$$$), maximum of $$$DP[i-1][y]$$$ plus $$$sum(animal[i \ldots i+1][j \ldots j+k-1])$$$.
• For all $$$z$$$ ($$$j-k+1 \le z \le j+k-1$$$), maximum of $$$DP[i-1][z]$$$ plus $$$sum(animal[i \ldots i+1][j \ldots j+k-1])$$$ minus the sum of animals in the intersected area on the $$$i$$$-th day.
The summation parts can be calculated in $$$O(1)$$$ time by prefix sum technique after $$$O(nm)$$$ pre-processing. For the ones that have no intersection, we can pre-calculate the prefix max and suffix max for the values in $$$DP[i-1]$$$ in $$$O(m)$$$ time for each day, therefore finding it in $$$O(1)$$$ as well.
Since $$$k$$$ is small in this problem, we can naively check all cases when there are one or more intersected areas. For each area we need to check $$$O(k)$$$ cases each in $$$O(1)$$$ time, so the problem can be solved in $$$O(nmk)$$$ time in total.