Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <cmath>
- #include <map>
- #include <cstring>
- using namespace std;
- int n, m;
- int mat[105][16];
- int dp[105][1 << 15];
- int rec(int at, int bitmask) {
- if(__builtin_popcount(bitmask) == m) {
- return 0;
- }
- if(at == n) {
- return 1e9;
- }
- if(dp[at][bitmask] != -1) {
- return dp[at][bitmask];
- }
- int result = 1e9;
- for(int i = 0; i < m; i++) {
- if((bitmask & (1 << i)) == 0) {
- int new_mask = bitmask | (1 << i);
- result = min(result, rec(at + 1, new_mask) + mat[at][i]);
- }
- }
- result = min(result, rec(at + 1, bitmask));
- return dp[at][bitmask] = result;
- }
- int main() {
- cin >> n >> m;
- vector<int> cost(m, 0);
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- cost[j] += mat[i][j];
- }
- }
- memset(dp, -1, sizeof dp);
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- mat[i][j] = cost[j] - mat[i][j];
- }
- }
- cout << rec(0, 0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement