The only difference between this question and Big orange cat's puzzle III is the input constraints.
Based on his previous three experiences (hw4, lab4, mid1), Big Orange Cat has finally figured out which puzzle pieces to choose!
He has now completed 90% of the puzzle. The current puzzle can be represented as an n x m matrix A, with -1 indicating unfilled spaces.
Fortunately, he knows that the sum of numbers in each row should be approximately Si. However, there might be some error, so the actual sum could be in the range [Si - P, Si + P].
He now has k puzzle pieces B1 to Bk. Please tell him how many ways he can use pieces from B to fill the puzzle so that each row's sum falls within the specified range. Also, find the method with the smallest lexicographical order.
The lexicographical order for this puzzle starts from the top-left corner A1,1, then A1,2, and so on. After comparing the first row, move to the second row, following a left-to-right, top-to-bottom rule. For detailed examples, please refer to the sample output.
The first line of input contains four integers n, m, k, P, separated by spaces.
The second line contains k integers B1 to Bk, separated by spaces.
The next n lines each contain m integers Ai,1 to Ai,m.
The last line contains n integers representing S1 to Sn.
Subtasks
There are two types of output based on different situations:
"baganono"
(without quotes).Please remember to print "\n" at the end, and dont print space (" ") at the end of every line.