Pagini recente » Cod sursa (job #2016926) | Cod sursa (job #435843) | Cod sursa (job #2069074) | Cod sursa (job #1639092) | Cod sursa (job #2654769)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
ifstream in("textin.txt");
ofstream out("textout.txt");
int n, m, mat[20][20], maxi;
void flipLine(int l) {
for (int i = 0; i < m; i++)mat[l][i] *= -1;
}
void flipColumn(int c) {
for (int i = 0; i < n; i++)mat[i][c] *= -1;
}
void greedy() {
int sum = 0, ps = 0;
for (int i = 0; i < m; ++i) {
ps = 0;
for (int j = 0; j < n; ++j) {
ps += mat[j][i];
}
sum += (ps > 0) ? ps : -ps;
}
if (sum > maxi) {
maxi = sum;
}
}
void back(const int l) {
if (l == n) {
greedy();
}
else {
back(l + 1);
flipLine(l);
back(l + 1);
flipLine(l);
}
}
int main() {
ios::sync_with_stdio(false);
in.tie(NULL), out.tie(NULL);
in >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
in >> mat[i][j];
}
}
back(0);
out << maxi;
return 0;
}