Alvn

Time limit: 1s Memory limit: 32MB Input: alvn.in Output: alvn.out

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 NN rows of oak trees, each row containing MM 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 CC and will produce acorns based on the following distribution:

  • Produces x1Cx_1 \cdot C acorns in its own plot (center).
  • A number x2Cx_2 \cdot C of acorns land in the immediate outer ring (directly adjacent plots), as shown in the adjacent diagram.
  • A number x3Cx_3 \cdot C 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 kk rings, including its own plot, and the sequence x1,x2,xkx_1, x_2, \dots x_k 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 N,MN, M, the coefficients of each oak tree in the garden, kk, and the values x1,x2,xkx_1, x_2, \dots x_k, with the meaning described above.

Task

  1. Determine SS, the maximum number of acorns that ALVN’s group can consume when they are alone in the garden.
  2. Determine TT, the total number of acorns consumed by two groups of squirrels in the garden.

Input data

  • Line 1 of alvn.in: a number pp (1 or 2) indicating the task.
  • Line 2: two integers NN and MM (garden size).
  • Next NN lines: each with MM integers, the production coefficients of each oak tree.
  • Line N+3N+3: the value kk (number of rings).
  • Line N+4N+4: kk integers: x1,x2,,xkx_1, x_2, \dots, x_k.

Output data

The output file alvn.out contains a single integer:

  • If p=1p = 1, output SS (task 1).
  • If p=2p = 2, output TT (task 2).

Constraints and clarifications

  • 1N,M7001 \leq N, M \leq 700
  • 1Kmin(N,M,200)1 \leq K \leq \min(N, M, 200)
  • 0xk1000 \leq x_k \leq 100 for any kk
  • 0C1000 \leq C \leq 100 (coefficient of each oak)
  • x1x2xkx_1 \geq x_2 \geq \dots \geq x_k
# Points Constraints
1 10 p=1p=1, N,M100N, M \leq 100, K10K \leq 10
2 35 p=1p=1, no further restrictions
3 20 p=2p=2, K10K \leq 10
4 35 p=2p=2, 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:

131315913231816151825149161414\begin{matrix} 13 & 13 & 15 & 9 \\ 13 & 23 & 18 & 16 \\ 15 & 18 & 25 & 14 \\ 9 & 16 & 14 & 14 \\ \end{matrix}

In plot (3,3)(3, 3), acorns come from the following trees and plots:

  • (3,3)(3, 3): 34=123 \cdot 4 = 12
  • (2,2)(2, 2): 22=42 \cdot 2 = 4
  • (2,4)(2, 4): 21=22 \cdot 1 = 2
  • (4,2)(4, 2): 21=22 \cdot 1 = 2
  • (4,4)(4, 4): 21=22 \cdot 1 = 2
  • (1,1)(1, 1): 11=11 \cdot 1 = 1
  • (1,3)(1, 3): 11=11 \cdot 1 = 1
  • (3,1)(3, 1): 11=11 \cdot 1 = 1

Total: 12+4+2+2+2+1+1+1=2512 + 4 + 2 + 2 + 2 + 1 + 1 + 1 = 25 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 (1,3)(1, 3) are marked green, and those of the tree at (4,2)(4, 2) are marked purple.

  • Plot (1,3)(1,3) will have value 5
  • Plot (4,2)(4,2) will have value 6

Total = 5 + 6 = 11 acorns.

Log in or sign up to be able to send submissions!