Pagini recente » Cod sursa (job #1450549) | Cod sursa (job #1925672) | Cod sursa (job #694064)
Cod sursa(job #694064)
#include <cstdio>
using namespace std;
int v[20][20];
int n,i,m,j;
long long crt,mx;
int x[20],y[20];
void check() {
int i,j;
crt=0;
for (i=1;i<=m;i++) for (j=1;j<=n;j++) {
if (x[i]&&y[j]) crt+=v[i][j];
if (x[i]&&!y[j]) crt-=v[i][j];
if (!x[i]&&y[j]) crt-=v[i][j];
if (!x[i]&&!y[j]) crt+=v[i][j];
}
if (crt>mx) mx=crt;
}
void permut2(int k) {
if (k<=n) {
y[k]=true;
permut2(k+1);
y[k]=false;
permut2(k+1);
}
else check();
}
void permut(int k) {
if (k<=n) {
x[k]=true;
permut(k+1);
x[k]=false;
permut(k+1);
}
else permut2(1);
}
int main() {
freopen("flip.in","r",stdin);
freopen("flip.out","w",stdout);
scanf("%d %d",&m,&n);
for (i=1;i<=m;i++) for (j=1;j<=n;j++) scanf("%d",&v[i][j]);
permut(1);
printf("%d",mx);
return 0;
}