alexjercan/bugnet huggingface dataset:
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstring>
#include <iostream>
using namespace std;
int r, c, ans, num;
bitset<10005> g[15];

int main() {
  cin >> r >> c;
  while (r && c) {
    for (int i = 1; i <= r; i++) {
      for (int j = 1; j <= c; j++) {
        cin >> num;
        g[i][j] = num;
      }
    }
    ans = 0;
    // 每一行都有是否翻转这2种情况，最多2的r次方种。
    // 从0000000000～1111111111，等于1的，对应行翻身，等于0的，对应行不翻身
    for (int i = 0; i < (1 << r); i++) {

      // 把i表示成二进制字符串,从右数第row_id位上的数字是否为1
      for (int row_id = 0; row_id <= 9; row_id++) {
        if (!(i >> row_id))
          break;
        if ((i >> row_id) & 1)
          g[row_id + 1].flip();
      }
      // 不必真的翻转列。取每一列朝向数较多的
      int temp = 0;
      int shang;
      int xia;
      for (int j = 1; j <= c; j++) {
        shang = 0;

        for (int k = 1; k <= r; k++) {
          if (g[k][j])
            shang++;
        }
        xia = r - shang;
        if (shang >= xia)
          temp += shang;
        else
          temp += xia;
      }
      ans = ans > temp ? ans : temp;
      // 恢复原状
      for (int row_id = 0; row_id <= 9; row_id++) {
        if (!(i >> row_id))
          break;
        if ((i >> row_id) & 1)
          g[row_id + 1].flip();
      }
    }
    cout << ans << endl;
    cin >> r >> c;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <set>
using namespace std;
int r, c, ans;
int g[15][10005];
int new_g[15][10005];
int main() {
  cin >> r >> c;
  while (r && c) {
    for (int i = 1; i <= r; i++) {
      for (int j = 1; j <= c; j++) {
        cin >> g[i][j];
      }
    }
    ans = 0;
    // 每一行都有是否翻转这2种情况，最多2的r次方种。
    // 从0000000000～1111111111，等于1的，对应行翻身，等于0的，对应行不翻身
    for (int i = 0; i < (1 << r); i++) {
      for (int k = 1; k <= r; k++) {
        for (int j = 1; j <= c; j++) {
          new_g[k][j] = g[k][j];
        }
      }
      // 把i表示成二进制字符串,从右数第row_id位是否为1
      int row_id = 1;
      while (i > 0) {
        if (i & 1) {
          for (int j = 1; j <= c; j++) {
            new_g[row_id][j] = 1 - new_g[row_id][j];
          }
        }
        i = i >> 1;
        row_id++;
      }
      // 不必真的翻转列。取每一列朝向数较多的
      int temp = 0;
      int shang;
      int xia;
      for (int j = 1; j <= c; j++) {
        shang = 0;
        xia = 0;
        for (int k = 1; k <= r; k++) {
          if (new_g[k][j])
            shang++;
          else
            xia++;
        }
        if (shang >= xia)
          temp += shang;
        else
          temp += xia;
      }
      ans = ans > temp ? ans : temp;
    }
    cout << ans << endl;
    cin >> r >> c;
  }

  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;
int sen[10000];

int main() {
  int r, c, cc;
  while (true) {
    scanf("%d %d", &r, &c);
    if (!r && !c)
      break;
    for (int i = 0; i < c; i++)
      sen[i] = 0;

    for (int j = 0; j < r; j++) {
      for (int i = 0; i < c; i++) {
        scanf("%d", &cc);
        sen[i] <<= 1;
        sen[i] += cc;
      }
    }

    int s, n, sum = 0, smax = 0, k = 1;
    k <<= r;
    for (int i = 0; i < k; i++) {
      sum = 0;
      for (int j = 0; j < c; j++) {
        s = sen[j] ^ i;
        for (n = 0; s > 0; s >>= 1)
          n += s & 1;
        sum += max(n, r - n);
      }
      if (sum > smax)
        smax = sum;
    }
    printf("%d\\n", smax);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  int r, c, cc;
  int sen[c];
  while (true) {
    scanf("%d %d", &r, &c);
    if (!r && !c)
      break;
    for (int i = 0; i < c; i++)
      sen[i] = 0;

    for (int j = 0; j < r; j++) {
      for (int i = 0; i < c; i++) {
        scanf("%d", &cc);
        sen[i] <<= 1;
        sen[i] += cc;
      }
    }

    int s, n, sum = 0, smax = 0, k = 1;
    k <<= r;
    for (int i = 0; i < k; i++) {
      sum = 0;
      for (int j = 0; j < c; j++) {
        s = sen[j] ^ i;
        for (n = 0; s > 0; n += s & 1, s >>= 1)
          ;
        sum += max(n, r - n);
      }
      if (sum > smax)
        smax = sum;
    }
    printf("%d\\n", smax);
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int g[10][10000];
int maxn;
int r, c;
void dfs(int step) {
  if (step == r) {
    int num = 0;
    for (int i = 0; i < c; ++i) {
      int res = 0;
      for (int j = 0; j < r; ++j)
        res += g[j][i];
      num += max(res, r - res);
    }
    maxn = max(num, maxn);
  } else {
    dfs(step + 1);
    for (int j = 0; j < c; ++j)
      g[step][j] = !g[step][j];
    dfs(step + 1);
  }
}
int main() {
  while (cin >> r >> c && r && c) {
    for (int i = 0; i < r; ++i)
      for (int j = 0; j < c; ++j)
        cin >> g[i][j];
    maxn = 0;
    dfs(0);
    cout << maxn << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int g[11][1001];
int maxn;
int r, c;
void dfs(int step) {
  if (step == r) {
    int num = 0;
    for (int i = 0; i < c; ++i) {
      int res = 0;
      for (int j = 0; j < r; ++j)
        res += g[j][i];
      num += max(res, r - res);
    }
    maxn = max(num, maxn);
  } else {
    dfs(step + 1);
    for (int j = 0; j < c; ++j)
      g[step][j] = !g[step][j];
    dfs(step + 1);
  }
}
int main() {
  while (cin >> r >> c && r && c) {
    for (int i = 0; i < r; ++i)
      for (int j = 0; j < c; ++j)
        cin >> g[i][j];
    maxn = 0;
    dfs(0);
    cout << maxn << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <bitset>
#include <cstdio>
#include <iostream>

using namespace std;

#define MaxC 10000
#define MaxR 10
// bitset<MaxC> pan[MaxR];
int pan[MaxR][MaxC];
int R, C;
int result = 0;

void dfs(
    int index) { // sum表示已经得到的最大1的数目，dfs搜索当前行是否反转，得到最大的1的数
  if (index == R) {
    int sum = 0;
    for (int i = 0; i < C; ++i) { // 列
      int num = 0;
      for (int j = 0; j < R; ++j) { // 行
        if (pan[j][i] == 1)
          num++;
      }

      sum += max(num, R - num);
    }
    result = max(result, sum);
  } else {
    dfs(index + 1);
    for (int i = 0; i < C; ++i) {
      if (pan[index][i] == 1)
        pan[index][i] = 0;
      else
        pan[index][i] = 1;
    }
    dfs(index + 1);
  }
}
int main() {
  while (cin >> R >> C && C != 0 && R != 0) {
    //    cin>>R>>C;
    for (int i = 0; i < R; ++i) {
      for (int j = 0; j < C; ++j) {
        cin >> pan[i][j];
      }
    }
    dfs(0);
    cout << result << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bitset>
#include <cstdio>
#include <iostream>

using namespace std;

#define MaxC 1000
#define MaxR 10
// bitset<MaxC> pan[MaxR];
int pan[MaxR][MaxC];
int R, C;
int result = 0;

void dfs(
    int index) { // sum表示已经得到的最大1的数目，dfs搜索当前行是否反转，得到最大的1的数
  if (index == R) {
    int sum = 0;
    for (int i = 0; i < C; ++i) { // 列
      int num = 0;
      for (int j = 0; j < R; ++j) { // 行
        if (pan[j][i] == 1)
          num++;
      }

      sum += max(num, R - num);
    }
    result = max(result, sum);
  } else {
    dfs(index + 1);
    for (int i = 0; i < C; ++i) {
      if (pan[index][i] == 1)
        pan[index][i] = 0;
      else
        pan[index][i] = 1;
    }
    dfs(index + 1);
  }
}
int main() {
  while (cin >> R >> C && C != 0 && R != 0) {
    //    cin>>R>>C;
    for (int i = 0; i < R; ++i) {
      for (int j = 0; j < C; ++j) {
        cin >> pan[i][j];
      }
    }
    dfs(0);
    cout << result << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int a[102][10002], f[10], n, m;

int calc(int f[10]) {
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int res = 0;
    for (int j = 0; j < n; j++) {
      if (f[j])
        res += !a[j][i];
      else
        res += a[j][i];
    }
    ans += max(res, n - res);
  }
  return ans;
}

int solve(int k, int b) {
  if (k == n)
    return 0;
  int a[3];
  // for(int i = 0;i < 10;i++)f[i] = c[i];
  f[k] = b;
  a[0] = calc(f);
  a[1] = solve(k + 1, 0);
  a[2] = solve(k + 1, 1);
  sort(a, a + 3);
  return a[2];
}

int main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> a[i][j];
    int x, y;
    fill(f, f + 10, 0);
    x = solve(0, 0);
    y = solve(0, 1);
    cout << max(x, y) << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int a[12][1002], f[10], n, m;

int calc(int f[10]) {
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int res = 0;
    for (int j = 0; j < n; j++) {
      if (f[j])
        res += !a[j][i];
      else
        res += a[j][i];
    }
    ans += max(res, n - res);
  }
  return ans;
}

int solve(int k, int b) {
  if (k == n)
    return 0;
  int a[3] = {0, 0, 0};
  // for(int i = 0;i < 10;i++)f[i] = c[i];
  f[k] = b;
  // a[0] = calc(f);
  // a[1] = solve(k+1, 0);
  // a[2] = solve(k+1, 1);
  sort(a, a + 3);
  return a[2];
}

int main() {
  while (cin >> n >> m, n) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> a[i][j];
    /*int x, y;
    fill(f, f+10, 0);
    x = solve(0, 0);
    y = solve(0, 1);*/
    cout << 3 << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int ary[20][10010];
int main() {
  int R, C, res, tmp, cnt;
  while (true) {
    cin >> R >> C;
    if (R == 0 && C == 0)
      break;
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        cin >> ary[i][j];
      }
    }

    res = 0;
    for (int i = 0; i < (1 << R); i++) {
      cnt = 0;
      for (int j = 0; j < C; j++) {
        tmp = 0;
        for (int k = 0; k < R; k++) {
          tmp += (ary[k][j] ^ ((i >> k) & 1));
        }
        cnt += max(tmp, R - tmp);
      }
      res = max(res, cnt);
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int ary[20][10010];
int main() {
  int R, C, res, tmp, cnt;
  while (true) {
    cin >> R >> C;
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        cin >> ary[i][j];
      }
    }

    res = 0;
    for (int i = 0; i < (1 << R); i++) {
      cnt = 0;
      for (int j = 0; j < C; j++) {
        tmp = 0;
        for (int k = 0; k < R; k++) {
          tmp += (ary[k][j] ^ ((i >> k) & 1));
        }
        cnt += max(tmp, R - tmp);
      }
      res = max(res, cnt);
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

#define rep2(i, b, n) for (int i = b; i < n; ++i)
#define rep(i, n) rep2(i, 0, n)

using namespace std;

bool fd[10][10000];

int h, w;

int search(int r) {
  int res = 0;
  if (r >= h) {
    rep(x, w) {
      int t_sum = 0;
      rep(y, h) {
        if (!fd[y][x])
          ++t_sum;
      }
      res += max(t_sum, h - t_sum);
    }
    return res;
  }
  rep(i, w) { fd[r][i] = !fd[r][i]; }
  res = max(res, search(r + 1));
  rep(i, w) { fd[r][i] = !fd[r][i]; }
  res = max(res, search(r + 1));
  return res;
}

int main(void) {
  while (cin >> h >> w, h | w) {
    rep(y, h) rep(x, w) { cin >> fd[y][x]; }
    cout << search(0) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

#define rep2(i, b, n) for (int i = b; i < n; ++i)
#define rep(i, n) rep2(i, 0, n)

using namespace std;

bool fd[10][10000];

int h, w;

int search(int r) {
  int res = 0;
  if (r >= w) {
    rep(x, w) {
      int t_sum = 0;
      rep(y, h) {
        if (!fd[y][x])
          ++t_sum;
      }
      res += max(t_sum, h - t_sum);
    }
    return res;
  }
  rep(i, w) { fd[r][i] = !fd[r][i]; }
  res = max(res, search(r + 1));
  rep(i, w) { fd[r][i] = !fd[r][i]; }
  res = max(res, search(r + 1));
  return res;
}

int main(void) {
  while (cin >> h >> w, h | w) {
    rep(y, h) rep(x, w) { cin >> fd[y][x]; }
    cout << search(0) << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

bool s[15][10005] = {0};

int ans = 0;
int n, m;

void dfs(int x) {
  if (x == n + 1) {
    int sum = 0;
    int term = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 1; i <= n; i++)
        if (s[i][j] == 1)
          term++;
      sum += term > n - term ? term : n - term;
      term = 0;
    }
    ans = max(ans, sum);
    return;
  }
  dfs(x + 1);
  for (int i = 1; i <= m; i++)
    s[x][i] = !s[x][i];
  dfs(x + 1);
}

int main() {
  // freopen("in.in","r",stdin);
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;
    ans = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        scanf("%d", &s[i][j]);
    dfs(1);
    printf("%d\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

const int MAX_R = 10;
const int MAX_C = 10000;
int pan[MAX_R][MAX_C];

int r, c;

void reverse(int row) {
  for (int i = 0; i < c; i++) {
    if (pan[row][i] == 0)
      pan[row][i] = 1;
    else
      pan[row][i] = 0;
  }
}

int dfs(int row, int rev) {
  if (rev)
    reverse(row);
  if (row == (r - 1)) {
    if (rev)
      reverse(row);
    int cnt = 0;
    int a, b;
    for (int j = 0; j < c; j++) {
      for (int i = 0; i < r; j++) {
        if (pan[i][j] == 1)
          a++;
      }
      b = r - a;
      b > a ? (cnt += b) : (cnt += a);
    }
    return cnt;
  }
  int a = dfs(row + 1, 0);
  int b = dfs(row + 1, 1);
  if (rev)
    reverse(row);
  return a > b ? a : b;
}
int solve() {
  int a = dfs(0, 0);
  int b = dfs(0, 1);
  return a > b ? a : b;
}

int main() {
  while ((cin >> r) && (cin >> c) && (r > 0)) {
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        cin >> pan[i][j];
      }
    }
    cout << solve() << endl;
  }
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

bool s[15][10005] = {0};

int ans = 0;
int n, m;

int dfs(int x) {
  if (x == n + 1) {
    int sum = 0;
    int term = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 1; i <= n; i++)
        if (s[i][j] == 1)
          term++;
      sum += term > n - term ? term : n - term;
      term = 0;
    }
    ans = max(ans, sum);
    return ans;
  }
  int a = dfs(x + 1);
  for (int i = 1; i <= m; i++)
    s[x][i] = !s[x][i];
  int b = dfs(x + 1);
  return a > b ? a : b;
}

int main() {
  // freopen("in.in","r",stdin);
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;
    ans = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        scanf("%d", &s[i][j]);
    cout << dfs(1) << endl;
    // printf("%d\\n",ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int MAX_R = 10;
const int MAX_C = 10000;
int pan[MAX_R][MAX_C];

int r, c;

void reverse(int row) {
  for (int i = 0; i < c; i++) {
    if (pan[row][i] == 0)
      pan[row][i] = 1;
    else
      pan[row][i] = 0;
  }
}

int dfs(int row) {
  if (row == r) {
    int cnt = 0;
    int a, b;
    for (int j = 0; j < c; j++) {
      for (int i = 0; i < r; j++) {
        if (pan[i][j] == 1)
          a++;
      }
      b = r - a;
      b > a ? (cnt += b) : (cnt += a);
    }
    return cnt;
  }
  int a = dfs(row + 1);
  reverse(row);
  int b = dfs(row + 1);
  return a > b ? a : b;
}
int solve() { return dfs(0); }

int main() {
  while ((scanf("%d%d", &r, &c) != EOF) && (r > 0)) {
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        cin >> pan[i][j];
      }
    }
    cout << solve() << endl;
  }
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

bool s[15][10005] = {0};

int n, m;

int dfs(int x) {
  if (x == n + 1) {
    int sum = 0;
    int term = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 1; i <= n; i++)
        if (s[i][j] == 1)
          term++;
      sum += term > n - term ? term : n - term;
      term = 0;
    }
    return sum;
  }
  int a = dfs(x + 1);
  for (int i = 1; i <= m; i++)
    s[x][i] = !s[x][i];
  int b = dfs(x + 1);
  return a > b ? a : b;
}

int main() {
  // freopen("in.in","r",stdin);
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        scanf("%d", &s[i][j]);
    cout << dfs(1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int MAX_R = 10;
const int MAX_C = 10000;
int pan[MAX_R][MAX_C];

int r, c;

void reverse(int row) {
  for (int i = 0; i < c; i++) {
    if (pan[row][i] == 0)
      pan[row][i] = 1;
    else
      pan[row][i] = 0;
  }
}

int dfs(int row) {
  if (row == r) {
    int cnt = 0;
    int a, b;
    for (int j = 0; j < c; j++) {
      for (int i = 0; i < r; j++) {
        if (pan[i][j] == 1)
          a++;
      }
      b = r - a;
      b > a ? (cnt += b) : (cnt += a);
    }
    return cnt;
  }
  int a = dfs(row + 1);
  for (int i = 0; i < c; i++)
    pan[row][i] = !pan[row][i];
  int b = dfs(row + 1);
  return a > b ? a : b;
}

int main() {
  while ((scanf("%d%d", &r, &c) != EOF)) {
    if (r == 0 && c == 0)
      break;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        cin >> pan[i][j];
      }
    }
    cout << dfs(0) << endl;
  }
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

bool s[15][10005] = {0};

int n, m;

void reverse(int row) {
  for (int i = 1; i <= m; i++) {
    if (s[row][i] == 0)
      s[row][i] = 1;
    else
      s[row][i] = 0;
  }
}

int dfs(int x) {
  if (x == n + 1) {
    int sum = 0;
    int term = 0;
    for (int j = 1; j <= m; j++) {
      for (int i = 1; i <= n; i++)
        if (s[i][j] == 1)
          term++;
      // sum += term > n-term ? term : n-term;
      term > n - term ? (sum += term) : (sum += (n - term));
      term = 0;
    }
    return sum;
  }
  int a = dfs(x + 1);
  reverse(x);
  // for(int i=1;i<=m;i++)
  //   s[x][i]=!s[x][i];
  int b = dfs(x + 1);
  return a > b ? a : b;
}

int main() {
  // freopen("in.in","r",stdin);
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        cin >> s[i][j];
    cout << dfs(1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int MAX_R = 15;
const int MAX_C = 10005;
bool pan[MAX_R][MAX_C] = {0};

int r, c;

void reverse(int row) {
  for (int i = 0; i < c; i++) {
    if (pan[row][i] == 0)
      pan[row][i] = 1;
    else
      pan[row][i] = 0;
  }
}

int dfs(int row) {
  if (row == r) {
    int cnt = 0;
    int a, b;
    for (int j = 0; j < c; j++) {
      a = 0;
      for (int i = 0; i < r; j++) {
        if (pan[i][j] == 1)
          a++;
      }
      b = r - a;
      b > a ? (cnt += b) : (cnt += a);
    }
    return cnt;
  }
  int a = dfs(row + 1);
  reverse(row);
  int b = dfs(row + 1);
  return a > b ? a : b;
}

int main() {
  while ((scanf("%d%d", &r, &c) != EOF)) {
    if (r == 0 && c == 0)
      break;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        cin >> pan[i][j];
      }
    }
    cout << dfs(0) << endl;
  }
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

bool s[15][10005] = {0};

int n, m;

void reverse(int row) {
  for (int i = 1; i <= m; i++) {
    if (s[row][i] == 0)
      s[row][i] = 1;
    else
      s[row][i] = 0;
  }
}

int dfs(int x) {
  if (x == n + 1) {
    int sum = 0;
    int term = 0, term2;
    for (int j = 1; j <= m; j++) {
      for (int i = 1; i <= n; i++)
        if (s[i][j] == 1)
          term++;
      // sum += term > n-term ? term : n-term;
      term2 = n - term;
      term > term2 ? (sum += term) : (sum += term2);
      term = 0;
    }
    return sum;
  }
  int a = dfs(x + 1);
  reverse(x);
  // for(int i=1;i<=m;i++)
  //   s[x][i]=!s[x][i];
  int b = dfs(x + 1);
  return a > b ? a : b;
}

int main() {
  // freopen("in.in","r",stdin);
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        cin >> s[i][j];
    cout << dfs(1) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int MAX_R = 15;
const int MAX_C = 10005;
bool pan[MAX_R][MAX_C] = {0};

int r, c;

void reverse(int row) {
  for (int i = 0; i < c; i++) {
    if (pan[row][i] == 0)
      pan[row][i] = 1;
    else
      pan[row][i] = 0;
  }
}

int dfs(int row) {
  if (row == r) {
    int cnt = 0;
    int a, b;
    for (int j = 0; j < c; j++) {
      a = 0;
      for (int i = 0; i < r; j++) {
        if (pan[i][j] == 1)
          a++;
      }
      b = r - a;
      // a>r-a?(cnt+=a):(cnt+=(r-a));
      b > a ? (cnt += b) : (cnt += a);
    }
    return cnt;
  }
  int a = dfs(row + 1);
  reverse(row);
  int b = dfs(row + 1);
  return a > b ? a : b;
}

int main() {
  while ((scanf("%d%d", &r, &c) != EOF)) {
    if (r == 0 && c == 0)
      break;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        cin >> pan[i][j];
      }
    }
    cout << dfs(0) << endl;
  }
}