Cod sursa(job #2457510)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 17 septembrie 2019 22:08:59
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.26 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream f("flip.in");
ofstream g("flip.out");

const int NMAX = 20;

int ans = -2e9;

int N, M, A[NMAX][NMAX], B[NMAX][NMAX];

bool Sel[NMAX];

static inline void Read ()
{
    f.tie(NULL);

    f >> N >> M;

    for(int i = 1; i <= N; ++i)
        for(int j = 1; j <= M; ++j)
            f >> A[i][j];

    return;
}

int main()
{
    Read();

    for(int i = 0; i < (1 << N); ++i)
    {
        memset(Sel, 0, sizeof(Sel));

        for(int j = 0; j < N; ++j)
            if(i & (1 << j))
                Sel[j + 1] = true;

        int Best = 0;

        for(int l = 1; l <= N; ++l)
            for(int c = 1; c <= M; ++c)
                B[l][c] = A[l][c];

        for(int l = 1; l <= N; ++l)
            if(Sel[l])
                for(int c = 1; c <= M; ++c)
                    B[l][c] = -A[l][c];

        for(int c = 1; c <= M; ++c)
        {
            int Best1 = 0, Best2 = 0;

            for(int l = 1; l <= N; ++l)
            {
                Best1 += B[l][c];

                Best2 += -B[l][c];
            }

            Best += max(Best1, Best2);
        }

        ans = max(ans, Best);
    }

    g << ans << '\n';

    return 0;
}