Pagini recente » Cod sursa (job #144502) | Cod sursa (job #132886) | Cod sursa (job #2543625) | Cod sursa (job #2061434) | Cod sursa (job #2434401)
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <cstdio>
#include <memory>
#include <string>
using namespace std;
int A[16][16];
int B[16][16];
int sums[16];
int n, m;
int abs(int x)
{
return (x > 0) ? x : -x;
}
void read()
{
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
cin >> A[i][j];
}
}
void initB(int x)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
int flipCol = ((1 << j) & x) ? (-1) : 1;
B[i][j] = A[i][j] * flipCol;
}
}
}
int bestSum()
{
int sum = 0;
for (int i = 0; i < n; ++i)
{
int sumLine = 0;
for (int j = 0; j < m; ++j)
{
sumLine += B[i][j];
}
sum += abs(sumLine);
}
return sum;
}
int maxSum()
{
int maxim = -(1 << 30);
for (int i = 0; i < 1 << m; ++i)
{
initB(i);
int candidate = bestSum();
if (candidate > maxim)
maxim = candidate;
}
return maxim;
}
int main()
{
freopen("flip.in", "r", stdin);
freopen("flip.out", "w", stdout);
read();
cout << maxSum();
return 0;
}