atcoder#ARC133C. [ARC133C] Row Column Sums
[ARC133C] Row Column Sums
Score : points
Problem Statement
We have a grid with rows and columns.
Snuke is going to write in each square an integer between and (inclusive). Here, the conditions below must be satisfied.
- For each , the sum modulo of the integers written in the -th row is .
- For each , the sum modulo of the integers written in the -th column is .
Determine whether it is possible to write integers in the squares to satisfy the conditions. If it is possible, also find the maximum possible sum of the integers written.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
If it is impossible to write integers in the squares to satisfy the conditions, print -1
.
If it is possible, print the maximum possible sum of the integers written.
2 4 3
0 2
1 2 2 0
11
The following should be written.
-----------------
| 2 | 0 | 2 | 2 |
-----------------
| 2 | 2 | 0 | 1 |
-----------------
We can see that the conditions are satisfied. For example, the sum of the integers in the -st row is , which modulo is .
The sum of the integers written here is , which is the maximum possible value.
3 3 4
0 1 2
1 2 3
-1