Pagini recente » Cod sursa (job #1236788) | Cod sursa (job #1972054)
#include <stdio.h>
#include <iostream>
#define MAX 16
using namespace std;
int A[MAX][MAX], L[MAX], C[MAX], Max;
int total(int n, int m)
{
int suma, i, j;
suma = 0;
for(i = 0; i < n; i++)suma += L[i];
return suma;
}
void bkt(int n, int m, int k)
{
int suma;
if(k == n)
{
suma = total(n, m);
if(Max < suma)Max = suma;
}
else
{
L[k] *= -1;
bkt(n, m, k + 1);
L[k] *= -1;
bkt(n, m, k + 1);
}
}
int main()
{
FILE *fin, *fout;
int n, m, i, j;
fin = fopen("flip.in", "r");
fout = fopen("flip.out", "w");
fscanf(fin, "%d%d", &n, &m);
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
{
fscanf(fin, "%d", &A[i][j]);
C[j] += A[i][j];
}
for(i = 0; i < m; i++)
if(C[i] < 0)
for(j = 0; j < n; j++)A[j][i] *= -1;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
L[i] += A[i][j];
bkt(n, m, 0);
fprintf(fout, "%d", Max);
fclose( fin );
fclose( fout );
return 0;
}