Pagini recente » Cod sursa (job #886479) | Cod sursa (job #3323240) | Cod sursa (job #1509459) | Cod sursa (job #2187606) | Cod sursa (job #3315057)
#include <iostream>
#include <fstream>
#include <cassert>
#include <algorithm>
int arr[17][17];
int main()
{
std::ifstream f("flip.in");
assert(f.is_open());
int n, m;
f >> n >> m;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
f >> arr[i][j];
}
}
f.close();
int permutations = (1 << n);
int biggest{0};
for(int mask = 0; mask < permutations; ++mask) {
for(int j = 0; j < n; ++j) {
if(mask & (1 << j)) {
for(int k = 0; k < m; ++k) {
arr[j][k] *= -1;
}
}
}
int sum = 0;
for(int k = 0; k < m; ++k) {
int colSum = 0;
for(int j = 0; j < n; ++j) {
colSum += arr[j][k];
}
sum += abs(colSum);
}
for(int j = 0; j < n; ++j) {
if(mask & (1 << j)) {
for(int k = 0; k < m; ++k) {
arr[j][k] *= -1;
}
}
}
biggest = std::max(sum, biggest);
}
std::ofstream o("flip.out");
o << biggest;
o.close();
return 0;
}