Cod sursa(job #3341028)

Utilizator serbanbBrindescu Serban serbanb Data 17 februarie 2026 17:33:22
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>

using namespace std;

ifstream fin("flip.in");
ofstream fout("flip.out");

int n,m;
int A[20][20];
int sRows[20];
int sMax = 0;

void read()
{
    fin >> n >> m;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < m; ++j){
            fin >> A[i][j];
            sRows[i] += A[i][j];
        }
    }
}

void backtracking(int aux[20], int c)
{
    if(c >= m){
        int s = 0;
        for(int i = 0; i < n; ++i){
            if(aux[i] < 0){
                s -= aux[i];
            }
            else{
                s += aux[i];
            }
        }
        if(s > sMax){
            sMax = s;
        }
        return;
    }
    int aux2[20];
    for(int i = 0; i < n; ++i){
        aux2[i] = aux[i];
    }
    backtracking(aux2, c + 1);
    for(int i = 0; i < n; ++i){
        aux2[i] -= (2 * A[i][c]);
    }
    backtracking(aux2, c + 1);
}

int main()
{
    read();
    backtracking(sRows, 0);
    fout << sMax;
    return 0;
}