题意
传送门 NC 200190
题解
枚举行状态,求剩余列可以取到的的最大值,更新答案即可。
#include <bits/stdc++.h>
using namespace std;
#define maxn 16
typedef pair<int, int> P;
int csum[maxn], tsum[maxn];
int mat[maxn][maxn];
int main()
{
int n, m, k, sum = 0;
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &mat[i][j]);
csum[j] += mat[i][j];
sum += mat[i][j];
}
}
if (k >= n + m)
{
printf("%d\n", sum);
return 0;
}
int res = 0;
for (int i = 0; i < (1 << n); i++)
{
int tmp = i, cnt = 0, sum = 0;
while (tmp)
{
if (tmp & 1)
++cnt;
tmp >>= 1;
}
if (cnt > k || k - cnt > m)
continue;
memcpy(tsum, csum, sizeof(csum));
for (int x = 0; x < n; x++)
{
if (1 & (i >> x))
{
for (int y = 0; y < m; y++)
{
sum += mat[x][y];
tsum[y] -= mat[x][y];
}
}
}
sort(tsum, tsum + m);
int j = k - cnt;
for (int y = m - 1; y >= m - j; y--)
{
sum += tsum[y];
}
res = max(res, sum);
}
printf("%d\n", res);
return 0;
}