Pagini recente » Cod sursa (job #2280966) | Cod sursa (job #2646874) | Cod sursa (job #269764) | Cod sursa (job #2740372) | Cod sursa (job #1179632)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n,m, currentSum = 0, hasNegative = false;
ifstream in;
ofstream out;
in.open("flip.in");
out.open("flip.out");
in>>n;
in>>m;
int mat[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
in>>mat[i][j];
currentSum += mat[i][j];
if (mat[i][j] < 0) {
hasNegative = true;
}
}
if (hasNegative == true) {
for (int j = 0; j < m; j++) {
int nrNegatives = 0, normalSum = 0, flipSum = 0;
for (int i = 0; i < n; i++) {
normalSum += mat[i][j];
flipSum += (-mat[i][j]);
if (mat[i][j] < 0) {
nrNegatives++;
}
}
if (nrNegatives > n) {
currentSum -= normalSum;
currentSum += flipSum;
for (int i = 0; i < n; i++) {
mat[i][j] = -mat[i][j];
}
}
}
for (int i = 0; i < n; i++) {
int nrNegatives = 0, normalSum = 0, flipSum = 0;
for (int j = 0; j < m; j++) {
normalSum += mat[i][j];
flipSum += (-mat[i][j]);
if (mat[i][j] < 0) {
nrNegatives++;
}
}
if (nrNegatives > m) {
currentSum -= normalSum;
currentSum += flipSum;
for (int j = 0; j < m; j++) {
mat[i][j] = -mat[i][j];
}
}
}
out<<currentSum;
}
else {
out<<currentSum;
}
in.close();
out.close();
}