The ALVN squirrel and his friends Simon and Theodore have been affected by the new acorn crisis, so they set out in search of food. Fortunately, after some searching, they discovered a garden with rows of oak trees, each row containing oak trees. Each oak tree occupies a square plot of identical size. The oak trees are old and have large branches, so they produce acorns that can fall not only in the plot they occupy but also in adjacent plots. Each oak tree has an acorn production coefficient and will produce acorns based on the following distribution:
- Produces acorns in its own plot (center).
- A number of acorns land in the immediate outer ring (directly adjacent plots), as shown in the adjacent diagram.
- A number of acorns land in the cells of the second ring, and so on, for each outer ring.
This pattern continues up to the farthest ring. Each oak tree has at most rings, including its own plot, and the sequence is sorted in decreasing order such that the tree produces the most acorns in its own plot, and the number gradually decreases in more distant rings.
The rings are square and concentric and may be incomplete depending on the position of the oak tree in the garden.\
If only the group consisting of ALVN and his friends is in the garden, they choose the plot with the maximum number of acorns.\
If there are two groups of squirrels in the garden, they decide—so there are no disagreements—that both groups choose their own plots, following these rules:
- They can only eat from oak trees whose rings do not share any plots.
- They will try to maximize the total number of acorns they consume.
For example, in the adjacent image, if there are two groups of squirrels, they can sit on plots (1,1) and (4,2), since the rings of those trees (marked in green) only touch but do not share any plots. However, the squirrels cannot sit on plots (4,6) and (6,6) because their rings share plots at positions (5,5) and (5,6) (shown in red, including the shared ones).
We are given , the coefficients of each oak tree in the garden, , and the values , with the meaning described above.
Task
- Determine , the maximum number of acorns that ALVN’s group can consume when they are alone in the garden.
- Determine , the total number of acorns consumed by two groups of squirrels in the garden.
Input data
- Line 1 of
alvn.in
: a number (1 or 2) indicating the task. - Line 2: two integers and (garden size).
- Next lines: each with integers, the production coefficients of each oak tree.
- Line : the value (number of rings).
- Line : integers: .
Output data
The output file alvn.out
contains a single integer:
- If , output (task 1).
- If , output (task 2).
Constraints and clarifications
- for any
- (coefficient of each oak)
# | Points | Constraints |
---|---|---|
1 | 10 | , , |
2 | 35 | , no further restrictions |
3 | 20 | , |
4 | 35 | , no further restrictions |
Example 1
alvn.in
1
4 4
1 0 1 0
0 2 0 1
1 0 3 0
0 1 0 1
3
4 2 1
alvn.out
25
Explanation
Explanation:
The number of acorns in each cell is:
In plot , acorns come from the following trees and plots:
- :
- :
- :
- :
- :
- :
- :
- :
Total: acorns.
Example 2
alvn.in
2
4 4
1 0 1 0
0 2 0 1
1 0 3 0
0 1 0 1
2
2 1
alvn.out
11
Explanation
Explanation:
The squirrels will choose plots (1,3) and (4,2). The rings of these trees do not overlap.
Plots in the rings of the tree at are marked green, and those of the tree at are marked purple.
- Plot will have value 5
- Plot will have value 6
Total = 5 + 6 = 11 acorns.