Pagini recente » Cod sursa (job #917732) | Cod sursa (job #1117473) | Cod sursa (job #2193311) | Cod sursa (job #1405626) | Cod sursa (job #1268395)
#include<bits/stdc++.h>
using namespace std;
int n, m, a[17][17];
long long maxim = -1000000;
int takec[17], takel[17];
long long calculez() {
long long s = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
s += ((long long)a[i][j] * takec[j] * takel[i]);
// cout << s << " ";
return s;
}
void bktrc(int x) {
if(x == m) {
if( calculez() > maxim )
maxim = calculez();
} else {
takec[x] = 1;
bktrc(x + 1);
takec[x] = -1;
bktrc(x + 1);
}
}
void bktrl(int x) {
if(x == n) {
bktrc(0);
} else {
takel[x] = 1;
bktrl(x + 1);
takel[x] = -1;
bktrl(x + 1);
}
}
int main()
{
FILE *fin = fopen("flip.in", "r");
FILE *fout = fopen("flip.out", "w");
fscanf(fin, "%d %d", &n, &m);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
fscanf(fin, "%d", &a[i][j]);
bktrl(0);
fprintf(fout, "%lld", maxim);
}