Target practice
Statement
There is an N×N board with one or more targets on it. A student is practicing shooting and has K bullets numbered 1 to K. The student fires K times in total and must fire the bullets in order from number 1 to number K.
For each shot the student picks one row among rows 1 to N and fires along it. The bullet starts at a position in the leftmost column (column 1) and flies horizontally from left to right, one cell at a time. Each bullet carries an attack power.
In the starting input, a position on the board that holds a target is marked with a natural number of 1 or more, and a position with no target is marked with the integer 0. The value at a target position is its initial health, meaning its current health at the start. When a bullet then touches a target, the current health drops by the attack power of the bullet. A bullet cannot pass through a target, and a bullet that touches a target disappears at once.
When a bullet brings the current health to 0 or below, the target disappears. You then gain score equal to that target's initial health. After that, for every position above, below, to the left of, and to the right of the vanished target that is an empty cell (a place with no target), a new target is created whose initial health is 1/4 of the vanished target's initial health. The new initial health is rounded down to a whole number, and if that value is 0 no new target is created.
In the starting state the initial health and the current health are the same for every target. A target whose value is 10 or more is a bonus target. A bonus target has no initial health of its own. The moment a bonus target is hit, the bullet and the bonus target disappear together regardless of the bullet's attack power, and the score written on the bonus target is added right away. A bonus target creates no new target after it disappears. The initial health of a normal target, one that is not a bonus target, is a natural number between 1 and 9.
Given the starting N×N board, write a program that finds the largest score that can be gained from K shots. All K shots may be fired from the same position, meaning the same row. The attack power of each bullet is a natural number between 1 and 100.
For example, consider N=5 and K=3. One example drawn as a table looks like this. Assume the attack powers of the bullets are 1, 5, 1 in order. There are 5 positions to fire from. The score starts at 0.
| Position 1 | 0 | 0 | 0 | 0 | 0 |
|---|---|---|---|---|---|
| Position 2 | 10 | 0 | 4 | 0 | 0 |
| Position 3 | 0 | 0 | 7 | 0 | 0 |
| Position 4 | 0 | 0 | 0 | 0 | 0 |
| Position 5 | 0 | 0 | 2 | 0 | 0 |
1. First, the attack power of the first bullet is 1. Firing from position 2 hits the bonus target, and the score becomes 10.
| Position 1 | 0 | 0 | 0 | 0 | 0 |
|---|---|---|---|---|---|
| Position 2 | 0 | 0 | 4 | 0 | 0 |
| Position 3 | 0 | 0 | 7 | 0 | 0 |
| Position 4 | 0 | 0 | 0 | 0 | 0 |
| Position 5 | 0 | 0 | 2 | 0 | 0 |
2. Next, the attack power of the second bullet is 5. Firing from position 2 hits a normal target, and the score becomes 14.
| Position 1 | 0 | 0 | 1 | 0 | 0 |
|---|---|---|---|---|---|
| Position 2 | 0 | 1 | 0 | 1 | 0 |
| Position 3 | 0 | 0 | 7 | 0 | 0 |
| Position 4 | 0 | 0 | 0 | 0 | 0 |
| Position 5 | 0 | 0 | 2 | 0 | 0 |
3. Next, the attack power of the third bullet is 1. Firing from position 1 hits a normal target, and the score becomes 15.
| Position 1 | 0 | 0 | 0 | 0 | 0 |
|---|---|---|---|---|---|
| Position 2 | 0 | 1 | 0 | 1 | 0 |
| Position 3 | 0 | 0 | 7 | 0 | 0 |
| Position 4 | 0 | 0 | 0 | 0 | 0 |
| Position 5 | 0 | 0 | 2 | 0 | 0 |
In this example the score is 15 when the shots are fired as above, and that is the largest value.
Input and Output Examples
Example 1
Input
5 3 0 0 0 0 0 10 0 4 0 0 0 0 7 0 0 0 0 0 0 0 0 0 2 0 0 1 5 1
Output
15
Example 2
Input
5 5 0 0 7 0 0 0 0 5 20 20 0 6 7 0 0 0 1 0 0 0 0 0 2 0 0 2 3 1 1 1
Output
46
Input
The first line gives the board size N as a natural number. N is a natural number between 2 and 8.
The second line gives the number of shots K as a natural number. K is a natural number between 1 and 5.
From the third line to the (N+2)th line, the N×N board is given. The values for each position are separated by spaces, and each value is an integer between 0 and 30.
The (N+3)th line gives the attack power of each bullet from number 1 to number K in order, separated by spaces, and each attack power is a natural number between 1 and 100.
Output
On the first line, print the largest score that can be gained from K shots, as a natural number.
Requirements
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 512MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 42 cases, and 5 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 42 cases to solve this.
Examples
5 public cases. Judging also uses hidden cases. You have to pass all 42 cases to solve this.
5 3 0 0 0 0 0 10 0 4 0 0 0 0 7 0 0 0 0 0 0 0 0 0 2 0 0 1 5 1
15
5 5 0 0 7 0 0 0 0 5 20 20 0 6 7 0 0 0 1 0 0 0 0 0 2 0 0 2 3 1 1 1
46
6 3 23 12 10 20 23 22 22 22 1 1 15 9 20 26 17 9 14 10 12 1 9 6 11 21 26 14 26 19 14 14 10 20 14 2 18 13 81 61 47
72
4 2 24 27 24 16 7 14 5 29 23 12 10 27 1 7 12 2 25 32
51
5 2 6 24 17 3 12 25 27 18 8 22 5 7 7 6 4 20 20 0 26 17 23 19 11 1 11 59 18
52
Notes
Background that helps with solving. The rules you have to follow are in the input and output specs and the requirements above.
This is problem 1 of mock contest 1 in the original.
This is the only problem whose original form uses standard input and output. The reference solution printed in the original is also a script that reads with input() and writes with print() rather than a solution function, and the test case files are split by line breaks.
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 5 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.