alexjercan/bugnet huggingface dataset:
Problem p01646 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second

typedef long long ll;

int main() {
  int n;
  char s[1024][256];
  while (~scanf("%d", &n) && n) {
    rep(i, n) scanf("%s", s[i]);
    set<char> st[256];
    bool ans = true;
    rep(i, n) {
      REP(j, i + 1, n) {
        bool f = true;
        int li = strlen(s[i]), lj = strlen(s[j]);
        int l = min(li, lj);
        rep(k, l) {
          if (s[i][k] != s[j][k]) {
            f = false;
            if (st[s[j][k]].count(s[i][k])) {
              ans = false;
              break;
            }
            st[s[i][k]].insert(s[j][k]);
            break;
          }
        }
        if (f && li > lj)
          ans = false;
      }
    }
    printf("%s\\n", (ans ? "yes" : "no"));
  }
}

Error: 0

Fixed submission:

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

#define REP(i, s, e) for (int i = (int)s; i < (int)e; i++)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define fi first
#define se second

typedef long long ll;

int main() {
  int n;
  char s[256][256];
  while (~scanf("%d", &n) && n) {
    rep(i, n) scanf("%s", s[i]);
    set<char> st[256];
    bool ans = true;
    rep(i, n) {
      REP(j, i + 1, n) {
        bool f = true;
        int li = strlen(s[i]), lj = strlen(s[j]);
        int l = min(li, lj);
        rep(k, l) {
          if (s[i][k] != s[j][k]) {
            f = false;
            if (st[s[j][k]].count(s[i][k])) {
              ans = false;
              break;
            }
            st[s[i][k]].insert(s[j][k]);
            break;
          }
        }
        if (f && li > lj)
          ans = false;
      }
    }
    printf("%s\\n", (ans ? "yes" : "no"));
  }
}
Problem p01646 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    vector<string> S(N);
    for (auto &s : S)
      cin >> s;
    int G[26][26];
    fill(G[0], G[26], 0);
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        string s1 = S[i], s2 = S[j];
        if (s1 == s2)
          continue;
        int ite = 0;
        while (ite < min(s1.size(), s2.size()) && s1[ite] == s2[ite])
          ite++;
        if (ite == s1.size())
          continue;
        if (ite == s2.size()) {
          flag = false;
          continue;
        }
        G[s1[ite] - 'a'][s2[ite] - 'a'] = 1;
      }
    }
    for (int k = 0; k < 26; k++) {
      for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
          G[i][j] |= G[i][k] & G[k][j];
        }
      }
    }
    for (int i = 0; i < 26; i++) {
      if (G[i][i])
        flag = false;
    }
    cout << (flag ? "yes" : "no") << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    vector<string> S(N);
    for (auto &s : S)
      cin >> s;
    int G[26][26];
    fill(G[0], G[N], 0);
    bool flag = true;
    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        string s1 = S[i], s2 = S[j];
        if (s1 == s2)
          continue;
        for (int ite = 0; ite < max(s1.size(), s2.size()); ite++) {
          if (ite >= s2.size()) {
            flag = false;
            break;
          } else {
            if (ite >= s1.size())
              break;
            else {
              if (s1[ite] == s2[ite])
                continue;
              else {
                G[s1[ite] - 'a'][s2[ite] - 'a'] = 1;
                break;
              }
            }
          }
        }
      }
    }
    for (int k = 0; k < 26; k++) {
      for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
          G[i][j] |= G[i][k] & G[k][j];
        }
      }
    }
    for (int i = 0; i < 26; i++) {
      if (G[i][i])
        flag = false;
    }
    cout << (flag ? "yes" : "no") << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
class DisjointSet {
private:
  vector<int> rank, p;
  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }

public:
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      p[i] = i, rank[i] = 0;
  }
  void Union(int x, int y) {
    if (Find(x) != Find(y))
      link(Find(x), Find(y));
  }
  int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); }
};

struct edge {
  int u, v, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
};

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<edge> edges;
    for (int i = 0; i < m; i++) {
      int s, t, c;
      cin >> s >> t >> c;
      edges.push_back((edge){s - 1, t - 1, c});
    }
    sort(edges.begin(), edges.end());
    DisjointSet UnionFind(n);
    vector<int> cost;
    for (int i = 0; i < edges.size(); i++) {
      edge &e = edges[i];
      if (UnionFind.Find(e.u) != UnionFind.Find(e.v)) {
        UnionFind.Union(e.u, e.v);
        cost.push_back(e.cost);
      }
    }
    if (cost.size() % 2 == 0) {
      cout << (cost[cost.size() / 2] + cost[cost.size() / 2 + 1]) / 2 << endl;
    } else {
      cout << cost[cost.size() / 2] << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
class DisjointSet {
private:
  vector<int> rank, p;
  void link(int x, int y) {
    if (rank[x] > rank[y]) {
      p[y] = x;
    } else {
      p[x] = y;
      if (rank[x] == rank[y])
        rank[y]++;
    }
  }

public:
  DisjointSet(int size) {
    rank.resize(size, 0);
    p.resize(size, 0);
    for (int i = 0; i < size; i++)
      p[i] = i, rank[i] = 0;
  }
  void Union(int x, int y) {
    if (Find(x) != Find(y))
      link(Find(x), Find(y));
  }
  int Find(int x) { return (x != p[x] ? p[x] = Find(p[x]) : p[x]); }
};

struct edge {
  int u, v, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
};

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<edge> edges;
    for (int i = 0; i < m; i++) {
      int s, t, c;
      cin >> s >> t >> c;
      edges.push_back((edge){s, t, c});
    }
    sort(edges.begin(), edges.end());
    DisjointSet UnionFind(n);
    vector<int> cost;
    for (int i = 0; i < edges.size(); i++) {
      edge &e = edges[i];
      if (UnionFind.Find(e.u) != UnionFind.Find(e.v)) {
        UnionFind.Union(e.u, e.v);
        cost.push_back(e.cost);
      }
    }
    if (cost.size() % 2 == 0) {
      cout << (cost[cost.size() / 2] + cost[cost.size() / 2 + 1]) / 2 << endl;
    } else {
      cout << cost[cost.size() / 2] << endl;
    }
  }
}
Problem p01648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define eps 1e-9
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.begin())
#define rep(i, j, k) for (int i = j; i <= k; i++)
#define MAXN 10005
#define MAXM 40005
#define INF 0x3fffffff
#define PB push_back
#define MP make_pair
#define X first
#define Y second
#define clr(x, y) memset(x, y, sizeof(x));
using namespace std;
typedef long long LL;
int i, j, k, n, m, x, y, T, ans, big, cas, num, len;
bool flag;

int edge, head[MAXN];
struct node {
  int x, y, w;
} p[10005];

bool cmp(node a, node b) { return a.w < b.w; }

int pre[10005]; //??根?点，起始??pre[i]=i
int find(int u) //?找u?点??集合的根?点
{
  int r = u;
  while (pre[r] != r)
    r = pre[r]; // 沿着?向上不断找father，直到找到根?点root (存入r中)
  int i = u, j;
  while (
      i !=
      r) // 路径??，即把向上找???的?个?点（重新沿?点向上找一遍），他?的father都??root
  {
    j = pre[i];
    pre[i] = r;
    i = j;
  }
  return r; // 返回根?点的?号
}

int main() {
  while (scanf("%d%d", &n, &m), n + m) {
    memset(head, -1, sizeof(head));
    edge = 0;
    for (i = 0; i < m; i++) {
      scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].w);
    }
    sort(p, p + m, cmp);
    for (i = 1; i <= n; i++)
      pre[i] = i;
    int t = n / 2;
    num = 0;
    for (i = 0; i < m; i++) {
      int fx = find(p[i].x);
      int fy = find(p[i].y);
      if (fx != fy) {
        pre[fx] = fy;
        num++;
        if (num == t) {
          printf("%d\\n", p[i].w);
          break;
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int main() {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n + m == 0)
      break;
    int num[1011] = {0};
    for (int i = 0; i < m; i++) {
      scanf("%*d%*d%d", num + i);
    }
    sort(num, num + m);
    printf("%d\\n", num[n / 2 - 1]);
  }
}
Problem p01656 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
string name[10000];
int y, a, b, n;
string S[10000];
int main() {
  S[0] = "kogakubu10gokan";
  cin >> a >> b;
  n = 0;
  for (int i = 1; i <= a; i++) {
    cin >> y >> S[i];
    for (int j = n; j < y; j++) {
      name[j] = S[i - 1];
    }
    n = y;
  }
  for (int i = n; i < 9999; i++) {
    name[i] = S[a];
  }
  cout << name[b] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
string name[10000];
int y, a, b, n;
string S[10000];
int main() {
  S[0] = "kogakubu10gokan";
  cin >> a >> b;
  for (int i = 1; i <= a; i++) {
    cin >> y >> S[i];
    for (int j = n; j < y; j++) {
      name[j - 1] = S[i - 1];
    }
    n = y;
  }
  for (int i = n; i < 9999; i++) {
    name[i] = S[a];
  }
  cout << name[b] << endl;
  return 0;
}
Problem p01656 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  map<int, string> nm;
  nm[0] = "kogakubu10gokan";
  for (int i = 0; i < n; i++) {
    int num;
    string s;
    cin >> num >> s;
    nm[num] = s;
  }
  bool ju = true;
  map<int, string>::iterator itr = ++nm.begin();
  for (; itr != nm.end(); itr++) {
    if (itr->first > q) {
      cout << (--itr)->second << endl;
      ju = false;
      break;
    }
  }
  if (ju)
    cout << (--nm.end())->second << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  map<int, string> nm;
  nm[0] = "kogakubu10gokan";
  for (int i = 0; i < n; i++) {
    int num;
    string s;
    cin >> num >> s;
    nm[num] = s;
  }
  bool ju = true;
  map<int, string>::iterator itr = ++nm.begin();
  for (; itr != nm.end(); itr++) {
    if (itr->first > q) {
      cout << (--itr)->second << endl;
      ju = false;
      break;
    }
  }
  if (ju)
    cout << nm.end()->second << endl;
}
Problem p01656 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (ll i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (ll i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vvi;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

int main() {
  int n, m;
  cin >> n >> m;
  map<int, string> mp;
  int t;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> t >> s;
    mp[t] = s;
  }
  mp[0] = "kogakubu10gokan";
  auto it = mp.upper_bound(m);
  it--;
  cout << it->se << endl;
}

Error: 0

Fixed submission:

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

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (ll i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)
#define REPE(i, s, e) for (ll i = s; i > e; i--)
#define repe(i, n) REPE(i, n, -1)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vvi;

const int IMAX = ((1 << 30) - 1) * 2 + 1;
const int INF = 100000000;
const double EPS = 1e-10;
// int mod=1000000007

int main() {
  int n, m;
  cin >> n >> m;
  map<int, string> mp;
  int t;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> t >> s;
    mp[t] = s;
  }
  auto it = mp.upper_bound(m);
  it--;
  cout << it->se << endl;
}
Problem p01657 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  int n, m, x;
  int l[10], r[10], s[10];
  while (cin >> n >> x >> m) {
    for (int i = 0; i < m; i++) {
      cin >> l[i] >> r[i] >> s[i];
    }
    x++;
    int end = 1;
    for (int i = 0; i < n; i++)
      end *= x;
    int maxSum = -1;
    int ans[10];
    int res[10];
    for (int i = 0; i < end; i++) {
      int t = i;
      for (int j = 0; j < n; j++) {
        ans[j] = t % x;
        t /= x;
      }
      bool check = true;
      for (int j = 0; j < m; j++) {
        int sum = 0;
        for (int k = l[j]; k <= r[j]; k++) {
          sum += ans[k - 1];
        }
        if (sum != s[j])
          check = false;
      }
      if (check) {
        int allSum = 0;
        for (int j = 0; j < n; j++)
          allSum += ans[j];
        if (allSum > maxSum) {
          maxSum = allSum;
          for (int j = 0; j < n; j++)
            res[j] = ans[j];
        }
      }
    }
    if (maxSum >= 0) {
      for (int i = 0; i < n - 1; i++)
        cout << res[i] << " ";
      cout << res[n - 1] << endl;
    } else {
      cout << -1 << endl;
    }
  }
}

Error: WA

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, m, x;
  int l[10], r[10], s[10];
  while (cin >> n >> x >> m) {
    for (int i = 0; i < n; i++) {
      cin >> l[i] >> r[i] >> s[i];
    }
    x++;
    int end = 1;
    for (int i = 0; i < n; i++)
      end *= x;
    int maxSum = -1;
    int ans[10];
    int res[10];
    for (int i = 0; i < end; i++) {
      int t = i;
      for (int j = 0; j < n; j++) {
        ans[j] = t % x;
        t /= x;
      }
      bool check = true;
      for (int j = 0; j < m; j++) {
        int sum = 0;
        for (int k = l[j]; k <= r[j]; k++) {
          sum += ans[k - 1];
        }
        if (sum != s[j])
          check = false;
      }
      if (check) {
        int allSum = 0;
        for (int j = 0; j < n; j++)
          allSum += ans[j];
        if (allSum > maxSum) {
          maxSum = allSum;
          for (int j = 0; j < n; j++)
            res[j] = ans[j];
        }
      }
    }
    if (maxSum >= 0) {
      for (int i = 0; i < n - 1; i++)
        cout << res[i] << " ";
      cout << res[n - 1] << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
Problem p01657 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); it++)

typedef vector<int> vi;

pair<vi, int> solve(int n, int x, int m, vi const &l, vi const &r,
                    vi const &s) {
  vi ans;
  int ans_sum = -1;
  int end = x + 1;
  rep(i, n - 1) end *= x + 1;
  rep(q, end) {
    int mask = q;
    vi v(n);
    rep(i, n) {
      v[i] = mask % (x + 1);
      mask /= (x + 1);
    }
    vi sum(n);
    sum[0] = v[0];
    loop(i, 1, n) sum[i] = sum[i - 1] + v[i];

    bool ok = true;
    rep(i, m) {
      int a = sum[r[i]];
      if (l[i] != 0)
        a -= sum[l[i] - 1];
      if (a == s[i])
        continue;
      ok = false;
      break;
    }
    if (ok && sum.back() > ans_sum) {
      ans_sum = sum.back();
      swap(ans, v);
    }
  }
  return make_pair(ans, ans_sum);
}

int main() {
  int n, x, m;
  while (cin >> n >> x >> m) {
    vi l(m), r(m), s(m);
    rep(i, m) {
      cin >> l[i] >> r[i] >> s[i];
      l[i]--;
      r[i]--;
    }
    auto t = solve(n, x, m, l, r, s);
    if (t.second != -1) {
      rep(i, t.first.size() - 1) cout << t.first[i] << " ";
      cout << t.first.back() << endl;
    } else {
      cout << -1 << endl;
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define iter(it, c) for (auto it = c.begin(); it != c.end(); it++)

typedef vector<int> vi;

pair<vi, int> solve(int n, int x, int m, vi const &l, vi const &r,
                    vi const &s) {
  vi ans;
  int ans_sum = -1;
  int end = x + 1;
  rep(i, n - 1) end *= x + 1;
  rep(q, end) {
    int mask = q;
    vi v(n);
    rep(i, n) {
      v[i] = mask % (x + 1);
      mask /= (x + 1);
    }
    vi sum(n);
    sum[0] = v[0];
    loop(i, 1, n) sum[i] = sum[i - 1] + v[i];

    bool ok = true;
    rep(i, m) {
      int a = sum[r[i]];
      if (l[i] != 0)
        a -= sum[l[i] - 1];
      if (a == s[i])
        continue;
      ok = false;
      break;
    }
    if (ok && sum.back() > ans_sum) {
      ans_sum = sum.back();
      swap(ans, v);
    }
  }
  return make_pair(ans, ans_sum);
}

int main() {
  int n, x, m;
  while (cin >> n >> x >> m) {
    vi l(n), r(n), s(n);
    rep(i, m) {
      cin >> l[i] >> r[i] >> s[i];
      l[i]--;
      r[i]--;
    }
    auto t = solve(n, x, m, l, r, s);
    if (t.second != -1) {
      rep(i, t.first.size() - 1) cout << t.first[i] << " ";
      cout << t.first.back() << endl;
    } else {
      cout << -1 << endl;
    }
  }
}
Problem p01658 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem2546 : Chocolate **/
int main() {
  int M, N;

  cin >> M >> N;
  vector<vector<int>> C(M);
  rep(i, M) {
    C[i].resize(N);
    rep(j, N) { cin >> C[i][j]; }
  }

  int ans = 0;
  rep(i, M) {
    int l = 0, r = N - 1;

    while (l != r) {
      if (C[i][l]) {
        ans++;
        C[i][l + 1] = 1 - C[i][l + 1];
        l++;
      } else if (C[i][r]) {
        ans++;
        C[i][r - 1] = 1 - C[i][r - 1];
        r--;
      } else {
        C[i][l + 1] = 1 - C[i][l + 1];
        l++;
      }
    }
    ans += C[i][l];

    if (i < M - 1) {
      rep(j, N) { C[i + 1][j] = 1 - C[i + 1][j]; }
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16>>(x);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
#define if_range(x, y, w, h)                                                   \\
  if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) &&                 \\
      (int)(y) < (int)(h))

const int INF = 1000000000;
const double EPS = 1e-8;
const double PI = 3.14159;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
using namespace std;
typedef pair<int, int> P;

/** Problem2546 : Chocolate **/
int M, N;

int solve(int l, int r, vector<int> C) {
  if (l == r)
    return C[l];

  int res = 0;
  // eat l
  C[l + 1] = 1 - C[l + 1];
  res = max(res, C[l] + solve(l + 1, r, C));

  // eat r
  C[l + 1] = 1 - C[l + 1];
  C[r - 1] = 1 - C[r - 1];

  res = max(res, C[r] + solve(l, r - 1, C));

  return res;
}

int main() {
  cin >> M >> N;
  vector<vector<int>> C(M);
  rep(i, M) {
    C[i].resize(N);
    rep(j, N) { cin >> C[i][j]; }
  }

  int ans = 0;
  rep(i, M) {
    ans += solve(0, N - 1, C[i]);

    if (i < M - 1) {
      rep(j, N) { C[i + 1][j] = 1 - C[i + 1][j]; }
    }
  }

  cout << ans << endl;
}
Problem p01658 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;

int solve(vi v, int k) {
  int ans = 0;
  for (int i = 0; i < k; i++) {
    ans += v[i];
    v[i + 1] ^= 1;
  }
  for (int i = v.size() - 1; i > k; i--) {
    ans += v[i];
    v[i - 1] ^= 1;
  }
  return ans + v[k];
}

int main() {
  // vi v(100,0);
  // rep(i,100)rep(j,100)dp[i][j]=-1;
  // cout << solve(v,0,v.size()-1) << endl;
  // return 0;

  int h, w;
  while (cin >> h >> w) {
    vvi c(h, vi(w));
    rep(i, h) rep(j, w) {
      cin >> c[i][j];
      if (i)
        c[i][j] ^= 1;
    }

    int ans = 0;
    rep(i, h) {
      int t = 0;
      rep(j, w) t = max(t, solve(c[i], j));
      ans += t;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

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

typedef vector<int> vi;
typedef vector<vi> vvi;

int solve(vi &v, int l, int r) {
  if (l == r)
    return v[l];
  int res = -1;

  v[l + 1] ^= 1;
  res = max(res, solve(v, l + 1, r) + v[l]);
  v[l + 1] ^= 1;
  v[r - 1] ^= 1;
  res = max(res, solve(v, l, r - 1) + v[r]);
  v[r - 1] ^= 1;

  return res;
}

int main() {
  // vi v{0,0,0,0,0};
  // cout << solve(v,0,4) << endl;

  int h, w;
  while (cin >> h >> w) {
    vvi c(h, vi(w));
    rep(i, h) rep(j, w) cin >> c[i][j];

    int ans = 0;
    rep(i, h) {
      if (i & 1)
        rep(j, w) c[i][j] ^= 1;
      ans += solve(c[i], 0, w - 1);
    }
    cout << ans << endl;
  }
}