# Cod sursa(job #112603)

Utilizator Data 6 decembrie 2007 09:14:19 Jocul Flip 10 fpc done Arhiva de probleme 1.68 kb
``````Var f,g:Text;
n,m,i,j,l,c,max,s,s1,s2:Longint;
a:array[1..17,1..17] of Longint;

Begin

Assign(f,'flip.in');ReSet(f);
Assign(g,'flip.out');ReWrite(g);

For i:=1 To n Do
For j:=1 To m Do

If m<n Then Begin
max:=0;

For i:=1 To m Do Begin
s1:=0;
s2:=0;
For j:=1 To n Do Begin
If a[j,i]<=0 Then s1:=s1+abs(a[j,i]);
If a[j,i]>0 Then s2:=s2+a[j,i]*(-1);
If s1+s2>max Then Begin
max:=s1+s2;
c:=i;
End;
End;
End;

max:=0;

For i:=1 To m Do Begin
s1:=0;
s2:=0;
For j:=1 To n Do Begin
If a[i,j]<0 Then s1:=s1+abs(a[i,j]);
If a[i,j]>0 Then s2:=s2+a[i,j]*(-1);
If s1+s2>max Then Begin
max:=s1+s2;
l:=i;
End;
End;
End;
End
Else Begin

max:=0;

For i:=1 To m Do Begin
s1:=0;
s2:=0;
For j:=1 To n Do Begin
If a[i,j]<0 Then s1:=s1+abs(a[i,j]);
If a[i,j]>0 Then s2:=s2+a[i,j]*(-1);
If s1+s2>max Then Begin
max:=s1+s2;
l:=i;
End;
End;
End;

max:=0;

For i:=1 To m Do Begin
s1:=0;
s2:=0;
For j:=1 To n Do Begin
If a[j,i]<=0 Then s1:=s1+abs(a[j,i]);
If a[j,i]>0 Then s2:=s2+a[j,i]*(-1);
If s1+s2>max Then Begin
max:=s1+s2;
c:=i;
End;
End;
End;
End;
s:=0;

For i:=1 To n Do
a[i,c]:=a[i,c]*(-1);

For i:=1 To m Do
a[l,i]:=a[l,i]*(-1);

a[l,c]:=a[l,c]*(-1);

For i:=1 To n Do
For j:=1 To m Do
s:=s+a[i,j];

WriteLn(g,s);

Close(f);
Close(g);

End.

``````