prithivMLmods/Coder-Stat huggingface dataset:
Problem p00446 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  int a;
  cin >> a;
  while (a != 0) {
    int b[100] = {0}, c[100] = {0}, d, e = 0, f[101] = {0}, g = 0, h = 0, i,
        k = 0, n = 0;
    h = 2 * a;
    for (i = 1; i <= a; i++) {
      cin >> d;
      f[d] = 1;
    }
    for (i = 1; i <= h; i++) {
      if (f[i] == 1) {
        b[e] = i;
        e++;
      } else {
        c[g] = i;
        g++;
      }
    }
    k = 0;
    g = 0;
    e = 0;
    int count = 0, j = 0, o = 0, p = 0, s = 0, l = 0, x = 0, z = 0;
    x = a;
    z = a;
    while (1) {
      if (k == 0) {
        for (i = 0; i < a; i++) {
          if (n < b[i]) {
            n = b[i];
            b[i] = 0;
            l = 1;
            x--;
            break;
          }
        }
        if (l == 0) {
          n = 0;
        }
        l = 0;
      }
      if (x == 0) {
        break;
      }
      k = 1;
      if (k == 1) {
        for (g = 0; g < a; g++) {
          if (n < c[g]) {
            n = c[g];
            c[g] = 0;
            s = 1;
            z--;
            break;
          }
        }
        if (s == 0) {
          n = 0;
        }
        s = 0;
      }
      k = 0;
      if (z == 0) {
        break;
      }
    }
    cout << z << endl << x << endl;
    cin >> a;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int a;
  cin >> a;
  while (a != 0) {
    int b[10000] = {0}, c[10000] = {0}, d, e = 0, f[10000] = {0}, g = 0, h = 0,
        i, k = 0, n = 0;
    h = 2 * a;
    for (i = 1; i <= a; i++) {
      cin >> d;
      f[d] = 1;
    }
    for (i = 1; i <= h; i++) {
      if (f[i] == 1) {
        b[e] = i;
        e++;
      } else {
        c[g] = i;
        g++;
      }
    }
    k = 0;
    g = 0;
    e = 0;
    int count = 0, j = 0, o = 0, p = 0, s = 0, l = 0, x = 0, z = 0;
    x = a;
    z = a;
    while (1) {
      if (k == 0) {
        for (i = 0; i < a; i++) {
          if (n < b[i]) {
            n = b[i];
            b[i] = 0;
            l = 1;
            x--;
            break;
          }
        }
        if (l == 0) {
          n = 0;
        }
        l = 0;
      }
      if (x == 0) {
        break;
      }
      k = 1;
      if (k == 1) {
        for (g = 0; g < a; g++) {
          if (n < c[g]) {
            n = c[g];
            c[g] = 0;
            s = 1;
            z--;
            break;
          }
        }
        if (s == 0) {
          n = 0;
        }
        s = 0;
      }
      k = 0;
      if (z == 0) {
        break;
      }
    }
    cout << z << endl << x << endl;
    cin >> a;
  }
}
Problem p00446 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  while (cin >> n && n) {
    vector<int> a, b;
    bool used[105];
    memset(used, 0, sizeof(used));

    int x;
    rep(i, n) {
      cin >> x;
      used[x] = true;
      a.push_back(x);
    }

    sort(a.begin(), a.end());

    REP(i, 1, 2 * n + 1) {
      if (used[i])
        continue;
      b.push_back(i);
    }

    int res = 0, i = 0;
    while (true) {
      if (a.size() == 0 || b.size() == 0)
        break;

      if (i % 2 == 0) {
        vector<int>::iterator ite = upper_bound(a.begin(), a.end(), res);
        if (ite == a.end()) {
          res = 0;
        } else {
          res = *ite;
          a.erase(ite);
        }
      } else {
        vector<int>::iterator ite = upper_bound(b.begin(), b.end(), res);
        if (ite == b.end()) {
          res = 0;
        } else {
          res = *ite;
          b.erase(ite);
        }
      }

      i++;
    }

    cout << b.size() << endl;
    cout << a.size() << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n;
  while (cin >> n && n) {
    vector<int> a, b;
    bool used[205];
    memset(used, 0, sizeof(used));

    int x;
    rep(i, n) {
      cin >> x;
      used[x] = true;
      a.push_back(x);
    }

    sort(a.begin(), a.end());

    REP(i, 1, 2 * n + 1) {
      if (used[i])
        continue;
      b.push_back(i);
    }

    int res = 0, i = 0;
    while (true) {
      if (a.size() == 0 || b.size() == 0)
        break;

      if (i % 2 == 0) {
        vector<int>::iterator ite = upper_bound(a.begin(), a.end(), res);
        if (ite == a.end()) {
          res = 0;
        } else {
          res = *ite;
          a.erase(ite);
        }
      } else {
        vector<int>::iterator ite = upper_bound(b.begin(), b.end(), res);
        if (ite == b.end()) {
          res = 0;
        } else {
          res = *ite;
          b.erase(ite);
        }
      }

      i++;
    }

    cout << b.size() << endl;
    cout << a.size() << endl;
  }

  return 0;
}
Problem p00446 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  while (true) {
    cin >> n;
    vector<int> taro, hanako;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      taro.push_back(a);
    }
    sort(taro.begin(), taro.end());
    for (int j = 1; j < taro[0]; j++) {
      hanako.push_back(j);
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = taro[i] + 1; j < taro[i + 1]; j++) {
        hanako.push_back(j);
      }
    }
    for (int j = taro[n - 1] + 1; j <= 2 * n; j++) {
      hanako.push_back(j);
    }
    int top = 0;
    vector<int>::iterator min;
    while (true) {
      min = lower_bound(taro.begin(), taro.end(), top);
      if (min == taro.end()) {
        top = 0;
      } else {
        top = *min;
        taro.erase(min);
      }
      if (taro.empty())
        break;
      min = lower_bound(hanako.begin(), hanako.end(), top);
      if (min == hanako.end()) {
        top = 0;
      } else {
        top = *min;
        hanako.erase(min);
      }
      if (hanako.empty())
        break;
    }
    cout << hanako.size() << '\n' << taro.size() << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  while (true) {
    cin >> n;
    if (!n)
      break;
    vector<int> taro, hanako;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      taro.push_back(a);
    }
    sort(taro.begin(), taro.end());
    for (int j = 1; j < taro[0]; j++) {
      hanako.push_back(j);
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = taro[i] + 1; j < taro[i + 1]; j++) {
        hanako.push_back(j);
      }
    }
    for (int j = taro[n - 1] + 1; j <= 2 * n; j++) {
      hanako.push_back(j);
    }
    int top = 0;
    vector<int>::iterator min;
    while (true) {
      min = lower_bound(taro.begin(), taro.end(), top);
      if (min == taro.end()) {
        top = 0;
      } else {
        top = *min;
        taro.erase(min);
      }
      if (taro.empty())
        break;
      min = lower_bound(hanako.begin(), hanako.end(), top);
      if (min == hanako.end()) {
        top = 0;
      } else {
        top = *min;
        hanako.erase(min);
      }
      if (hanako.empty())
        break;
    }
    cout << hanako.size() << '\n' << taro.size() << endl;
  }
  return 0;
}
Problem p00446 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int n;
  while (scanf("%d", &n) != 0) {
    vector<int> t, h;
    for (int i = 0; i < n; i++) {
      int tmp;
      scanf("%d", &tmp);
      t.push_back(tmp);
    }
    for (int i = 0; i < 2 * n; i++) {
      bool find = false;
      for (int j = 0; j < n; j++) {
        if (t[j] == i + 1) {
          find = true;
        }
      }
      if (find == false) {
        h.push_back(i + 1);
      }
      find = false;
    }
    sort(t.begin(), t.end());
    sort(h.begin(), h.end());
    int b = 0;
    bool td = false;
    bool hd = false;
    while (1) {
      for (int i = 0; i < t.size(); i++) {
        if (t[i] > b) {
          b = t[i];
          t.erase(t.begin() + i);
          td = true;
          break;
        }
      }
      if (td == false) {
        b = 0;
      }
      td = false;
      if (t.size() == 0)
        break;
      for (int i = 0; i < h.size(); i++) {
        if (h[i] > b) {
          b = h[i];
          h.erase(h.begin() + i);
          hd = true;
          break;
        }
      }
      if (hd == false) {
        b = 0;
      }
      hd = false;
      if (h.size() == 0)
        break;
    }
    int ts = h.size();
    int hs = t.size();
    printf("%d\n%d\n", ts, hs);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int n;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    vector<int> t, h;
    for (int i = 0; i < n; i++) {
      int tmp;
      scanf("%d", &tmp);
      t.push_back(tmp);
    }
    for (int i = 0; i < 2 * n; i++) {
      bool find = false;
      for (int j = 0; j < n; j++) {
        if (t[j] == i + 1) {
          find = true;
        }
      }
      if (find == false) {
        h.push_back(i + 1);
      }
      find = false;
    }
    sort(t.begin(), t.end());
    sort(h.begin(), h.end());
    int b = 0;
    bool td = false;
    bool hd = false;
    while (1) {
      for (int i = 0; i < t.size(); i++) {
        if (t[i] > b) {
          b = t[i];
          t.erase(t.begin() + i);
          td = true;
          break;
        }
      }
      if (td == false) {
        b = 0;
      }
      td = false;
      if (t.size() == 0)
        break;
      for (int i = 0; i < h.size(); i++) {
        if (h[i] > b) {
          b = h[i];
          h.erase(h.begin() + i);
          hd = true;
          break;
        }
      }
      if (hd == false) {
        b = 0;
      }
      hd = false;
      if (h.size() == 0)
        break;
    }
    int ts = h.size();
    int hs = t.size();
    printf("%d\n%d\n", ts, hs);
  }

  return 0;
}
Problem p00446 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int n, a, t[100], h[100], b[101] = {};
    int c = 0, d = 0, e = 0, ba = 0, ta = 0, ha = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a;
      b[a]++;
    }
    for (int i = 1; i <= n * 2; i++) {
      if (b[i] == 0) {
        h[c] = i;
        c++;
      } else {
        t[d] = i;
        d++;
      }
    }
    while (1) {
      int f = 0;
      for (int i = 0; i < n; i++) {
        if (t[i] > ba) {
          ba = t[i];
          t[i] = 0;
          f++;
          ta++;
          break;
        }
      }
      if (f == 0)
        ba = 0;
      if (ta == n)
        break;
      f = 0;
      for (int i = 0; i < n; i++) {
        if (h[i] > ba) {
          ba = h[i];
          h[i] = 0;
          f++;
          ha++;
          break;
        }
      }
      if (f == 0)
        ba = 0;
      if (ha == n)
        break;
    }
    printf("%d\n%d\n", n - ha, n - ta);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int n, a, t[1000], h[1000], b[1000] = {};
    int c = 0, d = 0, ba = 0, ta = 0, ha = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a;
      b[a]++;
    }
    for (int i = 1; i <= n * 2; i++) {
      if (b[i] == 0) {
        h[c] = i;
        c++;
      } else {
        t[d] = i;
        d++;
      }
    }
    while (1) {
      int f = 0;
      for (int i = 0; i < n; i++) {
        if (t[i] > ba) {
          ba = t[i];
          t[i] = 0;
          f++;
          ta++;
          break;
        }
      }
      if (f == 0)
        ba = 0;
      if (ta == n)
        break;
      f = 0;
      for (int i = 0; i < n; i++) {
        if (h[i] > ba) {
          ba = h[i];
          h[i] = 0;
          f++;
          ha++;
          break;
        }
      }
      if (f == 0)
        ba = 0;
      if (ha == n)
        break;
    }
    printf("%d\n%d\n", n - ha, n - ta);
  }
  return 0;
}
Problem p00447 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ZERO(a) memset(a, 0, sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second

const int M = 1e6 + 10;
bool px[M], py[M];
vector<P> tg, stars; // (x,y)

void init(void) {
  ZERO(px);
  ZERO(py);
  tg.clear();
  stars.clear();
}

bool OK(int dx, int dy) {
  for (auto &t : tg) {
    if (!px[t.F + dx] or !py[t.S + dy])
      return false;
  }
  return true;
}

int main() {
  int m, n;
  while (cin >> m && m) {
    init();
    rep(i, m) {
      int x, y;
      scanf("%d%d", &x, &y);
      tg.pb(mp(x, y));
    }
    cin >> n;
    rep(i, n) {
      int x, y;
      scanf("%d%d", &x, &y);
      stars.pb(mp(x, y));
      px[x] = py[y] = true;
    }

    for (auto &star : stars) {
      int diffx = star.F - tg[0].F;
      int diffy = star.S - tg[0].S;

      if (OK(diffx, diffy)) {
        printf("%d %d\n", diffx, diffy);
        goto out;
      }
    }
  out:
    m += 0;
  }

  return 0;
}

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

typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define ZERO(a) memset(a, 0, sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second

const int M = 1e6 + 10;
bool px[M], py[M];
vector<P> tg, stars; // (x,y)

void init(void) {
  ZERO(px);
  ZERO(py);
  tg.clear();
  stars.clear();
}

bool OK(int dx, int dy) {
  for (auto &t : tg) {
    if (t.F + dx > 1e6 or t.S + dy > 1e6)
      return false;
    if (!px[t.F + dx] or !py[t.S + dy])
      return false;
  }
  return true;
}

int main() {
  int m, n;
  while (cin >> m && m) {
    init();
    rep(i, m) {
      int x, y;
      scanf("%d%d", &x, &y);
      tg.pb(mp(x, y));
    }
    cin >> n;
    rep(i, n) {
      int x, y;
      scanf("%d%d", &x, &y);
      stars.pb(mp(x, y));
      px[x] = py[y] = true;
    }

    for (auto &star : stars) {
      int diffx = star.F - tg[0].F;
      int diffy = star.S - tg[0].S;

      if (OK(diffx, diffy)) {
        printf("%d %d\n", diffx, diffy);
        goto out;
      }
    }
  out:
    m += 0;
  }

  return 0;
}
Problem p00447 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>

#define x first
#define y second

using namespace std;
typedef pair<int, int> P;
P hosi[100];
P hosi2[100];
P shift;
bool flag;
int main() {
  int m, n;
  while (1) {
    cin >> m;
    if (!m)
      break;
    for (int i = 0; i < m; i++) {
      cin >> hosi[i].x >> hosi[i].y;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> hosi2[i].x >> hosi2[i].y;
    }

    sort(hosi, hosi + m);
    sort(hosi2, hosi2 + n);

    for (int i = 0; i < n; i++) {
      flag = true;
      shift.x = hosi2[i].x - hosi[0].x;
      shift.y = hosi2[i].y - hosi[0].y;
      for (int j = 0; j < m; j++) {
        if (!binary_search(hosi2, hosi2 + n,
                           P(hosi[j].x + shift.x, hosi[j].y + shift.y))) {
          flag = false;
          break;
        }
      }
      if (flag == true)
        break;
    }
    cout << shift.x << " " << shift.y << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>

#define x first
#define y second

using namespace std;
typedef pair<int, int> P;
P hosi[220];
P hosi2[1010];
P shift;
bool flag;
int main() {
  int m, n;
  while (1) {
    cin >> m;
    if (!m)
      break;
    for (int i = 0; i < m; i++) {
      cin >> hosi[i].x >> hosi[i].y;
    }

    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> hosi2[i].x >> hosi2[i].y;
    }

    sort(hosi, hosi + m);
    sort(hosi2, hosi2 + n);

    for (int i = 0; i < n; i++) {
      flag = true;
      shift.x = hosi2[i].x - hosi[0].x;
      shift.y = hosi2[i].y - hosi[0].y;
      for (int j = 0; j < m; j++) {
        if (!binary_search(hosi2, hosi2 + n,
                           P(hosi[j].x + shift.x, hosi[j].y + shift.y))) {
          flag = false;
          break;
        }
      }
      if (flag == true)
        break;
    }
    cout << shift.x << " " << shift.y << endl;
  }
}
Problem p00447 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

#define sint(i) scanf("%d", &i)
#define sintt(i, j) scanf("%d%d", &i, &j)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int nx[200];
  int ny[200];
  int mx[200];
  int my[200];

  while (1) {
    int n;
    sint(n);

    if (n == 0) {
      break;
    }

    rep(i, n) { sintt(nx[i], ny[i]); }

    int m;
    sint(m);
    rep(i, m) { sintt(mx[i], my[i]); }

    rep(i, m) {
      rep(j, m) {
        int temp;
        if (mx[i] < mx[j]) {
          temp = mx[i];
          mx[i] = mx[j];
          mx[j] = temp;

          temp = my[i];
          my[i] = my[j];
          my[j] = temp;
        }
      }
    }

    rep(i, m) {
      int dx = mx[i] - nx[0];
      int dy = my[i] - ny[0];

      int flgg = 1;
      rep(j, n) {
        int flg = 0;
        rep(k, m) {
          if (nx[j] + dx == mx[k] && ny[j] + dy == my[k]) {
            flg = 1;
            break;
          }
        }

        if (flg == 0) {
          flgg = 0;
          break;
        }
      }

      if (flgg == 1) {
        printf("%d %d\n", dx, dy);
        break;
      }
    }
  }
}

/*
5
8 5
6 4
4 3
7 10
0 10
10
10 5
2 7
9 7
8 10
10 2
1 2
8 1
6 7
6 0
0 9

2
1 1
2 2
3
1 2
2 3
1 1

*/

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

#define sint(i) scanf("%d", &i)
#define sintt(i, j) scanf("%d%d", &i, &j)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int nx[200];
  int ny[200];
  int mx[1000];
  int my[1000];

  while (1) {
    int n;
    sint(n);

    if (n == 0) {
      break;
    }

    rep(i, n) { sintt(nx[i], ny[i]); }

    int m;
    sint(m);
    rep(i, m) { sintt(mx[i], my[i]); }

    rep(i, m) {
      rep(j, m) {
        int temp;
        if (mx[i] < mx[j]) {
          temp = mx[i];
          mx[i] = mx[j];
          mx[j] = temp;

          temp = my[i];
          my[i] = my[j];
          my[j] = temp;
        }
      }
    }

    rep(i, m) {
      int dx = mx[i] - nx[0];
      int dy = my[i] - ny[0];

      int flgg = 1;
      rep(j, n) {
        int flg = 0;
        rep(k, m) {
          if (nx[j] + dx == mx[k] && ny[j] + dy == my[k]) {
            flg = 1;
            break;
          }
        }

        if (flg == 0) {
          flgg = 0;
          break;
        }
      }

      if (flgg == 1) {
        printf("%d %d\n", dx, dy);
        break;
      }
    }
  }
}

/*
5
8 5
6 4
4 3
7 10
0 10
10
10 5
2 7
9 7
8 10
10 2
1 2
8 1
6 7
6 0
0 9

2
1 1
2 2
3
1 2
2 3
1 1

*/
Problem p00447 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
typedef pair<int, int> PA;
int main() {
  int m, n;
  PA seiza[200], hosi[200];
  while (scanf("%d", &m), m) {
    for (int i = 0; i < m; i++)
      scanf("%d %d", &seiza[i].first, &seiza[i].second);
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      scanf("%d %d", &hosi[i].first, &hosi[i].second);
    sort(hosi, hosi + n);
    for (int i = 0; i < n; i++) {
      int x = hosi[i].first - seiza[0].first;
      int y = hosi[i].second - seiza[0].second;
      for (int j = 1; j < m; j++) {
        if (binary_search(hosi, hosi + n,
                          PA(seiza[j].first + x, seiza[j].second + y))) {
          if (j == m - 1)
            printf("%d %d\n", x, y);
        } else
          break;
      }
    }
  }
  return (0);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
typedef pair<int, int> PA;
int main() {
  int m, n;
  PA seiza[200], hosi[1001];
  while (scanf("%d", &m), m) {
    for (int i = 0; i < m; i++)
      scanf("%d %d", &seiza[i].first, &seiza[i].second);
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
      scanf("%d %d", &hosi[i].first, &hosi[i].second);
    sort(hosi, hosi + n);
    for (int i = 0; i < n; i++) {
      int x = hosi[i].first - seiza[0].first;
      int y = hosi[i].second - seiza[0].second;
      for (int j = 1; j < m; j++) {
        if (binary_search(hosi, hosi + n,
                          PA(seiza[j].first + x, seiza[j].second + y))) {
          if (j == m - 1)
            printf("%d %d\n", x, y);
        } else
          break;
      }
    }
  }
  return (0);
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  while (1) {
    int h, w;
    cin >> h >> w;
    if (!h && !w)
      break;

    bool mp[11][1001];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];

    int ans = 0;
    for (int i = 0; i < (1 << h); i++) {
      int cnt = 0;
      for (int j = 0; j < w; j++) {
        int c = 0;
        for (int k = 0; k < h; k++)
          c += ((i >> k) & 1) ? !mp[k][j] : mp[k][j];
        cnt += max(c, h - c);
      }
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main() {
  while (1) {
    int h, w;
    cin >> h >> w;
    if (!h && !w)
      break;
    bool mp[11][10001];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];

    int ans = 0;
    for (int i = 0; i < (1 << h); i++) {
      int cnt = 0;
      for (int j = 0; j < w; j++) {
        int c = 0;
        for (int k = 0; k < h; k++)
          c += ((i >> k) & 1) ? !mp[k][j] : mp[k][j];
        cnt += max(c, h - c);
      }
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p00448 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
// #define int long long
using namespace std;
typedef long long ll;

int R, C;
int a[10][10000];

signed main() {
  while (true) {
    cin >> R >> C;
    // rep(i,R) rep(j,C) cin >> a[i][j];
    rep(i, R) rep(j, C) scanf("%d", &a[i][j]);

    int ans = 0;
    for (int i = 0; i < 1 << R; i++) {
      int temp = 0;
      rep(j, C) {
        int one = 0;
        rep(k, R) {
          if ((i >> k & 1) && (a[k][j] == 0))
            one++;
          else if (!(i >> k & 1) && (a[k][j] == 1))
            one++;
        }
        temp += max(one, R - one);
      }
      ans = max(ans, temp);
    }

    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
// #define int long long
using namespace std;
typedef long long ll;

int R, C;
int a[10][10000];

signed main() {
  while (true) {
    cin >> R >> C;
    if (R == 0 && C == 0)
      break;
    // rep(i,R) rep(j,C) cin >> a[i][j];
    rep(i, R) rep(j, C) scanf("%d", &a[i][j]);

    int ans = 0;
    for (int i = 0; i < 1 << R; i++) {
      int temp = 0;
      rep(j, C) {
        int one = 0;
        rep(k, R) {
          if ((i >> k & 1) && (a[k][j] == 0))
            one++;
          else if (!(i >> k & 1) && (a[k][j] == 1))
            one++;
        }
        temp += max(one, R - one);
      }
      ans = max(ans, temp);
    }

    cout << ans << endl;
  }
}
Problem p00448 (C++)

Buggy submission (status: 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;
    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;
}

Fixed submission:
#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;
}
Problem p00448 (C++)

Buggy submission (status: 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 >= 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;
}

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 >= 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;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define FOR(i, j, k) for (int i = j; i < (int)k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)

typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;

const int MOD = 1e9 + 7;
const int dy[] = {0, 0, 1, -1};
const int dx[] = {1, -1, 0, 0};

template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }

int r, c;
int n[5][10000], ans = 0;
bool use[10];

void dfs(int now) {
  if (now == r) {
    int res = 0;
    rep(j, c) {
      int count = 0;
      rep(i, r) {
        if ((use[i] && n[i][j]) || (!use[i] && !n[i][j]))
          ++count;
      }
      res += (r - count < count) ? count : r - count;
    }
    chmax(ans, res);
    return;
  }
  use[now] = true;
  dfs(now + 1);
  use[now] = false;
  dfs(now + 1);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while ((cin >> r >> c), r | c) {
    ans = 0;
    rep(i, r) rep(j, c) cin >> n[i][j];
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define FOR(i, j, k) for (int i = j; i < (int)k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define INF (1 << 30)

typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> Pi;

const int MOD = 1e9 + 7;
const int dy[] = {0, 0, 1, -1};
const int dx[] = {1, -1, 0, 0};

template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }

int r, c;
int n[10][10000], ans = 0;
bool use[10];

void dfs(int now) {
  if (now == r) {
    int res = 0;
    rep(j, c) {
      int count = 0;
      rep(i, r) {
        if ((use[i] && n[i][j]) || (!use[i] && !n[i][j]))
          ++count;
      }
      res += (r - count < count) ? count : r - count;
    }
    chmax(ans, res);
    return;
  }
  use[now] = true;
  dfs(now + 1);
  use[now] = false;
  dfs(now + 1);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while ((cin >> r >> c), r | c) {
    ans = 0;
    rep(i, r) rep(j, c) cin >> n[i][j];
    dfs(0);
    cout << ans << endl;
  }
  return 0;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
int R, C;
const int MAX = 10000;
bool senbei[10][MAX];
int count() {
  /*cout << "begin" << endl;
  for(int i = 0;i < R;i++){
    for(int j = 0;j < C;j++){
      cout << (int)senbei[i][j];
    }
    cout << endl;
  }
  */

  int ans = 0;

  for (int i = 0; i < C; i++) {
    int num = 0;
    for (int j = 0; j < R; j++) {
      if (senbei[j][i])
        num++;
    }
    ans += max(num, R - num);
  }

  // cout << ans << endl;
  // cout << "end" << endl;
  return ans;
}

int dfs(int r) {
  if (r == 0)
    return count();
  int ans = dfs(r - 1);
  for (int i = 0; i < C; ++i)
    senbei[r][i] = !senbei[r][i];
  ans = max(dfs(r - 1), ans);
  for (int i = 0; i < C; ++i)
    senbei[r][i] = !senbei[r][i];
  return ans;
}
int main() {
  ios::sync_with_stdio(false);

  while (cin >> R >> C && (R + C) > 0) {
    for (int r = 0; r < R; ++r) {
      for (int c = 0; c < C; ++c) {
        bool b;
        cin >> b;
        senbei[r][c] = b > 0 ? true : false;
      }
    }
    cout << dfs(R) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
int R, C;
const int MAX = 10000;
bool senbei[MAX][MAX];
int count() {
  /*cout << "begin" << endl;
  for(int i = 0;i < R;i++){
    for(int j = 0;j < C;j++){
      cout << (int)senbei[i][j];
    }
    cout << endl;
  }
  */

  int ans = 0;

  for (int i = 0; i < C; i++) {
    int num = 0;
    for (int j = 0; j < R; j++) {
      if (senbei[j][i])
        num++;
    }
    ans += max(num, R - num);
  }

  // cout << ans << endl;
  // cout << "end" << endl;
  return ans;
}

int dfs(int r) {
  if (r == 0)
    return count();
  int ans = dfs(r - 1);
  for (int i = 0; i < C; ++i)
    senbei[r][i] = !senbei[r][i];
  ans = max(dfs(r - 1), ans);
  for (int i = 0; i < C; ++i)
    senbei[r][i] = !senbei[r][i];
  return ans;
}
int main() {
  ios::sync_with_stdio(false);

  while (cin >> R >> C && (R + C) > 0) {
    for (int r = 0; r < R; ++r) {
      for (int c = 0; c < C; ++c) {
        bool b;
        cin >> b;
        senbei[r][c] = b > 0 ? true : false;
      }
    }
    cout << dfs(R) << endl;
  }

  return 0;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e4 + 10;
bitset<maxn> cookie[10];

int main() {
  int R, C;
  while (cin >> R >> C && (R || C)) {
    int i, j;
    for (int i = 0; i < R; i++)
      for (int j = 0; j < C; j++) {
        bool upward;
        cin >> upward;
        cookie[i][j] = upward;
      }

    int r = 1 << R;
    int res = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < R; j++) {
        if (i & (1 << j))
          cookie[i].flip();
      }

      int ans = 0;
      for (int j = 0; j < C; j++) {
        int up = 0;
        for (int k = 0; k < R; k++) {
          if (cookie[k][j])
            up++;
        }
        ans += max(up, R - up);
      }

      res = max(ans, res);

      for (int j = 0; j < R; j++) {
        if (i & (1 << j))
          cookie[j].flip();
      }
    }
    printf("%d\n", res);
  }
}

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

const int maxn = 1e4 + 10;
bitset<maxn> cookie[10];

int main() {
  int R, C;
  while (cin >> R >> C && (R || C)) {
    int i, j;
    for (int i = 0; i < R; i++)
      for (int j = 0; j < C; j++) {
        bool upward;
        cin >> upward;
        cookie[i][j] = upward;
      }

    int r = 1 << R;
    int res = 0;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < R; j++) {
        if (i & (1 << j))
          cookie[j].flip();
      }

      int ans = 0;
      for (int j = 0; j < C; j++) {
        int up = 0;
        for (int k = 0; k < R; k++) {
          if (cookie[k][j])
            up++;
        }
        ans += max(up, R - up);
      }

      res = max(ans, res);

      for (int j = 0; j < R; j++) {
        if (i & (1 << j))
          cookie[j].flip();
      }
    }
    printf("%d\n", res);
  }
}
Problem p00448 (C++)

Buggy submission (status: 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;
    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;
}

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;
    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;
}
Problem p00448 (C++)

Buggy submission (status: 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<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;
}

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<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;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;
int sen[20][10010];
int tate, yoko;
int cou(int sum[10010][2], int row) {
  if (row == tate) {
    int ans = 0;
    for (int i = 0; i < yoko; i++)
      ans = ans + max(sum[i][0], sum[i][1]);
    return ans;
  }
  int sum0[10010][2] = {0}, sum1[10010][2] = {0}, ans0, ans1;
  for (int i = 0; i < yoko; i++) {
    sum0[i][0] = sum[i][0];
    sum0[i][1] = sum[i][1];
    sum1[i][0] = sum[i][0];
    sum1[i][1] = sum[i][1];
    if (sen[i][row] == 0) {
      sum0[i][0]++;
      sum1[i][1]++;
    } else {
      sum0[i][1]++;
      sum1[i][0]++;
    }
  }
  ans0 = cou(sum0, row + 1);
  ans1 = cou(sum1, row + 1);
  return max(ans0, ans1);
}
int main() {
  while (scanf("%d%d", &tate, &yoko), tate + yoko) {
    int sam[10010][2] = {0};
    for (int i = 0; i < tate; i++)
      for (int j = 0; j < yoko; j++)
        scanf("%d", &sen[j][i]);
    printf("%d\n", cou(sam, 0));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int sen[10010][20];
int tate, yoko;
int cou(int sum[10010][2], int row) {
  if (row == tate) {
    int ans = 0;
    for (int i = 0; i < yoko; i++)
      ans = ans + max(sum[i][0], sum[i][1]);
    return ans;
  }
  int sum0[10010][2] = {0}, sum1[10010][2] = {0}, ans0, ans1;
  for (int i = 0; i < yoko; i++) {
    sum0[i][0] = sum[i][0];
    sum0[i][1] = sum[i][1];
    sum1[i][0] = sum[i][0];
    sum1[i][1] = sum[i][1];
    if (sen[i][row] == 0) {
      sum0[i][0]++;
      sum1[i][1]++;
    } else {
      sum0[i][1]++;
      sum1[i][0]++;
    }
  }
  ans0 = cou(sum0, row + 1);
  ans1 = cou(sum1, row + 1);
  return max(ans0, ans1);
}
int main() {
  while (scanf("%d%d", &tate, &yoko), tate + yoko) {
    int sam[10010][2] = {0};
    for (int i = 0; i < tate; i++)
      for (int j = 0; j < yoko; j++)
        scanf("%d", &sen[j][i]);
    printf("%d\n", cou(sam, 0));
  }
  return 0;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
int R, C;
const int MAX_R = 12;
const int MAX_C = 10020;
bool omote[MAX_R][MAX_C];
int avai[MAX_C];
int avai_ori[MAX_C]; // 変化させない
int main(int argc, const char *argv[]) {
  while (true) {
    scanf("%d %d", &R, &C);
    if (R == 0 && C == 0)
      break;
    int ret = 0;
    memset(avai_ori, 0, sizeof(avai_ori));
    memset(avai, 0, sizeof(avai));
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        int n;
        scanf("%d", &n); // 表なら1,裏なら0
        omote[i][j] = n;
        if (n == 0) { // 裏の場合
          avai[j]++;
          avai_ori[j]++;
        }
      }
    }

    for (int i = 0; i < (1 << R); i++) {
      for (int j = 0; j < C; j++)
        avai[j] = avai_ori[j];
      for (int j = 0; j < R; j++) {
        if ((i >> j) & 1) { // j行目を裏返す場合
          for (int k = 0; k < C; k++) {
            if (omote[i][j]) {
              if (omote[j][k])
                avai[k]++;
              else
                avai[k]--;
            }
          }
        }
      }
      int sum = 0;
      for (int j = 0; j < C; j++) {
        sum += max(avai[j], R - avai[j]);
      }
      ret = max(ret, sum);
    }
    printf("%d\n", ret);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
int R, C;
const int MAX_R = 12;
const int MAX_C = 10020;
bool omote[MAX_R][MAX_C];
int avai[MAX_C];
int avai_ori[MAX_C]; // 変化させない
int main(int argc, const char *argv[]) {
  while (true) {
    scanf("%d %d", &R, &C);
    if (R == 0 && C == 0)
      break;
    int ret = 0;
    memset(avai_ori, 0, sizeof(avai_ori));
    memset(avai, 0, sizeof(avai));
    for (int i = 0; i < R; i++) {
      for (int j = 0; j < C; j++) {
        int n;
        scanf("%d", &n); // 表なら1,裏なら0
        omote[i][j] = n;
        if (n == 0) { // 裏の場合
          avai[j]++;
          avai_ori[j]++;
        }
      }
    }

    for (int i = 0; i < (1 << R); i++) {
      for (int j = 0; j < C; j++)
        avai[j] = avai_ori[j];
      for (int j = 0; j < R; j++) {
        if ((i >> j) & 1) { // j行目を裏返す場合
          for (int k = 0; k < C; k++) {
            if (omote[j][k])
              avai[k]++;
            else
              avai[k]--;
          }
        }
      }
      int sum = 0;
      for (int j = 0; j < C; j++) {
        sum += max(avai[j], R - avai[j]);
      }
      ret = max(ret, sum);
    }
    printf("%d\n", ret);
  }

  return 0;
}
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int r, c;
vector<vector<int>> field;

int search() {
  int counter;
  int max_num = 0;

  counter = 1 << r;
  for (int i = 0; i < counter; i++) {
    int num = 0;

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }

    for (int j = 0; j < c; j++) {
      int co_num = 0;
      for (int k = 0; k < r; k++) {
        co_num += field[k][j];
      }

      if (co_num <= r / 2) {
        num += r - co_num;
      } else {
        num += co_num; /*reverse*/
      }
    }

    if (max_num < num) {
      max_num = num;
    }

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }
  }

  return max_num;
}

int main(int argc, char **argv) {
  cin >> r >> c;

  while (r != 0 && c != 0) {
    for (int i = 0; i < r; i++) {
      field.push_back(vector<int>(c));
      for (int j = 0; j < c; j++) {
        cin >> field[i][j];
      }
    }
    cout << search() << endl;
    cin >> r >> c;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int r, c;
vector<vector<int>> field;

int search() {
  int counter;
  int max_num = 0;

  counter = 1 << r;
  for (int i = 0; i < counter; i++) {
    int num = 0;

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }

    for (int j = 0; j < c; j++) {
      int co_num = 0;
      for (int k = 0; k < r; k++) {
        co_num += field[k][j];
      }

      if (co_num <= r / 2) {
        num += r - co_num;
      } else {
        num += co_num; /*reverse*/
      }
    }

    if (max_num < num) {
      max_num = num;
    }

    for (int j = 0; j < r; j++) {
      if ((i >> j) & 1) {
        for (int k = 0; k < c; k++) {
          field[j][k] ^= 1;
        }
      }
    }
  }

  return max_num;
}

int main(int argc, char **argv) {
  cin >> r >> c;

  while (r != 0 && c != 0) {
    field.clear();
    for (int i = 0; i < r; i++) {
      field.push_back(vector<int>(c));
      for (int j = 0; j < c; j++) {
        cin >> field[i][j];
      }
    }
    cout << search() << endl;
    cin >> r >> c;
  }
  return 0;
}