Cod sursa(job #2939171)

Utilizator Luca_Miscocilucainfoarena Luca_Miscoci Data 13 noiembrie 2022 10:32:50
Problema Jocul Flip Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>

using namespace std;

const int nmax = 16;
int mat[nmax + 1][nmax + 1];
int f[nmax];

int MaxSum();

int n, m, maxx;
void bkt (int poz){
  if (poz == n){
    int a = MaxSum();
    maxx = max (maxx, a);
    return ;
  }
  ++poz;
  f[poz] = 1;
  bkt (poz);
  f[poz] = 0;
  bkt(poz);
  return ;
}

int MaxSum(){
  int sol = 0;
  int s = 0;
  for (int i = 0; i < n; i++){
    s = 0;
    for (int j = 0; j < m; j++){
      if (f[j]) s -= mat[i][j];
      else s += mat[i][j];
    }

    if (s < 0) s *= -1;
    sol += s;
  }
  return sol;
}
int main(){

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

  fin >> n >> m;
  for (int i = 0; i < n; i++){
    for (int j = 0; j < m; j++)
      fin >> mat[i][j];
  }
  bkt (-1);
  fout << maxx;
  return 0;
}