Cod sursa(job #2939177)

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

using namespace std;

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

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;
}