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

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int a, b, c, n;
  while (cin >> a >> b >> c >> n, a + b + c + n) {
    vector<int> in(a + b + c + 1, 2);
    vector<vector<int>> jud(n, vector<int>(4, 0));
    for (int l = 0; l < n; l++) {
      int i, j, k, r;
      cin >> i >> j >> k >> r;
      if (r == 1) {
        in[i] = in[j] = in[k] = 1;
      }
      jud[l][0] = i;
      jud[l][1] = j;
      jud[l][2] = k;
      jud[l][3] = r;
    }

    for (int i = 0; i < jud.size(); i++) {
      if (jud[i][3] == 0) {
        if (in[jud[i][0]] == 1 && in[jud[i][1]] == 1)
          in[jud[i][2]] = 0;
        if (in[jud[i][2]] == 1 && in[jud[i][0]] == 1)
          in[jud[i][1]] = 0;
        if (in[jud[i][2]] == 1 && in[jud[i][1]] == 1)
          in[jud[i][0]] = 0;
      }
    }

    for (int i = 1; i < in.size(); i++)
      cout << in[i] << endl;
  }
  return 0;
}

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

using namespace std;

int main(void) {
  int a, b, c, n;
  while (cin >> a >> b >> c, a + b + c) {
    cin >> n;
    vector<int> in(a + b + c + 1, 2);
    vector<vector<int>> jud(n, vector<int>(4, 0));
    for (int l = 0; l < n; l++) {
      int i, j, k, r;
      cin >> i >> j >> k >> r;
      if (r == 1) {
        in[i] = in[j] = in[k] = 1;
      }
      jud[l][0] = i;
      jud[l][1] = j;
      jud[l][2] = k;
      jud[l][3] = r;
    }

    for (int i = 0; i < jud.size(); i++) {
      if (jud[i][3] == 0) {
        if (in[jud[i][0]] == 1 && in[jud[i][1]] == 1)
          in[jud[i][2]] = 0;
        if (in[jud[i][2]] == 1 && in[jud[i][0]] == 1)
          in[jud[i][1]] = 0;
        if (in[jud[i][2]] == 1 && in[jud[i][1]] == 1)
          in[jud[i][0]] = 0;
      }
    }

    for (int i = 1; i < in.size(); i++)
      cout << in[i] << endl;
  }
  return 0;
}
Problem p00437 (C++)

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

#define loop(i, n) for (int i = 1; i <= n; i++)
struct test {
  int i, j, k;
};

vector<test> t;
int p[302];

int main() {
  int a, b, c, n, i, j, k, r;
  while (cin >> a >> b >> c) {
    cin >> n;
    t.clear();
    loop(i, a + b + c) p[i] = 2;
    loop(h, n) {
      cin >> i >> j >> k >> r;
      if (r == 1) {
        p[h] = p[h] = p[h] = 1;
      } else {
        t.push_back({i, j, k});
      }
    }
    for (int h = 0; h < t.size(); h++) {
      i = t[h].i;
      j = t[h].j;
      k = t[h].k;
      if (p[i] == 1 && p[j] == 1) {
        p[k] = 0;
      } else if (p[j] == 1 && p[k] == 1) {
        p[i] = 0;
      } else if (p[k] == 1 && p[i] == 1) {
        p[j] = 0;
      }
    }
    loop(i, a + b + c) cout << p[i] << endl;
  }
  return 0;
}

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

#define loop(i, n) for (int i = 1; i <= n; i++)
struct test {
  int i, j, k;
};

vector<test> t;
int p[302];

int main() {
  int a, b, c, n, i, j, k, r;
  while (cin >> a >> b >> c) {
    cin >> n;
    t.clear();
    loop(i, a + b + c) p[i] = 2;
    loop(h, n) {
      cin >> i >> j >> k >> r;
      if (r == 1) {
        p[i] = p[j] = p[k] = 1;
      } else {
        t.push_back({i, j, k});
      }
    }
    for (int h = 0; h < t.size(); h++) {
      i = t[h].i;
      j = t[h].j;
      k = t[h].k;
      if (p[i] == 1 && p[j] == 1) {
        p[k] = 0;
      } else if (p[j] == 1 && p[k] == 1) {
        p[i] = 0;
      } else if (p[k] == 1 && p[i] == 1) {
        p[j] = 0;
      }
    }
    loop(i, a + b + c) cout << p[i] << endl;
  }
  return 0;
}
Problem p00437 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a < c; a++)
using namespace std;
int con[1001 * 2];
int an[1001];
int ans[1001];
int main() {
  while (1) {
    rep(i, 0, 1001) ans[i] = 2;
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0 && b == 0 && c == 0)
      break;
    int n;
    cin >> n;
    rep(i, 0, n) {
      // int x,y,z,an;
      cin >> con[i] >> con[n + i] >> con[n * 2 + i] >> an[i];
      // x--;y--;z--;
      // cout << i << a+i << a+b+i << endl;
      if (an[i] == 1) {
        ans[con[i]] = 1;
        ans[con[n + i]] = 1;
        ans[con[n * 2 + i]] = 1;
      }
    }
    rep(i, 0, n) {
      // cout << con[i] << con[i+n] << con[i+n+2] << endl;
      if (an[i] == 0)
        if (ans[con[i]] == 1 && ans[con[n + i]] == 1 &&
            ans[con[n * 2 + i]] == 2) {
          ans[con[i + n * 2]] = 0;
        }
      if (an[i] == 0)
        if (ans[con[i]] == 1 && ans[con[n + i]] == 2 &&
            ans[con[n * 2 + i]] == 1) {
          ans[con[i + n]] = 0;
        }
      if (an[i] == 0)
        if (ans[con[i]] == 2 && ans[con[n + i]] == 1 &&
            ans[con[n * 2 + i]] == 1) {
          ans[con[i]] = 0;
        }
    }
    rep(i, 1, a + b + c + 1) { cout << ans[i] << endl; }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a < c; a++)
using namespace std;
int con[3003];
int an[1001];
int ans[1001];
int main() {
  while (1) {
    rep(i, 0, 1001) ans[i] = 2;
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0 && b == 0 && c == 0)
      break;
    int n;
    cin >> n;
    rep(i, 0, n) {
      // int x,y,z,an;
      cin >> con[i] >> con[n + i] >> con[n * 2 + i] >> an[i];
      // x--;y--;z--;
      // cout << i << a+i << a+b+i << endl;
      if (an[i] == 1) {
        ans[con[i]] = 1;
        ans[con[n + i]] = 1;
        ans[con[n * 2 + i]] = 1;
      }
    }
    rep(i, 0, n) {
      // cout << con[i] << con[i+n] << con[i+n+2] << endl;
      if (an[i] == 0)
        if (ans[con[i]] == 1 && ans[con[n + i]] == 1 &&
            ans[con[n * 2 + i]] == 2) {
          ans[con[i + n * 2]] = 0;
        }
      if (an[i] == 0)
        if (ans[con[i]] == 1 && ans[con[n + i]] == 2 &&
            ans[con[n * 2 + i]] == 1) {
          ans[con[i + n]] = 0;
        }
      if (an[i] == 0)
        if (ans[con[i]] == 2 && ans[con[n + i]] == 1 &&
            ans[con[n * 2 + i]] == 1) {
          ans[con[i]] = 0;
        }
    }
    rep(i, 1, a + b + c + 1) { cout << ans[i] << endl; }
  }
  return 0;
}
Problem p00437 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int ans[500];
int a, b, c, N;
int r[1000], x[1000], y[1000], z[1000];
void init() {
  for (int i = 0; i < 2000; i++)
    ans[i] = 2;
}
int main() {
  while (cin >> a >> b >> c && a && b && c) {
    init();
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i] >> z[i] >> r[i];
      if (r[i] == 1)
        ans[x[i]] = 1, ans[y[i]] = 1, ans[z[i]] = 1;
    }
    for (int i = 0; i < N; i++) {
      if (r[i] == 1)
        continue;
      if (ans[x[i]] == 1 && ans[y[i]] == 1)
        ans[z[i]] = 0;
      if (ans[y[i]] == 1 && ans[z[i]] == 1)
        ans[x[i]] = 0;
      if (ans[z[i]] == 1 && ans[x[i]] == 1)
        ans[y[i]] = 0;
    }
    for (int i = 1; i <= a + b + c; i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int ans[500];
int a, b, c, N;
int r[1000], x[1000], y[1000], z[1000];
void init() {
  for (int i = 0; i < 500; i++)
    ans[i] = 2;
}
int main() {
  while (cin >> a >> b >> c && a && b && c) {
    init();
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> x[i] >> y[i] >> z[i] >> r[i];
      if (r[i] == 1)
        ans[x[i]] = 1, ans[y[i]] = 1, ans[z[i]] = 1;
    }
    for (int i = 0; i < N; i++) {
      if (r[i] == 1)
        continue;
      if (ans[x[i]] == 1 && ans[y[i]] == 1)
        ans[z[i]] = 0;
      if (ans[y[i]] == 1 && ans[z[i]] == 1)
        ans[x[i]] = 0;
      if (ans[z[i]] == 1 && ans[x[i]] == 1)
        ans[y[i]] = 0;
    }
    for (int i = 1; i <= a + b + c; i++) {
      cout << ans[i] << endl;
    }
  }
  return 0;
}
Problem p00437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  while (true) {
    int parts[301], d[301][3], g[1000];
    int a, b, c, N;
    cin >> a >> b >> c;
    if (a == 0 && b == 0 && c == 0)
      break;
    cin >> N;
    for (int i = 1; i <= a + b + c; i++) {
      parts[i] = 2;
    }
    for (int i = 0; i < N; i++) {
      cin >> d[i][0] >> d[i][1] >> d[i][2] >> g[i];
      if (g[i] == 1) {
        for (int j = 0; j < 3; j++)
          parts[d[i][j]] = 1;
      }
    }
    for (int i = 0; i < N; i++) {
      if (g[i] == 0) {
        for (int j = 0; j < 3; j++) {
          if (parts[d[i][(j + 1) % 3]] == 1 && parts[d[i][(j + 2) % 3]] == 1)
            parts[d[i][j]] = 0;
        }
      }
    }
    for (int i = 1; i <= a + b + c; i++) {
      cout << parts[i] << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  while (true) {
    int parts[301], d[1000][3], g[1000];
    int a, b, c, N;
    cin >> a >> b >> c;
    if (a == 0 && b == 0 && c == 0)
      break;
    cin >> N;
    for (int i = 1; i <= a + b + c; i++) {
      parts[i] = 2;
    }
    for (int i = 0; i < N; i++) {
      cin >> d[i][0] >> d[i][1] >> d[i][2] >> g[i];
      if (g[i] == 1) {
        for (int j = 0; j < 3; j++)
          parts[d[i][j]] = 1;
      }
    }
    for (int i = 0; i < N; i++) {
      if (g[i] == 0) {
        for (int j = 0; j < 3; j++) {
          if (parts[d[i][(j + 1) % 3]] == 1 && parts[d[i][(j + 2) % 3]] == 1)
            parts[d[i][j]] = 0;
        }
      }
    }
    for (int i = 1; i <= a + b + c; i++) {
      cout << parts[i] << endl;
    }
  }
  return 0;
}
Problem p00437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

vector<int> ans;

bool check(vector<int> v) {
  int count = 0, no;

  for (int i = 0; i < 3; ++i)
    if (ans[v[i]] != 1) {
      no = v[i];
      ++count;
    }

  if (count > 1)
    return false;
  else if (count == 0) {
    cout << "ERROR" << endl;
    return false;
  } else
    ans[no] = 0;

  return true;
}

vector<int> make_result(int a, int b, int c, int r) {
  vector<int> ret;

  ret.push_back(a);
  ret.push_back(b);
  ret.push_back(c);
  ret.push_back(r);

  return ret;
}

int main() {
  int a, b, c;
  while (cin >> a >> b >> c, a || b || c) {
    int n = a + b + c;
    int N;
    cin >> N;
    ans.erase(ans.begin(), ans.end());

    for (int i = 0; i < n; ++i)
      ans.push_back(2);

    vector<vector<int>> results;
    for (int i = 0; i < N; ++i) {
      int r;
      cin >> a >> b >> c >> r;
      --a, --b, --c;

      if (r == 1)
        ans[a] = ans[b] = ans[c] = 1;

      else
        results.push_back(make_result(a, b, c, r));
    }

    while (1) {
      bool update = false;
      for (int i = 0; i < results.size(); ++i)
        if (update = check(results[i]))
          results.erase(results.begin() + i, results.begin() + i);

      if (!update)
        break;
    }

    for (int i = 0; i < n; ++i)
      cout << ans[i] << endl;
  }
}

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

using namespace std;

vector<int> ans;

bool check(vector<int> v) {
  int count = 0, no;

  for (int i = 0; i < 3; ++i)
    if (ans[v[i]] != 1) {
      no = v[i];
      ++count;
    }

  if (count > 1)
    return false;
  else if (count == 0) {
    cout << "ERROR" << endl;
    return false;
  } else
    ans[no] = 0;

  return true;
}

vector<int> make_result(int a, int b, int c, int r) {
  vector<int> ret;

  ret.push_back(a);
  ret.push_back(b);
  ret.push_back(c);
  ret.push_back(r);

  return ret;
}

int main() {
  int a, b, c;
  while (cin >> a >> b >> c, a || b || c) {
    int n = a + b + c;
    int N;
    cin >> N;
    ans.erase(ans.begin(), ans.end());

    for (int i = 0; i < n; ++i)
      ans.push_back(2);

    vector<vector<int>> results;
    for (int i = 0; i < N; ++i) {
      int r;
      cin >> a >> b >> c >> r;
      --a, --b, --c;

      if (r == 1)
        ans[a] = ans[b] = ans[c] = 1;

      else
        results.push_back(make_result(a, b, c, r));
    }

    bool update = false;
    for (int i = 0; i < results.size(); ++i)
      if (update = check(results[i]))
        results.erase(results.begin() + i, results.begin() + i);

    for (int i = 0; i < n; ++i)
      cout << ans[i] << endl;
  }
}
Problem p00437 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[1000][4];
int b[302];
int main() {
  int x, y, z, n, t;
  while (1) {
    scanf("%d%d%d", &x, &y, &z);
    scanf("%d", &n);
    fill(b, b + 302, 2);
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
      if (a[i][3])
        b[a[i][0]] = b[a[i][1]] = b[a[i][2]] = 1;
    }
    for (int i = 0; i < n; i++) {
      t = 0;
      if (a[i][3] == 0) {
        rep(j, 3) if (b[a[i][j]] == 1) t++;
        if (t == 2)
          rep(j, 3) if (b[a[i][j]] == 2) b[a[i][j]] = 0;
      }
    }
    rrep(i, x + y + z) printf("%d\n", b[i]);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100001
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[1000][4];
int b[302];
int main() {
  int x, y, z, n, t;
  while (1) {
    scanf("%d%d%d", &x, &y, &z);
    if (!x && !y && !z)
      break;
    scanf("%d", &n);
    fill(b, b + 302, 2);
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
      if (a[i][3])
        b[a[i][0]] = b[a[i][1]] = b[a[i][2]] = 1;
    }
    for (int i = 0; i < n; i++) {
      t = 0;
      if (a[i][3] == 0) {
        rep(j, 3) if (b[a[i][j]] == 1) t++;
        if (t == 2)
          rep(j, 3) if (b[a[i][j]] == 2) b[a[i][j]] = 0;
      }
    }
    rrep(i, x + y + z) printf("%d\n", b[i]);
  }
  return 0;
}
Problem p00437 (C++)

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

int main() {
  int a, b, c;
  while (cin >> a >> b >> c, a, b, c) {

    int q[350];
    for (int i = 0; i < a + b + c; i++)
      q[i] = 2;
    int n;
    cin >> n;
    int z = 0;
    int ls[350][3];

    for (int i = 0; i < n; i++) {
      int d, e, f, g;
      cin >> d >> e >> f >> g;
      if (g == 0) {
        ls[z][0] = d;
        ls[z][1] = e;
        ls[z][2] = f;
        z++;
      }

      else {
        q[d - 1] = 1;
        q[e - 1] = 1;
        q[f - 1] = 1;
      }
    }

    for (int i = 0; i < z; i++) {
      int d = 0, e = 0, f = 0;
      if (q[ls[i][0] - 1] == 1)
        d = 1;
      if (q[ls[i][1] - 1] == 1)
        e = 1;
      if (q[ls[i][2] - 1] == 1)
        f = 1;

      if (d == 1 && e == 1) {
        q[ls[i][2] - 1] = 0;
      }
      if (d == 1 && f == 1) {
        q[ls[i][1] - 1] = 0;
      }
      if (e == 1 && f == 1) {
        q[ls[i][0] - 1] = 0;
      }
    }

    for (int i = 0; i < a + b + c; i++) {
      cout << q[i] << endl;
    }
  }
  return 0;
}

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

int main() {
  int a, b, c;
  while (cin >> a >> b >> c, a, b, c) {

    int q[350];
    for (int i = 0; i < a + b + c; i++)
      q[i] = 2;
    int n;
    cin >> n;
    int z = 0;
    int ls[1200][5];

    for (int i = 0; i < n; i++) {
      int d, e, f, g;
      cin >> d >> e >> f >> g;
      if (g == 0) {
        ls[z][0] = d;
        ls[z][1] = e;
        ls[z][2] = f;
        z++;
      }

      else {
        q[d - 1] = 1;
        q[e - 1] = 1;
        q[f - 1] = 1;
      }
    }

    for (int i = 0; i < z; i++) {
      int d = 0, e = 0, f = 0;
      if (q[ls[i][0] - 1] == 1)
        d = 1;
      if (q[ls[i][1] - 1] == 1)
        e = 1;
      if (q[ls[i][2] - 1] == 1)
        f = 1;

      if (d == 1 && e == 1) {
        q[ls[i][2] - 1] = 0;
      }
      if (d == 1 && f == 1) {
        q[ls[i][1] - 1] = 0;
      }
      if (e == 1 && f == 1) {
        q[ls[i][0] - 1] = 0;
      }
    }

    for (int i = 0; i < a + b + c; i++) {
      cout << q[i] << endl;
    }
  }
  return 0;
}
Problem p00437 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
constexpr int MOD = 1000000007;
constexpr int INF = 2000000000;
using namespace std;

int main() {
  int a, b, c;
  int A[101], B[101], C[101], D[101];
  int ans[301];
  int n;
  while (cin >> a >> b >> c && a != 0 && b != 0 && c != 0) {
    cin >> n;
    for (int i = 1; i <= a + b + c; i++) {
      ans[i] = 2;
    }
    for (int i = 0; i < n; i++) {
      cin >> A[i] >> B[i] >> C[i] >> D[i];
      if (D[i] == 1) {
        ans[A[i]] = ans[B[i]] = ans[C[i]] = 1;
      }
    }
    for (int i = 0; i < n; i++) {
      if (D[i] == 0) {
        if (ans[A[i]] == 2 && ans[B[i]] == 1 && ans[C[i]] == 1)
          ans[A[i]] = 0;
        if (ans[A[i]] == 1 && ans[B[i]] == 2 && ans[C[i]] == 1)
          ans[B[i]] = 0;
        if (ans[A[i]] == 1 && ans[B[i]] == 1 && ans[C[i]] == 2)
          ans[C[i]] = 0;
      }
    }
    for (int i = 1; i <= a + b + c; i++)
      cout << ans[i] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
constexpr int MOD = 1000000007;
constexpr int INF = 2000000000;
using namespace std;

int main() {
  int a, b, c;
  int A[1001], B[1001], C[1001], D[1001];
  int ans[10001];
  int n;
  while (cin >> a >> b >> c && a != 0 && b != 0 && c != 0) {
    cin >> n;
    for (int i = 1; i <= a + b + c; i++) {
      ans[i] = 2;
    }
    for (int i = 0; i < n; i++) {
      cin >> A[i] >> B[i] >> C[i] >> D[i];
      if (D[i] == 1) {
        ans[A[i]] = ans[B[i]] = ans[C[i]] = 1;
      }
    }
    for (int i = 0; i < n; i++) {
      if (D[i] == 0) {
        if (ans[A[i]] == 2 && ans[B[i]] == 1 && ans[C[i]] == 1)
          ans[A[i]] = 0;
        if (ans[A[i]] == 1 && ans[B[i]] == 2 && ans[C[i]] == 1)
          ans[B[i]] = 0;
        if (ans[A[i]] == 1 && ans[B[i]] == 1 && ans[C[i]] == 2)
          ans[C[i]] = 0;
      }
    }
    for (int i = 1; i <= a + b + c; i++)
      cout << ans[i] << endl;
  }
}
Problem p00438 (C++)

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

int a, b, n;
struct cross {
  bool isbuilding;
  int access;
};
cross map[16][16];

void read() {
  cin >> n;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      map[i][j].isbuilding = false;
      map[i][j].access = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    map[x][y].isbuilding = true;
  }
  map[0][0].access = 1;
  return;
}

void dp_a(int crossa, int crossb) {
  if (crossa + 1 == a) {
    return;
  }
  if (map[crossa + 1][crossb].isbuilding) {
    return;
  }
  map[crossa + 1][crossb].access += map[crossa][crossb].access;
  return;
}

void dp_b(int crossa, int crossb) {
  if (crossb + 1 == b) {
    return;
  }
  if (map[crossa][crossb + 1].isbuilding) {
    return;
  }
  map[crossa][crossb + 1].access += map[crossa][crossb].access;
  return;
}

void solve() {
  for (int i = 0; i < a + b - 2; i++) {
    int times;
    if (i < (a < b) ? a : b) {
      times = i + 1;
    } else if (i <= (a > b) ? a : b) {
      times = (a < b) ? a : b;
    } else {
      times = a + b - i - 1;
    }

    for (int j = 0; j < times; j++) {
      dp_a(j, i - j);
      dp_b(j, i - j);
    }
  }
  return;
}

int main() {
  while (1) {
    cin >> a >> b;
    if (!a && !b) {
      break;
    }
    read();
    solve();
    cout << map[a - 1][b - 1].access << endl;
  }
  return 0;
}

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

int a, b, n;
struct cross {
  bool isbuilding;
  int access;
};
cross map[100][100];

void read() {
  cin >> n;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      map[i][j].isbuilding = false;
      map[i][j].access = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    map[x][y].isbuilding = true;
  }
  map[0][0].access = 1;
  return;
}

void dp_a(int crossa, int crossb) {
  if (crossa + 1 == a) {
    return;
  }
  if (map[crossa + 1][crossb].isbuilding) {
    return;
  }
  map[crossa + 1][crossb].access += map[crossa][crossb].access;
  return;
}

void dp_b(int crossa, int crossb) {
  if (crossb + 1 == b) {
    return;
  }
  if (map[crossa][crossb + 1].isbuilding) {
    return;
  }
  map[crossa][crossb + 1].access += map[crossa][crossb].access;
  return;
}

void solve() {
  for (int i = 0; i < a + b - 2; i++) {
    int times;
    if (i < (a < b) ? a : b) {
      times = i + 1;
    } else if (i <= (a > b) ? a : b) {
      times = (a < b) ? a : b;
    } else {
      times = a + b - i - 1;
    }

    for (int j = 0; j < times; j++) {
      dp_a(j, i - j);
      dp_b(j, i - j);
    }
  }
  return;
}

int main() {
  while (1) {
    cin >> a >> b;
    if (!a && !b) {
      break;
    }
    read();
    solve();
    cout << map[a - 1][b - 1].access << endl;
  }
  return 0;
}
Problem p00438 (C++)

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

int main() {
  int a, b, x, y;

  while (std::cin >> a >> b) {

    if (a == 0 && b == 0) {
      break;
    }

    std::vector<std::vector<int>> road(a, std::vector<int>(b, 1));
    int num = 0;

    std::cin >> num;

    for (int i = 0; i < num; ++i) {
      std::cin >> x >> y;
      road.at(x).at(y) = 0;
    }

    for (int i = 0; i < a; ++i) {
      for (int j = 0; j < b; ++j) {

        // start point & point under construction
        if (road.at(i).at(j) != 0 && (i + j) != 0) {
          if (i == 0) {
            if (road.at(i).at(j - 1) != 0) {
              road.at(i).at(j) = road.at(i).at(j - 1);
            } else {
              road.at(i).at(j) = 0;
            }
          } else if (j == 0) {
            if (road.at(i - 1).at(j) != 0) {
              road.at(i).at(j) = road.at(i - 1).at(j);
            } else {
              road.at(i).at(j) = 0;
            }
          } else {
            road.at(i).at(j) = road.at(i).at(j - 1) + road.at(i - 1).at(j);
          }
        }
      }
    }

    std::cout << road.at(a - 1).at(b - 1) << std::endl;
  }
}

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

int main() {
  int a, b, x, y;

  while (std::cin >> a >> b) {

    if (a == 0 && b == 0) {
      break;
    }

    std::vector<std::vector<int>> road(a, std::vector<int>(b, 1));
    int num = 0;

    std::cin >> num;

    for (int i = 0; i < num; ++i) {
      std::cin >> x >> y;
      road.at(x - 1).at(y - 1) = 0;
    }

    for (int i = 0; i < a; ++i) {
      for (int j = 0; j < b; ++j) {

        // start point & point under construction
        if (road.at(i).at(j) != 0 && (i + j) != 0) {
          if (i == 0) {
            if (road.at(i).at(j - 1) != 0) {
              road.at(i).at(j) = road.at(i).at(j - 1);
            } else {
              road.at(i).at(j) = 0;
            }
          } else if (j == 0) {
            if (road.at(i - 1).at(j) != 0) {
              road.at(i).at(j) = road.at(i - 1).at(j);
            } else {
              road.at(i).at(j) = 0;
            }
          } else {
            road.at(i).at(j) = road.at(i).at(j - 1) + road.at(i - 1).at(j);
          }
        }
      }
    }

    std::cout << road.at(a - 1).at(b - 1) << std::endl;
  }
}
Problem p00438 (C++)

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

int main() {
  while (1) {
    int a, b;
    std::cin >> a >> b;
    if (a == 0 && b == 0)
      break;

    int n;
    std::cin >> n;

    std::vector<std::vector<int>> dp(a, std::vector<int>(b, 0));
    for (int i = 0; i != n; ++i) {
      int c, d;
      std::cin >> c >> d;
      dp[c - 1][d - 1] = -1;
    }

    for (int i = 0; i != a; ++i) {
      for (int j = 0; j != b; ++j) {
        if (i == 0 && j == 0)
          dp[i][j] = 1;
        else if (dp[i][j] != -1) {
          if (i == 0 && dp[i][j - 1] != -1)
            dp[i][j] = dp[i][j - 1];
          else if (j == 0 && dp[i - 1][j] != -1)
            dp[i][j] = dp[i - 1][j];
          else if (dp[i - 1][j] != -1 && dp[i][j - 1] != -1)
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
          else if (dp[i - 1][j] != -1)
            dp[i][j] = dp[i - 1][j];
          else if (dp[i][j - 1] != -1)
            dp[i][j] = dp[i][j - 1];
        }
      }
    }

    std::cout << dp[a - 1][b - 1] << std::endl;
  }
  return 0;
}

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

int main() {
  while (1) {
    int a, b;
    std::cin >> a >> b;
    if (a == 0 && b == 0)
      break;

    int n;
    std::cin >> n;

    std::vector<std::vector<int>> dp(a, std::vector<int>(b, 0));
    for (int i = 0; i != n; ++i) {
      int c, d;
      std::cin >> c >> d;
      dp[c - 1][d - 1] = -1;
    }

    for (int i = 0; i != a; ++i) {
      for (int j = 0; j != b; ++j) {
        if (i == 0 && j == 0)
          dp[i][j] = 1;
        else if (dp[i][j] != -1) {
          if (i != 0 && dp[i - 1][j] != -1) {
            dp[i][j] += dp[i - 1][j];
          }
          if (j != 0 && dp[i][j - 1] != -1) {
            dp[i][j] += dp[i][j - 1];
          }
        }
      }
    }

    std::cout << dp[a - 1][b - 1] << std::endl;
  }
  return 0;
}
Problem p00438 (C++)

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

using namespace std;

const double PI = 3.14159265358979323846;

int map[1001][1001];
int w, h;

int f(int x, int y) {
  if ((map[x][y] == -1) || (x < 0) || (y < 0) || (w <= x) || (h <= y)) {
    return 0;
  }
  if ((x == 0) && (y == 0)) {
    return 1;
  }

  if (map[x][y] != 0) {
    return map[x][y];
  }
  return f(x - 1, y) + f(x, y - 1);
}

int main() {
  while (true) {
    int n;
    cin >> w >> h >> n;
    if ((w == 0) && (h == 0))
      return 0;
    for (int i = 0; i < w; ++i) {
      for (int j = 0; j < h; ++j) {
        map[i][j] = 0;
      }
    }
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      map[x - 1][y - 1] = -1;
    }

    cout << f(w - 1, h - 1) << endl;
  }
}

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

using namespace std;

const double PI = 3.14159265358979323846;

int map[16][16];
int w, h;

int f(int x, int y) {
  if ((map[x][y] == -1) || (x < 0) || (y < 0) || (w <= x) || (h <= y)) {
    return 0;
  }
  if ((x == 0) && (y == 0)) {
    return 1;
  }

  if (map[x][y] != 0) {
    return map[x][y];
  }
  return f(x - 1, y) + f(x, y - 1);
}

int main() {
  while (true) {
    int n;
    cin >> w >> h >> n;
    if ((w == 0) && (h == 0))
      return 0;
    for (int i = 0; i < w; ++i) {
      for (int j = 0; j < h; ++j) {
        map[i][j] = 0;
      }
    }
    for (int i = 0; i < n; ++i) {
      int x, y;
      cin >> x >> y;
      map[x - 1][y - 1] = -1;
    }

    cout << f(w - 1, h - 1) << endl;
  }
}
Problem p00438 (C++)

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

int main() {
  int a, b;
  while (std::cin >> a >> b) {
    if (a == 0 && b == 0)
      break;

    int n;
    std::cin >> n;
    std::vector<std::vector<int>> map(a, std::vector<int>(b, 1)); // 地図

    for (int i = 0; i < n; ++i) {
      int x, y;
      std::cin >> x >> y;
      if (x == 0 && y == 0)
        break;
      map[x - 1][y - 1] = 0;
      if (x == 1) {
        for (int y1 = y; y1 < b; y1++) {
          map[x - 1][y1] = 0;
        }
      }
      if (y == 1) {
        for (int x1 = x; x1 < a; x1++) {
          map[x - 1][x1] = 0;
        }
      }
    }

    for (int i = 1; i < a; ++i) {
      for (int j = 1; j < b; ++j) {
        if (map[i][j] > 0) {
          map[i][j] = map[i - 1][j] + map[i][j - 1];
        }
      }
    }
    std::cout << map[a - 1][b - 1] << std::endl;
  }
  return 0;
}


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

int main() {
  int a, b;
  while (std::cin >> a >> b) {
    if (a == 0 && b == 0)
      break;

    int n;
    std::cin >> n;
    std::vector<std::vector<int>> map(a, std::vector<int>(b, 1)); // 地図

    for (int i = 0; i < n; ++i) {
      int x, y;
      std::cin >> x >> y;
      if (x == 0 && y == 0)
        break;
      map[x - 1][y - 1] = 0;
      if (x == 1) {
        for (int y1 = y; y1 < b; y1++) {
          map[x - 1][y1] = 0;
        }
      }
      if (y == 1) {
        for (int x1 = x; x1 < a; x1++) {
          map[x1][y - 1] = 0;
        }
      }
    }

    for (int i = 1; i < a; ++i) {
      for (int j = 1; j < b; ++j) {
        if (map[i][j] > 0) {
          map[i][j] = map[i - 1][j] + map[i][j - 1];
        }
      }
    }
    std::cout << map[a - 1][b - 1] << std::endl;
  }
  return 0;
}
Problem p00439 (C++)

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

int main() {
  int n, k;
  int num[100005];
  while (cin >> n >> k && n && k) {
    for (int i = 0; i < n; i++)
      cin >> num[i];
    int ans = 0;
    for (int i = 0; i < n - k + 1; i++) {
      int sum = 0;
      for (int j = 0; j < k; j++) {
        sum += num[i + j];
      }
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }
}

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

int main() {
  int n, k;
  int num[100005];
  while (cin >> n >> k && n && k) {
    for (int i = 0; i < n; i++)
      cin >> num[i];
    int ans = 0;
    for (int i = 0; i < k; i++)
      ans += num[i];
    int sum = ans;
    for (int i = 1; i < n - k + 1; i++) {
      sum -= num[i - 1];
      sum += num[i + k - 1];
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }
}
Problem p00439 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {

  int n, k;
  int a[100000];

  while (cin >> n >> k) {
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int max = a[0];
    for (int i = 0; i <= n - k; i++) {
      int t = 0;
      int l = 0;
      for (int j = i; j < n; j++) {
        l++;
        t += a[j];
        if (t > max) {
          max = t;
        }
        if (l == k) {
          break;
        }
      }
    }
    cout << max << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {

  int n, k;
  int a[100000];

  while (cin >> n >> k) {
    if (n == 0 && k == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int max = a[0];
    int t = 0;
    for (int i = 0; i < k; i++) {
      t += a[i];
    }
    if (t > max) {
      max = t;
    }
    for (int i = k; i < n; i++) {
      t += a[i];
      t -= a[i - k];
      if (t > max) {
        max = t;
      }
    }
    cout << max << endl;
  }

  return 0;
}
Problem p00439 (C++)

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

using namespace std;

int main(void) {
  int n, k, input[5000];
  while (cin >> n >> k, n | k) {
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      cin >> input[i];
      if (i < k)
        sum += input[i];
    }
    int i, j, ans = sum;
    i = 0;
    j = k - 1;
    while (j < n - 1) {
      sum -= input[i++];
      sum += input[++j];
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int main(void) {
  int n, k, input[100000];
  while (cin >> n >> k, n | k) {
    int sum = 0;
    for (int i = 0; i < n; ++i) {
      cin >> input[i];
      if (i < k)
        sum += input[i];
    }
    int i, j, ans = sum;
    i = 0;
    j = k - 1;
    while (j < n - 1) {
      sum -= input[i++];
      sum += input[++j];
      ans = max(ans, sum);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {
  int a[100010], n, k;
  while (cin >> n >> k, n || k) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int mx = -1;
    for (int i = 0; i <= n - k; i++) {
      int sum = 0;
      for (int j = i; j < i + k; j++)
        sum += a[j];
      mx = max(mx, sum);
    }
    cout << mx << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int a[100010], n, k;
  while (cin >> n >> k, n || k) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int mx = -1;
    int sum = 0;
    for (int i = 0; i < k; i++)
      sum += a[i];
    mx = sum;
    for (int i = k; i < n; i++) {
      sum -= a[i - k];
      sum += a[i];
      mx = max(mx, sum);
    }
    cout << mx << endl;
  }
}
Problem p00439 (C++)

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

int N, K;
int a[10001];

void solve() {
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i + 1]);
    a[i + 1] += a[i];
  }
  int ma = 0;
  for (int i = K; i <= N; i++)
    ma = max(ma, a[i] - a[i - K]);
  printf("%d\n", ma);
}
int main() {
  while (scanf("%d%d", &N, &K), N || K)
    solve();
}

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

int N, K;
int a[100001];

void solve() {
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i + 1]);
    a[i + 1] += a[i];
  }
  int ma = 0;
  for (int i = K; i <= N; i++)
    ma = max(ma, a[i] - a[i - K]);
  printf("%d\n", ma);
}
int main() {
  while (scanf("%d%d", &N, &K), N || K)
    solve();
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int s = 0;
    int m = 0;

    while (s + 2 < n) {
      int cm = 0;
      for (int j = 0; j < k; j++) {
        cm += a[s + j];
      }
      if (cm > m) {
        m = cm;
      }
      s++;
    }
    cout << m << endl;
  }
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  while (1) {
    int n, k;
    cin >> n >> k;
    if (n == 0 && k == 0) {
      break;
    }
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int s = 0;
    int m = 0;

    while (s + k - 1 < n) {
      int cm = 0;
      for (int j = 0; j < k; j++) {
        cm += a[s + j];
      }
      if (cm > m) {
        m = cm;
      }
      s++;
    }
    cout << m << endl;
  }
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int main() {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (!n)
      return 0;
    int ret = -INF;
    int res = 0;
    queue<int> que;
    for (int i = 0; i < k; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      que.push(s);
    }
    ret = max(ret, res);
    for (int i = k; i <= n; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      res -= (que.front());
      ret = max(ret, res);
      que.pop();
      que.push(s);
    }
    printf("%d\n", ret);
  }
}

Fixed submission:
// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int main() {
  while (1) {
    int n, k;
    scanf("%d %d", &n, &k);
    if (!n)
      return 0;
    int ret = -INF;
    int res = 0;
    queue<int> que;
    for (int i = 0; i < k; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      que.push(s);
    }
    ret = max(ret, res);
    for (int i = k; i < n; i++) {
      int s;
      scanf("%d", &s);
      res += s;
      res -= (que.front());
      ret = max(ret, res);
      que.pop();
      que.push(s);
    }
    printf("%d\n", ret);
  }
}
Problem p00439 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {

  int in, k;

  while (cin >> in >> k) {
    if (in == 0 && k == 0)
      break;
    int data[in];
    int sum;

    for (int i = 0; i < in; i++) {
      cin >> data[i];
    }

    for (int i = 0; i <= in - k; i++) {
      int tmp = 0;
      for (int j = i; j < i + k; j++) {
        tmp += data[j];
      }
      if (i == 0 || sum < tmp)
        sum = tmp;
    }

    cout << sum << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {

  int in, k;

  while (cin >> in >> k) {
    if (in == 0 && k == 0)
      break;
    int data[in];
    int sum;

    for (int i = 0; i < in; i++) {
      cin >> data[i];
    }

    int tmp = 0;

    for (int i = 0; i < k; i++) {
      tmp += data[i];
    }
    sum = tmp;
    int num = 0;

    for (int i = k; i < in; i++) {
      tmp -= data[num];
      num++;
      tmp += data[i];
      if (i == 0 || sum < tmp)
        sum = tmp;
    }

    cout << sum << endl;
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> sum;
  int n, m, x, max;
  while (cin >> n >> m, n || m) {
    cin >> x;
    sum.push_back(0);
    sum.push_back(x);
    max = -1000000;
    for (int i = 2; i <= n; i++) {
      cin >> x;
      sum.push_back(sum[i - 1] + x);
    }
    for (int i = 0; i < n; i++) {
      if (sum[m + i] - sum[i] > max)
        max = sum[m + i] - sum[i];
    }
    cout << max << endl;
    sum.clear();
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> sum;
  int n, m, x, max;
  while (cin >> n >> m, n || m) {
    cin >> x;
    sum.push_back(0);
    sum.push_back(x);
    max = -1000000;
    for (int i = 2; i <= n; i++) {
      cin >> x;
      sum.push_back(sum[i - 1] + x);
    }
    for (int i = 0; i < n - m + 1; i++) {
      if (sum[m + i] - sum[i] > max)
        max = sum[m + i] - sum[i];
    }
    cout << max << endl;
    sum.clear();
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  while (1) {
    int n, k;
    int sum = 0, mx = 0;
    int ans[10000] = {0};
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> ans[i];
    for (int i = 0; i < k; i++) {
      sum += ans[i];
    }
    mx = sum;
    for (int i = k; i < n; i++) {
      sum += ans[i];
      sum -= ans[i - k];
      mx = max(mx, sum);
    }
    cout << mx << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
  while (1) {
    int n, k;
    int sum = 0, mx = 0;
    int ans[100000] = {0};
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> ans[i];
    for (int i = 0; i < k; i++) {
      sum += ans[i];
    }
    mx = sum;
    for (int i = k; i < n; i++) {
      sum += ans[i];
      sum -= ans[i - k];
      mx = max(mx, sum);
    }
    cout << mx << endl;
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int N, M;
    int lis[20000];
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> lis[i];
    int now = 0, ans = 0;
    for (int i = 0; i < M; i++)
      now += lis[i], ans += lis[i];
    for (int i = 1; i + M <= N; i++)
      now = now - lis[i - 1] + lis[i + M - 1], ans = max(now, ans);
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  while (1) {
    int N, M;
    int lis[100002];
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> lis[i];
    int now = 0, ans = 0;
    for (int i = 0; i < M; i++)
      now += lis[i], ans += lis[i];
    for (int i = 1; i + M <= N; i++)
      now = now - lis[i - 1] + lis[i + M - 1], ans = max(now, ans);
    cout << ans << endl;
  }
  return 0;
}
Problem p00439 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

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

int main(void) {
  int n, k;
  while (cin >> n >> k, n + k) {
    vector<int> a(n);

    rep(i, n) cin >> a[i];
    int ret = -100000;
    rep(i, n - k + 1) {
      int sum = 0;
      rep(j, k) { sum += a[i + j]; }
      ret = max(ret, sum);
    }
    cout << ret << endl;
  }
}

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

using namespace std;

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

int main(void) {
  int n, k;
  while (cin >> n >> k, n + k) {
    vector<int> a(n);

    rep(i, n) cin >> a[i];
    int ret = -100000;
    int sum = 0;
    rep(i, k) sum += a[i];
    for (int i = k; i < n; i++) {
      sum = sum + a[i] - a[i - k];
      ret = max(ret, sum);
    }
    cout << ret << endl;
  }
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int ans = 0, n, k, box[10000] = {0}, sum = 0;
    ;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      cin >> box[i];
      sum += box[i];
    }
    ans = sum;
    for (int i = k; i < n; i++) {
      cin >> box[i];
      sum += box[i] - box[i - k];
      if (sum > ans)
        ans = sum;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int ans = 0, n, k, box[100005] = {0}, sum = 0;
    ;
    cin >> n >> k;
    if (n == 0 && k == 0)
      break;
    for (int i = 0; i < k; i++) {
      cin >> box[i];
      sum += box[i];
    }
    ans = sum;
    for (int i = k; i < n; i++) {
      cin >> box[i];
      sum += box[i] - box[i - k];
      if (sum > ans)
        ans = sum;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00439 (C++)

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

const int MAX_N = 1 << 17, INF = 1 << 24;

class SegmentTree {
public:
  SegmentTree(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++) {
      data[i] = 0;
    }
  }

  void add(int k, int a) {
    k += n - 1;
    data[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = data[k * 2 + 1] + data[k * 2 + 2];
    }
  }

  int calculate(int a, int b) { return _calculate(a, b, 0, 0, n); }

private:
  int data[MAX_N];
  int n;
  int _calculate(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;

    if (a <= l && r <= b)
      return data[k];
    else {
      int l_child = _calculate(a, b, 2 * k + 1, l, (l + r) / 2),
          r_child = _calculate(a, b, 2 * k + 2, (l + r) / 2, r);
      return l_child + r_child;
    }
  }
};

int main() {
  int n, k;
  while (std::cin >> n >> k, n) {
    SegmentTree st(n);
    for (int i = 0; i < n; i++) {
      int a_i;
      std::cin >> a_i;
      st.add(i, a_i);
    }

    int res = -INF;

    for (int i = 0; i + k - 1 < n; i++) {
      res = std::max(res, st.calculate(i, i + k));
    }

    std::cout << res << std::endl;
  }
}

Fixed submission:
#include <iostream>

const int MAX_N = 1 << 18, INF = 1 << 24;

class SegmentTree {
public:
  SegmentTree(int _n) {
    n = 1;
    while (n < _n)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; i++) {
      data[i] = 0;
    }
  }

  void add(int k, int a) {
    k += n - 1;
    data[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = data[k * 2 + 1] + data[k * 2 + 2];
    }
  }

  int calculate(int a, int b) { return _calculate(a, b, 0, 0, n); }

private:
  int data[MAX_N];
  int n;
  int _calculate(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;

    if (a <= l && r <= b)
      return data[k];
    else {
      int l_child = _calculate(a, b, 2 * k + 1, l, (l + r) / 2),
          r_child = _calculate(a, b, 2 * k + 2, (l + r) / 2, r);
      return l_child + r_child;
    }
  }
};

int main() {
  int n, k;
  while (std::cin >> n >> k, n) {
    SegmentTree st(n);
    for (int i = 0; i < n; i++) {
      int a_i;
      std::cin >> a_i;
      st.add(i, a_i);
    }

    int res = -INF;

    for (int i = 0; i + k - 1 < n; i++) {
      res = std::max(res, st.calculate(i, i + k));
    }

    std::cout << res << std::endl;
  }
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;

int a[10000];

int main() {
  int n, k;
  while (cin >> n >> k && n && k) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    ll pre = 0;
    for (int i = 0; i < k; i++) {
      pre += a[i];
    }

    ll ret = pre;
    for (int i = 0; i < n - k; i++) {
      pre = pre - a[i] + a[k + i];
      ret = max(ret, pre);
    }

    cout << ret << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;

int a[1000000];

int main() {
  int n, k;
  while (cin >> n >> k && n && k) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }

    ll pre = 0;
    for (int i = 0; i < k; i++) {
      pre += a[i];
    }

    ll ret = pre;
    for (int i = 0; i < n - k; i++) {
      pre = pre - a[i] + a[k + i];
      ret = max(ret, pre);
    }

    cout << ret << endl;
  }
}
Problem p00439 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <deque>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

int ini[10001];
int main() {
  while (1) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (n == 0)
      break;
    rep(i, n) { scanf("%d", &ini[i]); }

    int sum = 0;
    rep(i, m) { sum += ini[i]; }

    int maxi = sum;
    for (int i = m; i < n; i++) {
      sum -= ini[i - m];
      sum += ini[i];
      maxi = max(maxi, sum);
    }

    printf("%d\n", maxi);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <deque>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)

int ini[100001];
int main() {
  while (1) {
    int n, m;
    scanf("%d%d", &n, &m);
    if (n == 0)
      break;
    rep(i, n) { scanf("%d", &ini[i]); }

    int sum = 0;
    rep(i, m) { sum += ini[i]; }

    int maxi = sum;
    for (int i = m; i < n; i++) {
      sum -= ini[i - m];
      sum += ini[i];
      maxi = max(maxi, sum);
    }

    printf("%d\n", maxi);
  }
}