Hide

Problem Y
Tomography

Binary tomography deals with the problem of reconstructing binary images from a small number of projections. One of its most basic problems is to construct a binary ({0,1}-valued) matrix with given row and column sums. This is not always possible and your task is to determine when it is.

Input

The first line of input contains two numbers 1m,n1000, the number of rows and columns of the matrix. The next line contains m numbers 0rin – the sum of each row in the matrix. The third line contains n numbers 0cjm – the sum of each column in the matrix.

Output

Output “Yes” if there exists an m-by-n matrix A, with each element either beeing 0 or 1, such that

j=1nAi,j=rii{1,2,,m} and i=1mAi,j=cjj{1,2,,n}.

Otherwise output “No”.

Example

The figure below illustrates a matrix with the row and column sums of sample input 1.

\includegraphics[width=4cm]{sample1.png}
Sample Input 1 Sample Output 1
3 4
2 3 2
1 1 3 2
Yes
Sample Input 2 Sample Output 2
3 3
0 0 3
0 0 3
No
Hide

Please log in to submit a solution to this problem

Log in