Dice Grid
Simon has an unusually large amount of dice, owing to his love for the game Liar’s dice which requires just that. This has resulted in Simon inventing a large number of new games concerning dice. His newest one is called Dice Grid.
Dice Grid is a single-player game, where the goal is to maximize one’s score. It is played on a $N \times M$ grid of squares. At the start of the game, the player places a regular 6-sided die on the top-left square, and designates a bunch of squares at random as forbidden. The goal is to move the die to the bottom-right square, by rolling it down $N-1$ times and to the right $M-1$ times, without touching any of the forbidden squares. For every of the $N + M - 1$ squares that the dice is placed on, the score of that square is the number that shows on top of the die when it was placed on that square. The score of the game is the sum of the scores of all those squares.
What is the maximum score Simon can achieve, assuming he plays optimally?
When the die is placed on the starting square, the number $1$ is at the bottom, $6$ is at the top, $2$ is at the front of the die (meaning it would be at the bottom if the die was rolled to the next row), $4$ is on the right side of the die (meaning it would be at the bottom if the die was rolled to the next column), $3$ is to the left of the die, and $5$ is at the back of the die.
Input
The first line of input contains the three integers $N, M$ and $K$ – the number of rows and columns of the grid, and the number of forbidden squares, respectively. The next $K$ lines of input each contain two integers $r, c$ ($1 \le r \le N, 1 \le c \le M$), denoting that the square on row $r$, column $c$ is forbidden.
No square will be marked as forbidden twice, and neither the top-left nor the bottom-right corner will be marked as forbidden. It is guaranteed that the top-left and bottom-right squares do not coincide (i.e., either $N$ or $M$ is larger than $1$).
Output
Output a single line – the maximum score Simon can obtain. If it is impossible to roll the dice to the bottom-right corner without touching any forbidden squares, output “impossible”.
Scoring
Your solution will be tested on a set of test groups, each worth a number of points. To get the points for a test group you need to solve all test cases in the test group. Your final score will be the maximum score of a single submission.
Group |
Points |
Constraints |
1 |
5 |
$N = 1, 2 \le M \le 10^{15}, K = 0$ |
2 |
10 |
$N = 2, 1 \le M \le 10^5, K = 0$ |
3 |
10 |
$N = 2, 1 \le M \le 10^{15}, 0 \le K \le 50$ |
4 |
0 |
$1 \le N, M \le 10^2, 0 \le K \le 50$ |
5 |
15 |
$1 \le N, M \le 10^{15}, K = 0$ |
6 |
20 |
$1 \le N, M \le 10^{15}, 0 \le K \le 50$ |
7 |
30 |
$1 \le N, M \le 10^{15}, 0 \le K \le 100\, 000$ |
Sample Input 1 | Sample Output 1 |
---|---|
1 6 0 |
23 |
Sample Input 2 | Sample Output 2 |
---|---|
2 5 0 |
25 |
Sample Input 3 | Sample Output 3 |
---|---|
4 3 2 2 1 3 3 |
21 |
Sample Input 4 | Sample Output 4 |
---|---|
5 2 2 2 1 4 2 |
impossible |