Pagini recente » Cod sursa (job #103028) | Cod sursa (job #801724) | Cod sursa (job #2195645) | Cod sursa (job #2297428) | Cod sursa (job #1867183)
#include <fstream>
using namespace std;
ifstream f("flip.in");
ofstream g("flip.out");
int a,b, v[10000][10000];
int sum(int line, int switcher){
int sume=0;
for(int i=1; i<=b; i++){
sume+=switcher*v[line][i];
}
return sume;
}
int sum2(int line, int switcher){
int sume=0;
for(int i=1; i<=a; i++){
sume+=switcher*v[i][line];
}
return sume;
}
void switchit(int line, int switcher){
//g<<"comut "<<line<<'\n';
for(int i=1; i<=b; i++){
v[line][i]=switcher*v[line][i];
}
}
void switchit2(int line, int switcher){
//g<<"comut "<<line<<'\n';
for(int i=1; i<=a; i++){
v[i][line]=switcher*v[i][line];
}
}
int doit(){
int S=0;
for(int i=1; i<=b; i++){
S=0;
if(sum2(i, 1) >= sum2(i, -1))
S += sum2(i, 1);
else
S += sum2(i, -1), switchit2(i, -1);
}
for(int i=1; i<=a; i++){
S=0;
if(sum(i, 1) >= sum(i, -1))
S += sum(i, 1);
else
S += sum(i, -1), switchit(i, -1);
}
S=0;
for(int i=1; i<=a; i++){
S += sum(i, 1);
}
return S;
}
int main()
{
f>>a>>b;
for(int i=1; i<=a; i++)
for(int j=1; j<=b; j++){
f>>v[i][j];
}
int s1 = doit();
int s2 = doit();
while(s2>s1){
s1=doit();
}
g<<s1;
f.close();
g.close();
return 0;
}