You have a grid n x n with n rows and n columns. For every column j you are given a number Cj and for every row i you are given a number Ri. You need to mark some points on the grid, in this way:
- the number of marked points in every row is at most Ri;
- the number of marked points in every column is at most Cj;
- you mark the maximum number of points that satify the last two constraints and return this number of points.
The input is: n (dimension of the grid); the sequence of Ri and the sequence of Cj.
for example in this grid the return is 34 example
Find an algorithm in linear time: O(n) or O(n log(n)) with demonstration. I have found a solution with Max-Flow alg. but the complexity is too high.