Cod sursa(job #1038082)

Utilizator karla10Karla Marian karla10 Data 20 noiembrie 2013 23:18:57
Problema Jocul Flip Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#define in "flip.in"
#define out "flip.out"
#define Max_Size 18
  
std :: ifstream f(in);
std :: ofstream g(out);
  
int N, M, Best_Sum;
int A[Max_Size][Max_Size];
  
inline void Read_Data()
{
    f >> N >> M;
  
    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= M; ++j)
            f >> A[i][j];
}
  
inline void Solve()
{
    int sum_on_line, sum;
    short Sign[Max_Size];
  
    for(int i = 0; i < (1 << M); ++i)
    {
        for(int j = 1; j <= M; ++j)
            if((1 << j) & i)   Sign[j] = -1;
            else                Sign[j] = 1;
  
        sum = 0;
        for(int i = 1; i <= N; ++i)
        {
            sum_on_line = 0;
            for(int j = 1; j <= M; ++j)
                sum_on_line += Sign[j] * A[i][j];
  
            //Daca suma pe linia i e mai mica decat 0 o comut
            if(sum_on_line < 0)
                sum -= sum_on_line;
            else
                sum += sum_on_line;
  
            Best_Sum = std :: max(Best_Sum, sum);
        }
    }
}
  
int main()
{
    Read_Data();
    Solve();
  
    g << Best_Sum << '\n';
  
    g.close();
    return 0;
}