Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1005;
- int n, m, k;
- int mat[maxn][maxn];
- int dp[1 << 5][maxn];
- int rec(vector<int> rows, int bitmask, int at) {
- if(dp[bitmask][at] != -1) {
- return dp[bitmask][at];
- }
- int res = 0;
- for(int i = at + 1; i < m; i++) {
- bool ok = true;
- int sum = 0;
- for(int j = 0; j < rows.size(); j++) {
- if(mat[rows[j]][at] > mat[rows[j]][i]) {
- ok = false;
- break;
- }
- sum += mat[rows[j]][i];
- }
- if(ok) {
- res = max(res, rec(rows, bitmask, i) + sum);
- }
- }
- return dp[bitmask][at] = res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin >> n >> m >> k;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- }
- }
- int res = 0;
- vector<int> v;
- memset(dp, -1, sizeof dp);
- for(int bitmask = 0; bitmask < (1 << n); bitmask++) {
- if(__builtin_popcount(bitmask) == k) {
- vector<int> rows;
- for(int i = 0; i < n; i++) {
- if(bitmask & (1 << i)) {
- rows.push_back(i);
- }
- }
- for(int i = 0; i < m; i++) {
- int sum = 0;
- for(int j = 0; j < rows.size(); j++) {
- sum += mat[rows[j]][i];
- }
- int tmp_res = rec(rows, bitmask, i) + sum;
- if(tmp_res > res) {
- res = tmp_res;
- v = rows;
- }
- else if(tmp_res == res) {
- v = min(v, rows);
- }
- }
- }
- }
- cout << res << endl;
- for(int i = 0; i < v.size(); i++) {
- cout << v[i] + 1 << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement