Buying items
Ad Hoc
Algorithms
Brute Force search
Dijkstra's algorithm
Graphs
Medium
Shortest path algorithm
簡単 普通
68.0% success
5,556 attempts
3s time
256MB memory
1024KB max code

There are n different types of objects. You want to buy atleast one instance of each type of object. There are m people , each of them sells one set of objects at a certain price (price is for the set).
Each of these m people sells either nothing or all the objects in the set he has. Information of these m sellers is represented by matrix M of size \(m * n \) . if \(M_{i,j} = 1\), it means that \(i^{th}\) seller has one \(j^{th}\) type of object in his set, otherwise \(M_{i,j} = 0\) . The \(i^{th}\) seller will sell set of all his objects at price \(C_i\).
You want to buy at least one object of each type (it is guaranteed that it is always possible to do so). Find the minimal cost needed to pay to achieve it.

\(\textbf{Input}\)

The first line contains two integers \(n, \; m\) separated by space. (\(1 \le n \times m \le 500\)).

The \((i + 1)^{th}\) (\(1 \le i \le m\)) line contains \((n + 1)\) integers - \(M_{i,1}, \; M_{i,2}, \; M_{i,3}, \dots, M_{i,n}, \; C_i\). (\(0 \le M_{i,j} \le 1, 1 \le C_i \le 10^9\)).

\(\textbf{Output}\)

Output the minimal cost you should pay to buy at least one object of each type.

Examples
Input
3 3
0 1 1 2
1 0 0 3
1 1 1 9
Output
5
Explanation

Buy from the 1 and 2 seller with cost \(2 + 3 = 5\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading Editor...