Pagini recente » Cod sursa (job #3283878) | Cod sursa (job #1924593) | Cod sursa (job #2070073) | Cod sursa (job #2532514) | Cod sursa (job #1343035)
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int NMAX = 7500, DMAX = 600;
int n, m, r, c, sum, smax, x[DMAX][DMAX], elim[NMAX], colSum[NMAX], linSum[NMAX];
int calc() {
int j, i, e, s, tempSum;
vector <int> tempCol, tempOrd;
tempCol.resize(m + 1);
tempOrd.resize(m + 1);
for(j = 1; j <= m; ++ j) {
s = colSum[j];
for(e = 1; e <= r; ++ e)
s -= x[elim[e]][j];
tempCol[j] = s;
}
sort(tempCol.begin() + 1, tempCol.end());
tempSum = sum;
for(e = 1; e <= r; ++ e)
tempSum -= linSum[elim[e]];
for(j = 1; j <= c; ++ j)
tempSum -= tempCol[j];
return tempSum;
}
void back (int k) {
for(int i = elim[k - 1] + 1; i <= n; ++ i) {
elim[k] = i;
if(k == r)
smax = max(smax, calc());
else
back(k + 1);
}
}
int main() {
freopen("elimin.in", "r", stdin);
freopen("elimin.out", "w", stdout);
int i, j;
scanf("%d%d%d%d", &n, &m, &r, &c);
if(n > m) {
swap(n, m);
swap(r, c);
for(i = 1; i <= m; ++ i)
for(j = 1; j <= n; ++ j) {
scanf("%d", &x[i][j]);
colSum[i] += x[i][j];
linSum[j] += x[i][j];
sum += x[i][j];
}
}
else {
for(i = 1; i <= n; ++ i)
for(j = 1; j <= m; ++ j) {
scanf("%d", &x[i][j]);
colSum[j] += x[i][j];
linSum[i] += x[i][j];
sum += x[i][j];
}
}
back(1);
printf("%d\n", smax);
return 0;
}