Pagini recente » Cod sursa (job #2388882) | Cod sursa (job #1468806)
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <cmath>
#include <climits>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;
int ans = -100000000;
void bt(int** t, int n, int m, int* p, int poz) {
if (poz > n) {
int** aux = new int[n + 1];
for (int i = 0; i <= n; i++)
aux[i] = new int[m + 1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
aux[i][j] = p[i] * t[i][j];
int total = 0;
for (int j = 1; j <= m; j++) {
int s = 0;
for (int i = 1; i <= n; i++) {
s += aux[i][j];
}
if (s < 0)
s = -s;
total += s;
}
if (total > ans)
ans = total;
return;
}
p[poz] = 1;
bt(t, n, m, p, poz + 1);
p[poz] = -1;
bt(t, n, m, p, poz + 1);
}
int main() {
freopen("flip.in", "r", stdin);
freopen("flip.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int** t = new int*[n + 1];
for (int i = 0; i <= n; i++)
t[i] = new int[m + 1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> t[i][j];
int* p = new int[n + 1];
bt(t, n, m, p, 1);
cout << ans;
return 0;
}