Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <cstring>
- using namespace std;
- typedef long long ll;
- int n, m, k;
- int mat[6][1001];
- int dp[1 << 5][1001];
- int rec(vector<int> idx, int bitmask, int at) {
- if(dp[bitmask][at] != -1) {
- return dp[bitmask][at];
- }
- int res = 0;
- for(int j = at + 1; j < m; j++) {
- bool ok = true;
- int sum = 0;
- for(int i = 0; i < idx.size(); i++) {
- if(mat[idx[i]][at] > mat[idx[i]][j]) {
- ok = false;
- break;
- }
- sum += mat[idx[i]][j];
- }
- if(ok) {
- res = max(res, rec(idx, bitmask, j) + sum);
- }
- }
- return dp[bitmask][at] = res;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- 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;
- memset(dp, -1, sizeof dp);
- vector<int> v_res;
- for(int bitmask = 0; bitmask < (1 << n); bitmask++) {
- vector<int> idx;
- for(int i = 0; i < n; i++) {
- if(bitmask & (1 << i)) {
- idx.push_back(i);
- }
- }
- if((int) idx.size() == k) {
- for(int j = 0; j < m; j++) {
- int sum = 0;
- for(int i = 0; i < k; i++) {
- sum += mat[idx[i]][j];
- }
- int tmp = rec(idx, bitmask, j) + sum;
- if(res < tmp) {
- res = tmp;
- v_res = idx;
- }
- else if(res == tmp) {
- v_res = min(v_res, idx);
- }
- }
- }
- }
- cout << res << endl;
- for(int i = 0; i < v_res.size(); i++) {
- cout << v_res[i] + 1 << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement