{"problem":{"name":"Row Column Sums","description":{"content":"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","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"arc133_c"},"statements":[{"statement_type":"Markdown","content":"We have a grid with $H$ rows and $W$ columns.\nSnuke is going to write in each square an integer between $0$ and $K-1$ (inclusive). Here, the conditions below must be satisfied.\n\n*   For each $1 \\leq i \\leq H$, the sum modulo $K$ of the integers written in the $i$\\-th row is $A_i$.\n*   For each $1 \\leq i \\leq W$, the sum modulo $K$ of the integers written in the $i$\\-th column is $B_i$.\n\nDetermine 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.\n\n## Constraints\n\n*   $1 \\leq H,W \\leq 200000$\n*   $2 \\leq K \\leq 200000$\n*   $0 \\leq A_i \\leq K-1$\n*   $0 \\leq B_i \\leq K-1$\n*   All values in input are integers.\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$H$ $W$ $K$\n$A_1$ $A_2$ $\\cdots$ $A_H$\n$B_1$ $B_2$ $\\cdots$ $B_W$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"arc133_c","tags":[],"sample_group":[["2 4 3\n0 2\n1 2 2 0","11\n\nThe following should be written.\n\n\\-----------------\n| 2 | 0 | 2 | 2 |\n-----------------\n| 2 | 2 | 0 | 1 |\n-----------------\n\nWe 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)$.\nThe sum of the integers written here is $11$, which is the maximum possible value."],["3 3 4\n0 1 2\n1 2 3","\\-1"]],"created_at":"2026-03-03 11:01:14"}}