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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)

int a[10][10000];
int R, C;
int m = 0;
void fr(int j) {

  rep(i, R) {
    if (a[i][j] == 0)
      a[i][j] = 1;
    else
      a[i][j] = 0;
  }
}

void fc(int i) {

  rep(j, C) {
    if (a[i][j] == 0)
      a[i][j] = 1;
    else
      a[i][j] = 0;
  }
}

void g(int k) {

  if (k == R) {
    rep(j, C) {

      int p = 0;
      rep(i, R) if (a[i][j] == 1) p++;

      if (p > R - p)
        fr(j);
    }

    int c = 0;
    rep(i, R) rep(j, C) if (a[i][j] == 0) c++;

    if (m < c)
      m = c;

    return;
  }

  g(k + 1);

  fc(k);
  g(k + 1);
}

int main() {

  while (true) {

    cin >> R >> C;
    if (R == 0 && C == 0)
      break;

    rep(i, R) rep(j, C) cin >> a[i][j];

    g(0);

    cout << m << endl;

    rep(i, R) rep(j, C) a[i][j] = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = int(s); i <= int(e); i++)
#define rep(i, n) for (int i = 0; i < int(n); i++)

int a[10][10000];
int R, C;
int m = 0;
void fr(int j) {

  rep(i, R) {
    if (a[i][j] == 0)
      a[i][j] = 1;
    else
      a[i][j] = 0;
  }
}

void fc(int i) {

  rep(j, C) {
    if (a[i][j] == 0)
      a[i][j] = 1;
    else
      a[i][j] = 0;
  }
}

void g(int k) {

  if (k == R) {
    rep(j, C) {

      int p = 0;
      rep(i, R) if (a[i][j] == 1) p++;

      if (p > R - p)
        fr(j);
    }

    int c = 0;
    rep(i, R) rep(j, C) if (a[i][j] == 0) c++;

    if (m < c)
      m = c;

    return;
  }

  g(k + 1);

  fc(k);
  g(k + 1);
}

int main() {

  while (true) {

    cin >> R >> C;
    rep(i, R) rep(j, C) cin >> a[i][j];

    g(0);

    cout << m << endl;

    rep(i, R) rep(j, C) a[i][j] = 0;
  }

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

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

//?意：
// 有一个??器可以?r行c列的煎?，煎?可以正面朝上（用1表示）也可以背面朝上（用0表示）。
// 一次可将同一行或同一列的煎?全部翻?。?在需要把尽可能多的煎?翻成正面朝上，?最多能使多少煎?正面朝上？?
//?入：
// 多??入，??第一行?二整数r, c (1 ? r ? 10, 1 ? c ? 10
// 000)，剩下r行c列表示煎?初始状?。r=c=0表示?入?束。? ?出：
//?于???入，?出最多能使多少煎?正面朝上。

// 思路：
//??上下翻的?首先想到bitset（flip功能），把?个煎?当成二?制数中的一位
// 由于翻?同?的行和列?，翻??序?煎?的最?朝向没有影?，所以可以先判断所有的横行是否翻?，然后求出??横行翻法??的煎?朝上数的最大?。
//?了得到最大?，希望?个?行在翻?后朝上的煎?数都?加，否?不翻?。
// 最后?比??横行翻法的最大?，从中取最大?就是答案

bitset<10000> cookie[10];
int r, c, i, j, ans, result, up;

int max(int a, int b) { return a > b ? a : b; }

void dfs(int k) {
  if (k == r) {
    result = 0;
    for (i = 0; i < c; i++) {
      up = 0;
      for (j = 0; j < r; j++) {
        if (cookie[j][i])
          up++;
      }
      result += max(up, r - up);
    }
    ans = max(ans, result);
    return;
  }
  dfs(k + 1);
  cookie[k].flip();
  dfs(k + 1);
}

int main() {
  while (scanf("%d %d", &r, &c) && r && c) {
    getchar();
    ans = 0;
    for (i = 0; i < r; i++) {
      for (j = 0; j < c; j++) {
        bool a;
        cin >> a;
        getchar();
        cookie[i][j] = a;
      }
    }
    ans = 0;
    dfs(1);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

//?意：
// 有一个??器可以?r行c列的煎?，煎?可以正面朝上（用1表示）也可以背面朝上（用0表示）。
// 一次可将同一行或同一列的煎?全部翻?。?在需要把尽可能多的煎?翻成正面朝上，?最多能使多少煎?正面朝上？?
//?入：
// 多??入，??第一行?二整数r, c (1 ? r ? 10, 1 ? c ? 10
// 000)，剩下r行c列表示煎?初始状?。r=c=0表示?入?束。? ?出：
//?于???入，?出最多能使多少煎?正面朝上。

// 思路：
//??上下翻的?首先想到bitset（flip功能），把?个煎?当成二?制数中的一位
// 由于翻?同?的行和列?，翻??序?煎?的最?朝向没有影?，所以可以先判断所有的横行是否翻?，然后求出??横行翻法??的煎?朝上数的最大?。
//?了得到最大?，希望?个?行在翻?后朝上的煎?数都?加，否?不翻?。
// 最后?比??横行翻法的最大?，从中取最大?就是答案

bitset<10000> cookie[10];

int max(int a, int b) { return a > b ? a : b; }

int main() {
  int r, c, i, j, k;
  while (scanf("%d %d", &r, &c) && r && c) {
    int result = 0;
    for (i = 0; i < r; i++) {
      for (j = 0; j < c; j++) {
        bool a;
        cin >> a;
        getchar();
        cookie[j] = a;
      }
    }
    for (i = 0; i < (1 << r); i++) { // 横行有2r?翻法
      for (j = 0; j < r; j++) {      // 分??判断?一行
        if (i & (1 << j))
          cookie[j].flip(); // 通?二?制位?比来判断翻不翻
      }
      int posible_answer = 0;
      for (j = 0; j < c; j++) {
        int up = 0;
        for (k = 0; k < r; k++) {
          if (cookie[k][j])
            up++;
        }
        posible_answer += max(up, r - up); //?算?一列面朝上的煎?数并判断翻不翻
      }
      result = max(result, posible_answer);
      for (j = 0; j < r; j++) { //?原
        if (i & (1 << j))
          cookie[j].flip();
      }
    }
    printf("%d\\n", result);
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>

using namespace std;

int bd[10][10000];

int main() {
  while (1) {
    int r, c;
    scanf(" %d %d", &r, &c);
    if (r == 0 && c == 0)
      break;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        scanf(" %d", &bd[i][j]);
      }
    }
    int ans = -1;
    int up = (1 << r);
    for (int sl = 0; sl < up; sl++) {
      int h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }

      int cnt = 0;
      for (int j = 0; j < c; j++) {
        int sm = 0;
        for (int i = 0; i < r; i++) {
          sm += bd[i][j];
        }
        cnt += max(r - sm, sm);
      }
      ans = max(cnt, ans);
      h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int bd[10][1000];

int main() {
  while (1) {
    int r, c;
    scanf(" %d %d", &r, &c);
    if (r == 0 && c == 0)
      break;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        scanf(" %d", &bd[i][j]);
      }
    }
    int ans = -1;
    int up = (1 << r);
    for (int sl = 0; sl < up; sl++) {
      int h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }

      int cnt = 0;
      for (int j = 0; j < c; j++) {
        int sm = 0;
        for (int i = 0; i < r; i++) {
          sm += bd[i][j];
        }
        cnt += max(r - sm, sm);
      }
      ans = max(cnt, ans);
      h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)

typedef bitset<10000> bs;
int R, C;
int check(const vector<bs> &s) {
  int res = 0;
  rep(j, C) {
    int cnt = 0;
    rep(i, R) if (s[i][j]) cnt++;
    res += max(cnt, R - cnt);
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> R >> C && R) {
    vector<bs> senbei(R, bs(0));
    bool in_;
    rep(i, R) rep(j, C) {
      cin >> in_;
      if (in_)
        senbei[i][j] = 1;
    }
    bs to_rev(0);
    rep(i, C) to_rev[i] = 1;
    vector<bs> temp;
    int ans = 0;
    rep(S, 1 << R) {
      temp = senbei;
      rep(i, R) if ((S >> i) & 1) temp[i] ^= to_rev;
      ans = max(ans, check(temp));
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)

typedef bitset<1000> bs;
int R, C;
int check(const vector<bs> &s) {
  int res = 0;
  rep(j, C) {
    int cnt = 0;
    rep(i, R) if (s[i][j]) cnt++;
    res += max(cnt, R - cnt);
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> R >> C && R) {
    vector<bs> senbei(R, bs(0));
    bool in_;
    rep(i, R) rep(j, C) {
      cin >> in_;
      if (in_)
        senbei[i][j] = 1;
    }
    bs to_rev(0);
    rep(i, C) to_rev[i] = 1;
    vector<bs> temp;
    int ans = 0;
    rep(S, 1 << R) {
      temp = senbei;
      rep(i, R) if ((S >> i) & 1) temp[i] ^= to_rev;
      ans = max(ans, check(temp));
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
int bcnt[4096];

inline int c(int n, int w) {
  int res = 0;
  for (int i = 0; n && i < w; i++) {
    if (n & 1)
      res++;
    n >>= 1;
  }
  return res;
}

int solve(vi const &v, int const h, int const w) {
  int ans = 0;
  rep(mask, 1 << w) {
    int t = 0;
    rep(i, h) {
      int m = v[i] ^ mask;
      // int x=c(m,w);
      int x = bcnt[m];
      x = max(w - x, x);
      t += x;
    }
    ans = max(ans, t);
  }
  return ans;
}

int main() {
  int h, w;
  for (int i = 0; i < (1 << 12); i++) {
    int x = c(i, 20);
    bcnt[i] = x;
    // printf("cnt[%d]=%d;\\n",i,x);
  }

  // exit(0);

  while (cin >> h >> w && h + w) {
    vvi v(h, vi(w));
    vi o(w);
    rep(i, h) rep(j, w) cin >> v[i][j];
    rep(i, w) {
      int t = 0;
      rep(j, h) {
        t <<= 1;
        t |= v[j][i];
      }
      o[i] = t;
    }
    cout << solve(o, w, h) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

using namespace std;

typedef vector<int> vi;
int bcnt[4096];

inline int c(int n, int w) {
  int res = 0;
  for (int i = 0; n && i < w; i++) {
    if (n & 1)
      res++;
    n >>= 1;
  }
  return res;
}

int solve(vi const &v, int h, int w) {
  int ans = 0;
  rep(mask, 1 << w) {
    int t = 0;
    rep(i, h) {
      int m = v[i] ^ mask;
      // int x=c(m,w);
      int x = bcnt[m];
      x = max(w - x, x);
      t += x;
    }
    ans = max(ans, t);
  }
  return ans;
}

int main() {
  int h, w;
  for (int i = 0; i < (1 << 12); i++) {
    int x = c(i, 20);
    bcnt[i] = x;
    // printf("cnt[%d]=%d;\\n",i,x);
  }

  // exit(0);

  while (cin >> h >> w && h + w) {
    vi v(h);
    rep(i, h) {
      int x = 0;
      rep(j, w) {
        x <<= 1;
        int t;
        cin >> t;
        x |= t;
      }
      v[i] = x;
    }
    cout << solve(v, h, w) << endl;
  }
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int R, C;
int f[10][10000];
bool flag[10];
int ans = 0;

int cnt(void) {
  int res = 0;
  for (int i = 0; i < C; i++) {
    int tmp = 0;
    for (int j = 0; j < R; j++) {
      if (!f[j][i] && flag[j] || f[j][i] && !flag[j])
        tmp++;
    }
    res += max(tmp, R - tmp);
  }
  return res;
}

void solve(int num) {
  if (num == R) {
    ans = max(cnt(), ans);
    return;
  }
  flag[num] = false;
  solve(num + 1);
  flag[num] = true;
  solve(num + 1);
}

int main() {
  while (cin >> R >> C) {
    if (R == 0 || C == 0)
      break;
    ans = 0;
    for (int r = 0; r < R; r++)
      for (int c = 0; c < C; c++)
        cin >> f[r][c];
    solve(0);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int R, C;
int f[10][1000];
bool flag[10];
int ans = 0;

int cnt(void) {
  int res = 0;
  for (int i = 0; i < C; i++) {
    int tmp = 0;
    for (int j = 0; j < R; j++) {
      if (!f[j][i] && !flag[j] || f[j][i] && flag[j])
        tmp++;
    }
    res += max(tmp, R - tmp);
  }
  return res;
}

void solve(int num) {
  if (num == R) {
    ans = max(cnt(), ans);
    return;
  }
  flag[num] = false;
  solve(num + 1);
  flag[num] = true;
  solve(num + 1);
}

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

#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, ano;
    for (int j = 0; j < c; j++) {
      a = 0;
      for (int i = 0; i < r; i++) {
        if (pan[i][j] == 1)
          a++;
      }
      ano = r - a;
      // a>r-a?(cnt+=a):(cnt+=(r-a));
      ano > a ? (cnt += ano) : (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;
  }
}


Error: TLE

Fixed submission:

/*直接对行用dfs:TLE*/
#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, ano;
    for (int j = 0; j < c; j++) {
      a = 0;
      for (int i = 0; i < r; j++) {
        if (pan[i][j] == 1)
          a++;
      }
      ano = r - a;
      // a>r-a?(cnt+=a):(cnt+=(r-a));
      ano > a ? (cnt += ano) : (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 <bitset>
#include <cstdio>
using namespace std;

const int MAXN = 10010;
const int INF = 0x3f3f3f3f;
int R, C, ans;
bitset<10005> B[10];

void dfs(int k) {
  int cnt = 0;
  if (k == R) {
    for (int j = 0; j < C; j++) {
      int t = 0;
      for (int i = 0; i < R; i++) {
        t += B[i][j];
      }
      if (t > R - t) {
        cnt += t;
      } else {
        cnt += R - t;
      }
    }
    if (cnt > ans)
      ans = cnt;
    return;
  }

  dfs(k + 1);
  B[k].flip();
  dfs(k + 1);
}

int main() {
  while (scanf("%d%d", &R, &C), R || C) {
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        bool t;
        scanf("%d", &t);
        B[i][j] = t;
      }
    }
    ans = 0;
    dfs(0);
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;

const int MAXN = 10010;
const int INF = 0x3f3f3f3f;
int R, C, ans;
int A[MAXN];
int B[MAXN];

void solve() {
  int cnt = 0, t;
  for (int i = 0; i < R; i++) {
    t = 0;
    for (int j = 0; j < C; j++) {
      t += B[i] & 0x1;
      B[i] >>= 1;
    }
    cnt += t > C - t ? t : C - t;
  }
  if (ans < cnt)
    ans = cnt;
}

int main() {
  while (scanf("%d%d", &R, &C), R) {
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        int t;
        scanf("%d", &t);
        A[i] = (A[i] << 1) | t;
      }
    }

    ans = 0;
    for (int i = 0; i < (1 << C); i++) {
      for (int j = 0; j < R; j++) {
        B[j] = A[j] ^ i;
      }
      solve();
    }
    printf("%d\\n", ans);
  }
}
Problem p00448 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
using namespace std;
int r, c, data[12][10010], start[10010], rowcnt[12], tmp[10010];
int main() {
  ios::sync_with_stdio(false);
  while (true) {
    cin >> r >> c;
    if (r == 0)
      return 0;
    int ans = 0;
    memset(start, 0, sizeof(start));
    memset(rowcnt, 0, sizeof(rowcnt));
    for (int i = 0; i < r; i++)
      for (int j = 0; j < c; j++) {
        cin >> data[i][j];
        if (data[i][j]) {
          ++start[j];
          ++rowcnt[i];
        }
      }
    for (int x = 0; x < (1 << r); x++) {
      int smallans = 0;
      for (int col = 0; col < c; col++)
        tmp[col] = start[col];
      for (int dig = 0; dig < r; dig++) {
        if ((x >> dig) & 1) {
          for (int col = 0; col < c; col++) {
            if (data[dig][col])
              --tmp[col];
            else
              ++tmp[col];
          }
        }
      }
      for (int col = 0; col < c; col++)
        smallans += max(r - tmp[col], tmp[col]);
      ans = max(ans, smallans);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bitset>
#include <iostream>
using namespace std;

bitset<10000> map1[10];
int r, c, cou;

void dfs(int all) {
  if (all == r) {
    int num, upcou = 0;
    for (int i = 0; i < c; i++) {
      num = 0;
      for (int k = 0; k < r; k++)
        if (map1[k][i])
          num++;
      upcou += max(num, r - num);
    }
    cou = max(cou, upcou);
    return;
  }
  dfs(all + 1);
  map1[all].flip();
  dfs(all + 1);
  return;
}
int main() {
  while (cin >> r >> c) // r行c列
  {
    for (int i = 0, g; i < r; i++)
      for (int k = 0; k < c; k++) {
        cin >> g;
        map1[i][k] = g;
      }
    cou = 0;
    dfs(1);
    cout << cou << endl;
  }
  return 0;
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
using namespace std;

int r, c;
bitset<10000> cookie[10];

int main() {
  while (cin >> r >> c && r) {
    for (int i = 0; i < r; ++i)
      for (int j = 0; j < c; ++j) {
        bool upward;
        cin >> upward;
        cookie[i][j] = upward;
      }
    int ans = 0;
    for (int i = 0; i < (1 << r); ++i) {
      for (int j = 0; j < r; ++j)
        if (i & (1 << j))
          cookie[j].flip();
      int possible_ans = 0;
      for (int j = 0; j < c; ++j) {
        int up_cookie_count = 0;
        for (int k = 0; k < r; ++k)
          if (cookie[k][j])
            up_cookie_count++;
        possible_ans += max(up_cookie_count, r - up_cookie_count);
      }
      ans = max(ans, possible_ans);
      for (int j = 0; j < r; ++j)
        if (i & (1 << j))
          cookie[j].flip();
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int ma[11][10001];
int vis[11]; // 记录这一行是不是被翻过了
int n, m, ans;

void flip(int h) // 翻转第h行
{
  for (int i = 0; i < m; i++)
    ma[h][i] = 1 - ma[h][i];
}

void judge() {
  int sum1 = 0;
  for (int i = 0; i < m;
       i++) // 查看每一列的情况，如果正面朝上比较多那么就不用翻，少就翻
  {
    int sum = 0;
    for (int j = 0; j < n; j++)
      sum += ma[j][i];
    sum = max(sum, n - sum); // 直接取最大值即可。
    sum1 += sum;
  }
  ans = max(sum1, ans);
  return;
}

void dfs(int deep) {
  if (deep == n + 1)
    return;
  for (int i = 0; i < n; i++) {
    if (vis[i] == 1)
      continue;
    flip(i);
    vis[i] = 1;
    judge();
    dfs(deep + 1);
    flip(i);
    vis[i] = 0;
  }
}

int main() {
  while (scanf("%d %d", &n, &m)) {
    memset(vis, 0, sizeof(vis));
    ans = 0;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        scanf("%d", &ma[i][j]);
    dfs(1);
    cout << ans << endl;
  }
}
Problem p00448 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int R;
int C;
int osenbei[10][10000];
int flag[11];
int ans;

int count(void) {
  int temp, res = 0;

  for (int j = 0; j < C; j++) {
    temp = 0;
    for (int i = 0; i < R; i++) {
      if (flag[i] && !osenbei[i][j] || !flag[i] && osenbei[i][j])
        temp++;
    }
    if (temp < R - temp) // j列目を裏返したほうがいい
      res += R - temp;
    else
      res += temp;
  }
  return res;
}

void solve(int idx) {
  int res;

  if (idx == R - 1) {
    res = count();
    if (ans < res)
      ans = res;
    return;
  }
  flag[idx] = 1; // idx行を裏返す
  solve(idx + 1);

  flag[idx] = 0; // idx行は裏返さない
  solve(idx + 1);
  return;
}

int main() {
  while (cin >> R >> C) {
    if (R == 0 && C == 0)
      break;
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        cin >> osenbei[i][j];
      }
    }
    ans = 0;
    solve(0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)

// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int R;
int C;
int osenbei[10][10000];
int flag[10];
int ans;

int count(void) {
  int temp, res = 0;

  for (int j = 0; j < C; j++) {
    temp = 0;
    for (int i = 0; i < R; i++) {
      if (flag[i] && !osenbei[i][j] || !flag[i] && osenbei[i][j])
        temp++;
    }
    if (temp < R - temp) // j列目を裏返したほうがいい
      res += R - temp;
    else
      res += temp;
  }
  return res;
}

void solve(int idx) {
  int res;

  if (idx == R) {
    res = count();
    if (ans < res)
      ans = res;
    return;
  }
  flag[idx] = 1; // idx行を裏返す
  solve(idx + 1);

  flag[idx] = 0; // idx行は裏返さない
  solve(idx + 1);
}

int main() {
  while (cin >> R >> C, R || C) {
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        scanf("%d", &osenbei[i][j]);
      }
    }
    for (int i = 0; i < R; i++) {
      ans = 0;
      solve(0);
    }
    printf("%d", ans);
  }
  return 0;
}