Cod sursa(job #3315057)

Utilizator tudorbconstantinBordei Tudor-Constantin tudorbconstantin Data 12 octombrie 2025 10:08:56
Problema Jocul Flip Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <cassert>
#include <algorithm>

int arr[17][17];

int main()
{
    std::ifstream f("flip.in");
    assert(f.is_open());

    int n, m;
    f >> n >> m;

    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            f >> arr[i][j];
        }
    }
    f.close();

    int permutations = (1 << n);
    
    int biggest{0};
    for(int mask = 0; mask < permutations; ++mask) {
        for(int j = 0; j < n; ++j) {
            if(mask & (1 << j)) {
                for(int k = 0; k < m; ++k) {
                    arr[j][k] *= -1;
                }
            }
        }

        int sum = 0;
        for(int k = 0; k < m; ++k) {
            int colSum = 0;
            for(int j = 0; j < n; ++j) {
                colSum += arr[j][k];
            }
            sum += abs(colSum);
        }

        for(int j = 0; j < n; ++j) {
            if(mask & (1 << j)) {
                for(int k = 0; k < m; ++k) {
                    arr[j][k] *= -1;
                }
            }
        }

        biggest = std::max(sum, biggest);
    }
    std::ofstream o("flip.out");
    o << biggest;
    o.close();

    return 0;
}