Cod sursa(job #487388)

Utilizator GodiesVlad Voicu Godies Data 24 septembrie 2010 23:13:51
Problema Jocul Flip Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <cstdio>
#include <cstdlib>
#include <iostream>

#define NMAX 17
#define DMAX 65536

using namespace std;

int main()
{
	FILE *f, *g;
	f = fopen("flip.in", "rt");
	g = fopen("flip.out", "wt");
	int i, j, k, l, n, h, m, x, sum, total_sum, max;
	int a[NMAX][NMAX];
	fscanf (f, "%d%d", &n, &m);
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++)
			fscanf (f, "%d", &a[i][j]);
	max = -1;
	for (h = 0; h <= DMAX; h++) {
		x = h;
		for (i = 0; i < n; i++) {
			total_sum = 0;
			if ((x & (1 << i)) > 0) {
				for (k = 0; k < m; k++) {
					a[i][k] = -a[i][k];
				}
			}
			for (j = 0; j < m; j++) {
				sum = 0;
				for (l = 0; l < n; l++) {
					sum += a[l][j];
				}
				if (sum < 0) {
					total_sum += -sum;
				}
				else
					total_sum += sum;
			}
		}
		if (total_sum > max) {
			max = total_sum;
		}
	}
	cout<<max;
	fclose(f);
	fclose(g);
	return 0;
}