Pagini recente » Cod sursa (job #3246883) | Cod sursa (job #453238) | Cod sursa (job #2076599) | Cod sursa (job #1791237) | Cod sursa (job #1592612)
#include<bits/stdc++.h>
using namespace std;
ofstream fout("flip.out");
ifstream fin("flip.in");
int st[100],n,m;
long mat[17][17],mat1[17][17];
long long int Smax=0LL, S=0LL;
void flip()
{
int l,j;
for (l=1;l<=n;l++)
for (j=1;j<=m;j++)
if (st[j]==1)
mat1[l][j]*=(-1);
}
void fliplinie()
{
int l,j;
for (l=1;l<=n;l++)
{
S=0;
for (j=1;j<=m;j++)
S=S+mat1[l][j];
if (S<0)
for (j=1;j<=m;j++)
mat1[l][j]*=(-1);
}
}
void matcpy()
{
for (int l=1;l<=n;l++)
for (int j=1;j<=m;j++)
mat1[l][j]=mat[l][j];
}
void rezolva()
{
matcpy();
flip();
fliplinie();
S=0;
for (int l=1;l<=n;l++)
for (int j=1;j<=m;j++)
S=S+mat1[l][j];
if (S>Smax)
Smax=S;
}
void back(int k)
{
int i;
for (i=0;i<=1;i++)
{
st[k]=i;
if (k==m)
rezolva();
else
back(k+1);
}
}
int main()
{
Smax=0;
fin>>n>>m;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
fin>>mat[i][j];
back(1);
fout<<Smax;
return 0;
}