AtCoder Regular Contest 133
C
Row Column Sums
AtCoder
⌀ 1583
原题
← BD →
翻译中…

Problem Statement

翻译中…

We have a grid with $H$ rows and $W$ columns.

翻译中…

Snuke is going to write in each square an integer between $0$ and $K-1$ (inclusive). Here, the conditions below must be satisfied.

翻译中…
  • For each $1 \leq i \leq H$, the sum modulo $K$ of the integers written in the $i$-th row is $A_i$.
  • For each $1 \leq i \leq W$, the sum modulo $K$ of the integers written in the $i$-th column is $B_i$.
翻译中…

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

翻译中…
  • $1 \leq H,W \leq 200000$
  • $2 \leq K \leq 200000$
  • $0 \leq A_i \leq K-1$
  • $0 \leq B_i \leq K-1$
  • All values in input are integers.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format:

翻译中…

H W K
A_1 A_2 \cdots A_H
B_1 B_2 \cdots B_W

翻译中…

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.

翻译中…

样例

Input #1
2 4 3
0 2
1 2 2 0
Output #1
11
The following should be written. We can see that the conditions are satisfied. For example, the sum of the integers in the $1$-st row is $6$, which modulo $K(=3)$ is $A_1(=0)$. The sum of the integers written here is $11$, which is the maximum possible value.
Input #2
3 3 4
0 1 2
1 2 3
Output #2
-1
题目信息
题号
arc133_c
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。