Cod sursa(job #3338417)

Utilizator kojoCojocaru Aurelian kojo Data 3 februarie 2026 00:17:01
Problema Jocul Flip Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#include <iostream>

using namespace std;

ifstream f("flip.in");
ofstream g("flip.out");

long a[17][17], b[17], k = 0;
int m, n;

long sum_row(int x)
{
	long s = 0;
	for (int i = 1; i <= n; i++)
	{		
			s += a[x][i];		
	}	
	return s;
}
long sum_col(int x)
{
	long s = 0;
	for (int i = 1; i <= m; i++)
	{
		s += a[i][x];
	}
	return s;
}



void maneta_row(int x)
{
	for (int i = 1; i <= n; i++)
	{
		a[x][i] *= -1;
	}	
}

void maneta_col(int x)
{
	for (int i = 1; i <= m; i++)
	{
		a[i][x] *= -1;
	}
}

int main()
{
	f >> m >> n;
	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= n; j++)
			f >> a[i][j];

	int gasit = 1;

	while (gasit == 1)
	{
		gasit = 0;
		for (int i = 1; i <= m; i++)
		{
			int ss = sum_row(i);
			if (ss < -1 * ss)
			{
				maneta_row(i);
				gasit = 1;
			}
			
		}
		for (int i = 1; i <= n; i++)
		{
			int ss = sum_col(i);
			if (ss < -1 * ss)
			{
				maneta_col(i);
				gasit = 1;
			}
		}

	}

	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			k += a[i][j];
			//cout << a[i][j]<<' ';
		}
		//cout << '\n';
	}

	g << k;
	return 0;
}