Pagini recente » Cod sursa (job #3337900) | Cod sursa (job #3338709) | Cod sursa (job #3338417) | Cod sursa (job #3338504) | Cod sursa (job #3338412)
#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 suma_poz(int x, int y)
{
long s = 0;
for (int i = 1; i <= m; i++)
{
if (a[x][i] > 0)
{
s += a[x][i];
}
}
for (int i = 1; i <= n; i++)
{
if (a[i][y] > 0)
{
s += a[i][y];
}
}
if (a[x][y] > 0)
{
s -= a[x][y];
}
return s;
}
long suma_neg(int x, int y)
{
long s = 0;
for (int i = 1; i <= m; i++)
{
if (a[x][i] < 0)
{
s += a[x][i];
}
}
for (int i = 1; i <= n; i++)
{
if (a[i][y] < 0)
{
s += a[i][y];
}
}
if (a[x][y] < 0)
{
s -= a[x][y];
}
return s;
}
void maneta(int x, int y)
{
for (int i = 1; i <= n; i++)
{
a[x][i] *= -1;
}
for (int i = 1; i <= m; i++)
{
a[i][y] *= -1;
}
a[x][y] *= -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 <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (suma_poz(i, j) < -1 * suma_neg(i, j))
{
maneta(i, j);
gasit = 1;
}
}
}
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
k += a[i][j];
}
}
g << k;
return 0;
}