Cod sursa(job #540634)

Utilizator Rares95Rares Arnautu Rares95 Data 24 februarie 2011 09:48:47
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
# include <cstdio>
# include <algorithm>
  using namespace std;
	  int n, m, i, j, a[17][17], S, smax, x[100000], k;
		
		void rezolva ()
		{ int S = 0, s, i, j;
			for (i = 1; i <= n; ++i)
			  { s = 0;
					for (j = 1; j <= m; ++j)
			      if (x[j]) s -= a[i][j]; else s += a[i][j];
					if (s < 0) S -= s; else S += s;
				}
			if (S > smax) smax = S;
		}
		
		void back ()
		{k = 1; x[k] = -1;
			do
		  {	while (x[k] < 1)
			  {	x[k]++;
			  	if (k == m) rezolva (); else x[++k] = -1;
			  }
			  k--;
		  } while (k);
		}
		
		int main ()
		{ freopen ("flip.in", "rt", stdin);
		  freopen ("flip.out", "wt", stdout);
			scanf ("%d%d", &n, &m);
			for (i = 1; i <= n; ++i) for (j = 1; j <= m; ++j) scanf ("%d", &a[i][j]);
			back ();
			printf ("%d\n", smax);
		}