CF1913E.Matrix Problem
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a matrix a , consisting of n rows by m columns. Each element of the matrix is equal to 0 or 1 .
You can perform the following operation any number of times (possibly zero): choose an element of the matrix and replace it with either 0 or 1 .
You are also given two arrays A and B (of length n and m respectively). After you perform the operations, the matrix should satisfy the following conditions:
- the number of ones in the i -th row of the matrix should be exactly Ai for every i∈[1,n] .
- the number of ones in the j -th column of the matrix should be exactly Bj for every j∈[1,m] .
Calculate the minimum number of operations you have to perform.
输入格式
The first line contains two integers n and m ( 2≤n,m≤50 ).
Then n lines follow. The i -th of them contains m integers ai,1,ai,2,…,ai,m ( 0≤ai,j≤1 ).
The next line contains n integers A1,A2,…,An ( 0≤Ai≤m ).
The next line contains m integers B1,B2,…,Bm ( 0≤Bi≤n ).
输出格式
Print one integer — the minimum number of operations you have to perform, or -1 if it is impossible.
输入输出样例
输入#1
3 3 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
输出#1
3
输入#2
3 3 1 1 1 1 1 1 1 1 1 3 2 1 1 2 3
输出#2
3
输入#3
2 2 0 0 0 0 1 2 0 1
输出#3
-1