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

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

int n;
char chain[10000];

int ccount(int a, int b, int cnt) {
  if (chain[a] != chain[b])
    return cnt;
  int len = 0;
  int i, j;
  for (i = a; i >= 0 && chain[a] == chain[i]; i--, len++)
    ;
  for (j = b; j < n && chain[b] == chain[j]; j++, len++)
    ;
  if (len < 4)
    return cnt;
  if (i == -1 || j == n)
    return cnt + len;
  return ccount(i, j, cnt + len);
}

int main() {
  while (true) {
    scanf("%d", &n);
    if (!n)
      return 0;
    int ans = 0;
    for (int i = 0; i < n; i++)
      scanf("%d", &chain[i]);
    for (int i = 0; i < n; i++) {
      char def = chain[i];
      for (int j = 1; j <= 3; j++) {
        chain[i] = j;
        int s, g, cnt = 1;
        for (s = i - 1; s >= 0 && chain[s] == chain[i]; s--, cnt++)
          ;
        for (g = i + 1; g < n && chain[g] == chain[i]; g++, cnt++)
          ;
        if (cnt >= 4) {
          if (s == -1 || g == n)
            ans = max(cnt, ans);
          else
            ans = max(ccount(s, g, cnt), ans);
        }
      }
      chain[i] = def;
    }
    printf("%d\n", n - ans);
  }
}

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

int n;
int chain[10002];

int ccount(int a, int b, int cnt) {
  if (chain[a] != chain[b])
    return cnt;
  int len = 0;
  int i, j;
  for (i = a; i >= 0 && chain[a] == chain[i]; i--, len++)
    ;
  for (j = b; j < n && chain[b] == chain[j]; j++, len++)
    ;
  if (len < 4)
    return cnt;
  if (i == -1 || j == n)
    return cnt + len;
  return ccount(i, j, cnt + len);
}

int main() {
  while (true) {
    scanf("%d", &n);
    if (!n)
      return 0;
    int ans = 0;
    for (int i = 0; i < n; i++)
      scanf("%d", &chain[i]);
    for (int i = 0; i < n; i++) {
      char def = chain[i];
      for (int j = 1; j <= 3; j++) {
        chain[i] = j;
        int s, g, cnt = 1;
        for (s = i - 1; s >= 0 && chain[s] == chain[i]; s--, cnt++)
          ;
        for (g = i + 1; g < n && chain[g] == chain[i]; g++, cnt++)
          ;
        if (cnt >= 4) {
          if (s == -1 || g == n)
            ans = max(cnt, ans);
          else
            ans = max(ccount(s, g, cnt), ans);
        }
      }
      chain[i] = def;
    }
    printf("%d\n", n - ans);
  }
}
Problem p00457 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10010] = {0};
int b[10010] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    w = n;
    ans = n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        memcpy(b, a, sizeof(int) * n);
        if (b[i] != j) {
          b[i] = j;
          kotani(0, 1, 1, n);
        }
        free(b);
      }
    }
    printf("%d\n", ans);
  }
}
void kotani(int i, int j, int c, int n) {
  if (b[i] == b[j] && j < n) {
    kotani(i, j + 1, c + 1, n);
  } else {
    if (c >= 4) {
      int u = 0;
      for (int z = j; z < n; z++) {
        b[i + u] = b[z];
        u++;
      }
      ans = min(ans, n - c);
      kotani(0, 1, 1, n - c);
    } else if (i + 1 < n) {
      kotani(i + 1, i + 2, 1, n);
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a[10010] = {0};
int b[10010] = {0};
int ans = 1001, w;
void kotani(int i, int j, int c, int n);
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    w = n;
    ans = n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 3; j++) {
        memcpy(b, a, sizeof(int) * n);
        if (b[i] != j) {
          b[i] = j;
          kotani(0, 1, 1, n);
        }
      }
    }
    printf("%d\n", ans);
  }
}
void kotani(int i, int j, int c, int n) {
  if (b[i] == b[j] && j < n) {
    kotani(i, j + 1, c + 1, n);
  } else {
    if (c >= 4) {
      int u = 0;
      for (int z = j; z < n; z++) {
        b[i + u] = b[z];
        u++;
      }
      ans = min(ans, n - c);
      kotani(0, 1, 1, n - c);
    } else if (i + 1 < n) {
      kotani(i + 1, i + 2, 1, n);
    }
  }
}
Problem p00457 (C++)

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

int n;
int a[10000];
int b[10001];
int ans = 1e9;

int c(int x, int y) { // change b[x] to y return remaining
  b[x] = y;
  int e = n;
  bool f = true;
  while (f) {
    f = false;
    int d = 1;
    int p = b[0];
    for (int i = 1; i < n + 1; ++i) {
      if (b[i] == 4)
        continue;
      if (b[i] == p)
        ++d;
      else {
        if (d > 3) {
          f = true;
          int j = 0, l = i - 1;
          while (j < d) {
            if (b[l] == 4) {
              --l;
              continue;
            }
            b[l] = 4;
            ++j;
            --l;
            --e;
/*						for(int k=0; k<n; ++k) printf("%d ", b[k]);
						printf("\n");
*/					}
        }
        d = 1;
      }
      p = b[i];
    }
  }
  /*	if(x == 5){
                  for(int i=0; i<n; ++i) printf("%d ", b[i]);
                  printf("\n");
          }
  */
  return e;
}

int main() {
  while (1) {
    memset(a, 0, sizeof(a));
    scanf("%d", &n);
    if (n == 0)
      return 0;
    for (int i = 0; i < n; ++i)
      scanf("%d", &a[i]);
    ans = 1e9;

    for (int i = 0; i < n; ++i)
      for (int j = 1; j < 4; ++j) {
        for (int k = 0; k < n; ++k)
b[k] = a[k];
        ans = min(ans, c(i, j));
        //				printf("%d %d\n", i, j);
        //				printf("%d\n", ans);
      }
    printf("%d\n", ans);
  }
}

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

int n;
int a[10000];
int b[10001];
int ans = 1e9;

int c(int x, int y) { // change b[x] to y return remaining
  b[x] = y;
  int e = n;
  bool f = true;
  while (f) {
    f = false;
    int d = 1;
    int p = b[0];
    for (int i = 1; i < n + 1; ++i) {
      if (b[i] == 4)
        continue;
      if (b[i] == p)
        ++d;
      else {
        if (d > 3) {
          f = true;
          int j = 0, l = i - 1;
          while (j < d) {
            if (b[l] == 4) {
              --l;
              continue;
            }
            b[l] = 4;
            ++j;
            --l;
            --e;
/*						for(int k=0; k<n; ++k) printf("%d ", b[k]);
						printf("\n");
*/					}
        }
        d = 1;
      }
      p = b[i];
    }
  }
  /*	if(x == 5){
                  for(int i=0; i<n; ++i) printf("%d ", b[i]);
                  printf("\n");
          }
  */
  return e;
}

int main() {
  while (1) {
    memset(a, 0, sizeof(a));
    scanf("%d", &n);
    if (n == 0)
      return 0;
    for (int i = 0; i < n; ++i)
      scanf("%d", &a[i]);
    ans = 1e9;

    for (int i = 0; i < n; ++i)
      for (int j = 1; j < 4; ++j) {
        for (int k = 0; k < n; ++k)
b[k] = a[k];
        if (a[i] != j) {
ans = min(ans, c(i, j));
        }
        //				printf("%d %d\n", i, j);
        //				printf("%d\n", ans);
      }
    printf("%d\n", ans);
  }
}
Problem p00458 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
bool maps[100][100];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int search(int y, int x) {
  int maxim = 0;
  maps[y][x] = false;
  REP(i, 4)
  if (y + dy[i] && x + dx[i] && maps[y + dy[i]][x + dx[i]])
    maxim = max(maxim, search(y + dy[i], x + dx[i]));
  maps[y][x] = true;
  return maxim + 1;
}
int main() {
  // your code goes here
  int m, n;
  while (cin >> m >> n && n) {
    int maxim = 0;
    REP(i, n)
    REP(j, m)
    cin >> maps[i][j];
    REP(i, n)
    REP(j, m)
    if (maps[i][j])
      maxim = max(maxim, search(i, j));
    cout << maxim << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
bool maps[100][100];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int search(int y, int x) {
  int maxim = 0;
  maps[y][x] = false;
  REP(i, 4)
  if (y + dy[i] >= 0 && x + dx[i] >= 0 && maps[y + dy[i]][x + dx[i]])
    maxim = max(maxim, search(y + dy[i], x + dx[i]));
  maps[y][x] = true;
  return maxim + 1;
}
int main() {
  // your code goes here
  int m, n;
  while (cin >> m >> n && n) {
    int maxim = 0;
    REP(i, n)
    REP(j, m)
    cin >> maps[i][j];
    REP(i, n)
    REP(j, m)
    if (maps[i][j])
      maxim = max(maxim, search(i, j));
    cout << maxim << endl;
  }
  return 0;
}
Problem p00458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define INF 1234567890

int map[60][60];
int x_max, y_max;
int ans;

int dp(int x, int y) {
  if (map[x][y] == 0)
    return 0;
  int answer = 1;
  map[x][y] = 0;
  if (map[x][y - 1] == 1)
    answer = max(answer, dp(x, y - 1) + 1);
  if (map[x][y + 1] == 1)
    answer = max(answer, dp(x, y + 1) + 1);
  if (map[x - 1][y] == 1)
    answer = max(answer, dp(x - 1, y) + 1);
  if (map[x + 1][y] == 1)
    answer = max(answer, dp(x + 1, y) + 1);
  map[x][y] = 1;
  return answer;
}

int main() {
  while (1) {
    memset(map, 0, sizeof(map));
    ans = 0;

    scanf("%d%d", &x_max, &y_max);
    if (x_max == 0 && y_max == 0)
      break;
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        scanf("%d", &map[a][b]);
      }
    }
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        ans = max(ans, dp(a, b));
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define INF 1234567890

int map[100][100];
int x_max, y_max;
int ans;

int dp(int x, int y) {
  if (map[x][y] == 0)
    return 0;
  int answer = 1;
  map[x][y] = 0;
  if (map[x][y - 1] == 1)
    answer = max(answer, dp(x, y - 1) + 1);
  if (map[x][y + 1] == 1)
    answer = max(answer, dp(x, y + 1) + 1);
  if (map[x - 1][y] == 1)
    answer = max(answer, dp(x - 1, y) + 1);
  if (map[x + 1][y] == 1)
    answer = max(answer, dp(x + 1, y) + 1);
  map[x][y] = 1;
  return answer;
}

int main() {
  while (1) {
    memset(map, 0, sizeof(map));
    ans = 0;

    scanf("%d%d", &x_max, &y_max);
    if (x_max == 0 && y_max == 0)
      break;
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        scanf("%d", &map[a][b]);
      }
    }
    for (int a = 1; a <= x_max; a++) {
      for (int b = 1; b <= y_max; b++) {
        ans = max(ans, dp(a, b));
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00460 (C++)

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

using namespace std;

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

int dp[50][2][3001];

int main() {

  int n, m, s, nn, st, i, j, k;
  int t1 = 0, t2 = 1;
  while (1) {
    scanf("%d%d%d", &n, &m, &s);
    if (n == 0)
      break;

    t1 = 0;
    t2 = 1;
    n *= n;
    rep(i, 50) rep(j, 2) rep(k, s + 1) dp[i][j][k] = 0;
    dp[0][0][0] = 1;
    reps(j, m) {

      dp[0][t2][0] = 1;

      reps(i, n) {
        nn = i;
        st = (nn * (nn + 1)) / 2;
        int mm, pp, end;
        mm = (j - 1) / 2;
        if (j % 2 == 0)
          pp = j / 2;
        else
          pp = 0;

        end = mm * (mm + 1) + pp + st;

        for (k = st - 1; k <= end; k++) {
          dp[i][t2][k] = 0;
          if (k - j >= 0) {
            dp[i][t2][k] += dp[i - 1][t1][k - j];
          }
          dp[i][t2][k] += dp[i][t1][k];
          if (dp[i][t2][k] < 0)
            puts("AAAAAAA");
          dp[i][t2][k] %= 100000;
        }
        /*
        if(i<9&&j<12){
                printf("%d %d\n",i,j);
                for(int k=0;k<50;k++){
                        printf("%d ",dp[i][t2][k]);
                }puts("");
        }*/
      }
      swap(t1, t2);
    }

    printf("%d\n", dp[n][t1][s]);
  }
}

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

using namespace std;

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

int dp[50][2][3001];

int main() {

  int n, m, s, nn, st, i, j, k;
  int t1 = 0, t2 = 1;
  while (1) {
    scanf("%d%d%d", &n, &m, &s);
    if (n == 0)
      break;

    t1 = 0;
    t2 = 1;
    n *= n;
    rep(i, 50) rep(j, 2) rep(k, s + 1) dp[i][j][k] = 0;
    dp[0][0][0] = 1;
    reps(j, m) {

      dp[0][t2][0] = 1;

      reps(i, n) {
        nn = i;
        st = (nn * (nn + 1)) / 2;
        int mm, pp, end;
        mm = (j - 1) / 2;
        if (j % 2 == 0)
          pp = j / 2;
        else
          pp = 0;

        end = min(mm * (mm + 1) + pp + st, s + 1);

        for (k = st - 1; k <= end; k++) {
          dp[i][t2][k] = 0;
          if (k - j >= 0) {
            dp[i][t2][k] += dp[i - 1][t1][k - j];
          }
          dp[i][t2][k] += dp[i][t1][k];
          if (dp[i][t2][k] < 0)
            puts("AAAAAAA");
          dp[i][t2][k] %= 100000;
        }
        /*
        if(i<9&&j<12){
                printf("%d %d\n",i,j);
                for(int k=0;k<50;k++){
                        printf("%d ",dp[i][t2][k]);
                }puts("");
        }*/
      }
      swap(t1, t2);
    }

    printf("%d\n", dp[n][t1][s]);
  }
}
Problem p00460 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mod 100000
typedef pair<int, int> P;
int n, m, s, ans;
int dp[2][3010][4010];
int main() {
  while (cin >> n >> m >> s && n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0][0] = 1;
    for (int i = 0; i < n * n; ++i) {
      for (int j = 0; j < m; ++j)
        for (int k = j; k < s; ++k)
          dp[1][j + 1][k + 1] = (dp[1][j][k] + dp[0][j][k - j]) % mod;
      for (int j = 0; j <= m; ++j) {
        for (int k = 0; k <= s; ++k) {
          dp[0][j][k] = dp[1][j][k];
          dp[1][j][k] = 0;
        }
      }
    }
    ans = 0;
    for (int i = 0; i <= m; ++i) {
      ans += dp[0][i][s];
      ans %= mod;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mod 100000
typedef pair<int, int> P;
int n, m, s, ans;
int dp[2][2001][3001];
int main() {
  while (cin >> n >> m >> s && n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0][0] = 1;
    for (int i = 0; i < n * n; ++i) {
      for (int j = 0; j < m; ++j)
        for (int k = j; k < s; ++k)
          dp[1][j + 1][k + 1] = (dp[1][j][k] + dp[0][j][k - j]) % mod;
      for (int j = 0; j <= m; ++j) {
        for (int k = 0; k <= s; ++k) {
          dp[0][j][k] = dp[1][j][k];
          dp[1][j][k] = 0;
        }
      }
    }
    ans = 0;
    for (int i = 0; i <= m; ++i) {
      ans += dp[0][i][s];
      ans %= mod;
    }
    cout << ans << endl;
  }
}
Problem p00460 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <string.h>
using namespace std;

int n, m, s, ans;
int dp[2][2001][3001];

int main() {
  while (scanf("%d%d%d", &n, &m, &s) && n && m && s) {
    for (int i = 1; i <= m; i++)
      dp[0][i][i] = 1;

    for (int i = 2; i <= n * n; i++) {
      for (int j = 1; j <= m; j++) {
        for (int k = 1; k < j; k++) {
          for (int l = 1; l <= s; l++) {
            if (j + l > s) {
              break;
            } else {
              dp[1][j][j + l] += dp[0][k][l];
              dp[1][j][j + l] %= 100000;
            }
          }
        }
      }
      for (int j = 1; j <= m; j++) {
        for (int k = 1; k <= s; k++) {
          dp[0][j][k] = dp[1][j][k];
          dp[1][j][k] = 0;
        }
      }
    }

    for (int i = 1; i <= m; i++) {
      ans += dp[0][i][s];
      ans %= 100000;
    }

    printf("%d\n", ans);
    memset(dp, 0, sizeof dp);
    ans = 0;
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <string.h>
using namespace std;

int n, m, s, ans;
int dp[2][2001][3001];

int main() {
  while (scanf("%d%d%d", &n, &m, &s) && n && m && s) {
    for (int i = 1; i <= m; i++)
      dp[0][i][i] = 1;

    for (int i = 2; i <= n * n; i++) {
      for (int j = 1; j < m; j++) {
        for (int k = j; k < s; k++) {
          dp[1][j + 1][k + 1] += dp[1][j][k];
          dp[1][j + 1][k + 1] += dp[0][j][k - j];
          dp[1][j + 1][k + 1] %= 100000;
        }
      }
      for (int j = 1; j <= m; j++) {
        for (int k = 1; k <= s; k++) {
          dp[0][j][k] = dp[1][j][k];
          dp[1][j][k] = 0;
        }
      }
    }

    for (int i = 1; i <= m; i++) {
      ans += dp[0][i][s];
      ans %= 100000;
    }

    printf("%d\n", ans);
    memset(dp, 0, sizeof dp);
    ans = 0;
  }

  return 0;
}
Problem p00460 (C++)

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

const int mod = 100000;
int main() {
  int n, m, s;
  int dp[50][2001];
  while (cin >> n >> m >> s, n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 1; i <= m; ++i) {
      for (int j = n * n - 1; j >= 0; --j) {
        for (int k = 0; k <= s - i; ++k) {
          dp[j + 1][k + i] += dp[j][k];
          dp[j + 1][k + i] %= mod;
        }
      }
    }
    cout << dp[n * n][s] << endl;
  }
  return 0;
}

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

const int mod = 100000;
int main() {
  int n, m, s;
  int dp[50][3001];
  while (cin >> n >> m >> s, n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 1; i <= m; ++i) {
      for (int j = n * n - 1; j >= 0; --j) {
        for (int k = 0; k <= s - i; ++k) {
          dp[j + 1][k + i] += dp[j][k];
          dp[j + 1][k + i] %= mod;
        }
      }
    }
    cout << dp[n * n][s] << endl;
  }
  return 0;
}
Problem p00460 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
int dp[50][3001];
int main() {
  int n, m, s;
  while (scanf("%d %d %d", &n, &m, &s), n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 1; i <= m; i++)
      for (int j = n * n; j >= 1; j--)
        for (int k = 1; k <= s; k++) {
          dp[j][k] += dp[j - 1][k - i];
          if (dp[j][k] >= 100000)
            dp[j][k] -= 100000;
        }
    printf("%d\n", dp[n * n][s]);
  }
}

Fixed submission:
#include <cstdio>
#include <cstring>
int dp[50][3001];
int main() {
  int n, m, s;
  while (scanf("%d %d %d", &n, &m, &s), n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 1; i <= m; i++)
      for (int j = n * n; j >= 1; j--)
        for (int k = i; k <= s; k++) {
          dp[j][k] += dp[j - 1][k - i];
          if (dp[j][k] >= 100000)
            dp[j][k] -= 100000;
        }
    printf("%d\n", dp[n * n][s]);
  }
}
Problem p00460 (C++)

Buggy submission (status: Runtime Error):

// y09-6 ビンゴ(2回目)
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define LL long long
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define segment_size 65536
#define ROOP() while (true)

int main() {
  ROOP() {
    int N, M, S;
    cin >> N >> M >> S;
    if (N == 0)
      return 0;

    int dp[50][3001];
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 3001; j++) {
        dp[i][j] = 0;
      }
    }
    dp[0][0] = 1;

    for (int i = 1; i <= M; i++) {
      for (int j = N * N; j >= 1; j--) {
        for (int k = i; k <= S; k++) {
          dp[j][k] += dp[j - 1][k - i];
          dp[i][k] %= 100000;
        }
      }
    }
    cout << dp[N * N][S] << endl;
  }
  return 0;
}


Fixed submission:

// y09-6 ビンゴ(2回目)
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define LL long long
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define segment_size 65536
#define ROOP() while (true)

int main() {
  ROOP() {
    int N, M, S;
    cin >> N >> M >> S;
    if (N == 0)
      return 0;

    int dp[50][3001];
    for (int i = 0; i < 50; i++) {
      for (int j = 0; j < 3001; j++) {
        dp[i][j] = 0;
      }
    }
    dp[0][0] = 1;

    for (int i = 1; i <= M; i++) {
      for (int j = N * N; j >= 1; j--) {
        for (int k = i; k <= S; k++) {
          dp[j][k] += dp[j - 1][k - i];
          dp[j][k] %= 100000;
        }
      }
    }
    cout << dp[N * N][S] << endl;
  }
  return 0;
}
Problem p00460 (C++)

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

using namespace std;

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

int dp[50][3001];
const int MOD = 100000;

int main() {
  while (true) {
    int n, m, s;
    cin >> n >> m >> s;
    if (n == 0)
      break;
    n = n * n;
    rep(i, 50) rep(j, 3001) dp[i][j] = 0;
    dp[0][0] = 1;
    rep(i, m + 1) if (i > 0) for (int j = n; j > 0; --j) for (int k = j; k <= s;
                                                              ++k) {
      dp[j][k] += dp[j - 1][k - i];
      dp[j][k] %= MOD;
    }
    cout << dp[n][s] << endl;
  }
}

Fixed submission:
#include <iostream>

using namespace std;

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

int dp[50][3001];
const int MOD = 100000;

int main() {
  while (true) {
    int n, m, s;
    cin >> n >> m >> s;
    if (n == 0)
      break;
    n = n * n;
    rep(i, 50) rep(j, 3001) dp[i][j] = 0;
    dp[0][0] = 1;
    rep(i, m + 1) if (i > 0) for (int j = n; j > 0; --j) for (int k = i; k <= s;
                                                              ++k) {
      dp[j][k] += dp[j - 1][k - i];
      dp[j][k] %= MOD;
    }
    cout << dp[n][s] << endl;
  }
}
Problem p00460 (C++)

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

const int mod = 100000;
int n, m, s;

int dp[50][2010][3100];

int dfs(int i = 0, int pre = 0, int sum = s) {
  if (sum < 0)
    return 0;
  if (pre > m)
    return 0;

  if (i == n * n) {
    return sum == 0;
  }

  int &ret = dp[i][pre][sum];

  if (ret != -1)
    return ret;
  ret = 0;
  for (int j = pre + 1; j <= m; j++) {
    ret += dfs(i + 1, j, sum - j);
    ret %= mod;
  }
  return ret;
}

signed main() {
  while (cin >> n >> m >> s, n) {
    memset(dp, -1, sizeof(dp));

    cout << dfs() << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
main() {
  for (int n, m, s, i, j, d[50][3500], M = 1e5;
       std::cin >> n >> m >> s, n *= n;) {
    std::fill(d[0], d[50], M);
    d[0][0] = 1;
    for (i = 1; i <= n; ++i)
      for (j = 0; j <= s; ++j) {
        if (j >= i)
          d[i][j] += d[i - 1][j - i] + d[i][j - i];
        if (j > m)
          d[i][j] -= d[i - 1][j - m - 1];
        d[i][j] %= M;
      }
    printf("%d\n", d[n][s]);
  }
}
Problem p00461 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int n, m;
string s;

int main() {
  while (cin >> n) {
    if (!n)
      return 0;
    cin >> m >> s;
    int a = 0;
    string t = "I";
    for (int i = 0; i < n; i++)
      t += "OI";
    while (s.find(t) != string::npos) {
      s = s.substr(s.find(t) + 1);
      a++;
    }
    cout << a << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int n, m;
string s;

int main() {
  while (cin >> n) {
    if (!n)
      return 0;
    cin >> m >> s;
    int a = 0, b = 0;
    for (int i = 0; i < m; i++) {
      if (s[i] == 'I' && b % 2 == 0 || s[i] == 'O' && b % 2 == 1)
        b++;
      else {
        if (s[i] == 'I')
          b = 1;
        else
          b = 0;
      }
      if (b % 2 == 1 && b >= 2 * n + 1)
        a++;
    }
    cout << a << endl;
  }
}
Problem p00461 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main() {
  //	cut here before submit
  //	freopen ("testcase.IOIOI", "r", stdin );
  int n;
  while (scanf("%d", &n), n) {
    int m;
    scanf("%d", &m);
    string s = "";
    cin >> s;
    string search = "";
    rep(i, n) { search += "IO"; } // end rep
    search += 'I';
    //		cerr << search << endl;
    int res = 0;
    rep(i, m) {
      if (s[i] == 'O')
        continue;
      if (s[i] == 'I' && i + 1 < m && s[i + 1] != 'O')
        continue;
      string sub = s.substr(i);
      if (sub.find(search) != string::npos) {
        res++;
      } // end if
    }   // end rep
    printf("%d\n", res);
  } // en drep

  return 0;
}

Fixed submission:
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main() {
  //	cut here before submit
  //	freopen ("testcase.IOIOI", "r", stdin );
  int n;
  while (scanf("%d", &n), n) {
    int m;
    scanf("%d", &m);
    string s = "";
    cin >> s;
    string search = "";
    rep(i, n) { search += "IO"; } // end rep
    search += 'I';
    //		cerr << search << endl;
    int res = 0;
    rep(i, m) {
      if (s[i] == 'O')
        continue;
      if (s[i] == 'I' && i + 1 < m && s[i + 1] != 'O')
        continue;
      string sub = s.substr(i, 2 * n + 1);
      if (sub == search) {
        res++;
      } // end if
    }   // end rep
    printf("%d\n", res);
  } // en drep

  return 0;
}
Problem p00461 (C++)

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

using namespace std;

int main() {
  int n, l;
  while (cin >> n >> l, n || l) {
    string str;
    cin >> str;

    int mode = 0;
    int chain = 0;
    int ans = 0;
    for (int i = 0; i < str.size(); ++i) {
      if (str[i] == 'I') {
        switch (mode) {
        case 0:
          mode = 1;
          break;
        case 1:
          chain = 0;
          break;
        case 2:
          if (++chain >= n) {
            ++ans;
            --chain;
          }
          mode = 1;
          break;
        }
      } else {
        switch (mode) {
        case 0:
          break;
        case 1:
          mode = 2;
          break;
        case 2:
          mode = 0;
          chain = 0;
          break;
        }
      }
    }
    cout << ans << endl;
  }
}

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

using namespace std;

int main() {
  int n, l;
  while (cin >> n, n) {
    cin >> l;
    string str;
    cin >> str;

    int mode = 0;
    int chain = 0;
    int ans = 0;
    for (int i = 0; i < str.size(); ++i) {
      if (str[i] == 'I') {
        switch (mode) {
        case 0:
          mode = 1;
          break;
        case 1:
          chain = 0;
          break;
        case 2:
          if (++chain >= n) {
            ++ans;
            --chain;
          }
          mode = 1;
          break;
        }
      } else {
        switch (mode) {
        case 0:
          break;
        case 1:
          mode = 2;
          break;
        case 2:
          mode = 0;
          chain = 0;
          break;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
ll n, m;
string s;
ll dp[100001];
ll ans = 0;
int main() {
  while (1) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    if (!n)
      break;
    cin >> m >> s;
    ans = 0;
    for (ll i = 0; i < m; i++) {
      if (i > 0 && s[i - 1] != s[i]) {
        dp[i] = dp[i - 1] + 1;
      } else if (s[i] == 'I')
        dp[i] = 1;

      if (s[i] == 'I' && dp[i] > 2 * n) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
ll n, m;
string s;
ll dp[1000001];
ll ans = 0;
int main() {
  while (1) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    if (!n)
      break;
    cin >> m >> s;
    ans = 0;
    for (ll i = 0; i < m; i++) {
      if (i > 0 && s[i - 1] != s[i]) {
        dp[i] = dp[i - 1] + 1;
      } else if (s[i] == 'I')
        dp[i] = 1;

      if (s[i] == 'I' && dp[i] > 2 * n) {
        ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00461 (C++)

Buggy submission (status: Runtime Error):
#include "stdio.h"

char s[1111111];
int w[1111111];
int h[1000000];

int main() {
  int noww = 0;
  while (1) {
    int n;
    int m;

    {
      scanf("%d", &n);
      if (n == 0) {
        break;
      }
      scanf("%d", &m);
      scanf("%s", s + 1);
    }

    {
      s[0] = s[1];
      w[0] = 0;
      for (int i = 1; i <= m; i++) {
        if (s[i - 1] != s[i]) {
          w[i] = w[i - 1] + 1;
        } else {
          w[i] = 1;
        }
      }
    }

    {
      h[noww] = 0;
      for (int i = 1; i <= m; i++) {
        if (w[i] > n * 2 && s[i] == 'I') {
          h[noww]++;
        }
      }
      noww++;
    }
  }

  for (int i = 0; noww; i++) {
    printf("%d\n", h[i]);
  }

  return 0;
}

Fixed submission:
#include "stdio.h"

char s[1111111];
int w[1111111];
int h[1000000];

int main() {
  int noww = 0;
  while (1) {
    int n;
    int m;

    {
      scanf("%d", &n);
      if (n == 0) {
        break;
      }
      scanf("%d", &m);
      scanf("%s", s + 1);
    }

    {
      s[0] = s[1];
      w[0] = 0;
      for (int i = 1; i <= m; i++) {
        if (s[i - 1] != s[i]) {
          w[i] = w[i - 1] + 1;
        } else {
          w[i] = 1;
        }
      }
    }

    {
      h[noww] = 0;
      for (int i = 1; i <= m; i++) {
        if (w[i] > n * 2 && s[i] == 'I') {
          h[noww]++;
        }
      }
      noww++;
    }
  }

  for (int i = 0; i < noww; i++) {
    printf("%d\n", h[i]);
  }

  return 0;
}
Problem p00461 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {

  int n;
  int m;
  string s;
  while (cin >> n, n != 0) {
    cin >> m >> s;
    int p[100] = {0};
    int f = 1;
    for (int i = 0; i < m; i++) {
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        for (int j = 1; j <= f; j++)
          p[j]++;
        f++;
        i++;
      } else {
        f = 1;
      }
    }

    cout << p[n] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {

  int n;
  int m;
  string s;
  while (cin >> n, n != 0) {
    cin >> m >> s;
    int p[1000001] = {0};
    int f = 1;
    for (int i = 0; i < m; i++) {
      if (s[i] == 'I' && s[i + 1] == 'O' && s[i + 2] == 'I') {
        for (int j = 1; j <= f; j++)
          p[j]++;
        f++;
        i++;
      } else {
        f = 1;
      }
    }

    cout << p[n] << endl;
  }
}
Problem p00461 (C++)

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

int main(int argc, char *argv[]) {
  int n, m;
  string s;
  while (std::cin >> n >> m >> s, n) {
    string p;
    for (int i = 0; i < n; i++) {
      p += "IO";
    }
    p += "I";
    if (p.length() > s.length()) {
      std::cout << 0 << std::endl;
      return 0;
    }
    int ans = 0;
    for (int i = 0; i < s.length() - p.length(); i++) {
      for (int j = 0; j < p.length(); j++) {
        if (s[i + j] != p[j])
          break;
        if (j == p.length() - 1)
          ans++;
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

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

int main(int argc, char *argv[]) {
  int n, m;
  string s;
  while (std::cin >> n >> m >> s, n) {
    string p;
    for (int i = 0; i < n; i++) {
      p += "IO";
    }
    p += "I";
    if (p.length() > s.length()) {
      std::cout << 0 << std::endl;
      return 0;
    }
    int ans = 0, pos = 0;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] != p[pos]) {
        if (s[i] == 'I')
          pos = 1;
        else
          pos = 0;
      } else {
        pos++;
      }
      if (pos == p.length()) {
        ans++;
        i -= p.length() - 1;
        pos = 0;
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
Problem p00462 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

static const int MAX_BUCKET = 1000;
vector<int> bucket[MAX_BUCKET];

int compute_dist(int dist, int lhs, int rhs) {
  return min(dist - abs(lhs - rhs), abs(lhs - rhs));
}

int main() {
  int total_dist, store_num, order_num;
  while (~scanf("%d", &total_dist)) {
    if (total_dist == 0)
      break;
    scanf("%d", &store_num);
    scanf("%d", &order_num);

    vector<int> store;
    store.push_back(0);
    for (int i = 0; i < store_num - 1; i++) {
      int num;
      scanf("%d", &num);
      store.push_back(num);
    }
    sort(store.begin(), store.end());

    int cost = 0;
    for (int i = 0; i < order_num; i++) {
      int num;
      scanf("%d", &num);

      int min_dist = INF;
      for (int j = 0; j < store.size(); j++) {
        min_dist = min(min_dist, compute_dist(total_dist, store[j], num));
      }
      cost += min_dist;
    }
    printf("%d\n", cost);
  }
}

Fixed submission:
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

static const int tx[] = {0, 1, 0, -1};
static const int ty[] = {-1, 0, 1, 0};

static const int MAX_BUCKET = 1000;
vector<int> bucket[MAX_BUCKET];

int compute_dist(int dist, int lhs, int rhs) {
  return min(dist - abs(lhs - rhs), abs(lhs - rhs));
}

int main() {
  int total_dist, store_num, order_num;
  while (~scanf("%d", &total_dist)) {
    if (total_dist == 0)
      break;
    scanf("%d", &store_num);
    scanf("%d", &order_num);

    vector<int> store;
    store.push_back(0);
    for (int i = 0; i < store_num - 1; i++) {
      int num;
      scanf("%d", &num);
      store.push_back(num);
    }
    sort(store.begin(), store.end());

    int cost = 0;
    for (int i = 0; i < order_num; i++) {
      int num;
      scanf("%d", &num);

      int idx = lower_bound(store.begin(), store.end(), num) - store.begin();
      int min_dist = compute_dist(total_dist, store[idx], num);

      for (int j = idx - 1; j >= 0; j--) {
        min_dist = min(min_dist, compute_dist(total_dist, store[j], num));
      }
      cost += min_dist;
    }
    printf("%d\n", cost);
  }
}
Problem p00462 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int main() {

  int d, n, m;
  while (cin >> d, d) {
    cin >> n >> m;
    int D[10001], M[10001];
    ;
    D[0] = 0, D[n] = d;
    for (int i = 1; i < n; i++)
      cin >> D[i];
    for (int i = 0; i < m; i++)
      cin >> M[i];
    sort(M, M + m);
    sort(D, D + n + 1);

    ll ans = 0;
    int pos = 1;
    for (int i = 0; i < m; i++) {
      while (D[pos] <= M[i])
        pos++;
      ans += min(abs(D[pos] - M[i]), abs(D[pos - 1] - M[i]));
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int main() {

  int d, n, m;
  while (cin >> d, d) {
    cin >> n >> m;
    int D[100001], M[100001];
    ;
    D[0] = 0, D[n] = d;
    for (int i = 1; i < n; i++)
      cin >> D[i];
    for (int i = 0; i < m; i++)
      cin >> M[i];
    sort(M, M + m);
    sort(D, D + n + 1);

    ll ans = 0;
    int pos = 1;
    for (int i = 0; i < m; i++) {
      while (D[pos] <= M[i])
        pos++;
      ans += min(abs(D[pos] - M[i]), abs(D[pos - 1] - M[i]));
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00462 (C++)

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

int d, n, m, shop[100000], k[10000];

int main() {
  while (true) {
    cin >> d;
    if (!d)
      break;
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
      cin >> shop[i];
    }
    shop[0] = 0;
    shop[n] = d;
    for (int i = 0; i < m; i++) {
      cin >> k[i];
    }

    sort(shop, shop + n);
    int sum = 0;
    for (int i = 0; i < m; i++) {
      int *s2 = upper_bound(shop, shop + n + 1, k[i]);
      int *s1 = s2 - 1;
      sum += min(k[i] - *s1, *s2 - k[i]);
    }
    cout << sum << endl;
  }
  return 0;
}

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

int d, n, m, shop[100001], k[10000];

int main() {
  while (true) {
    cin >> d;
    if (!d)
      break;
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
      cin >> shop[i];
    }
    shop[0] = 0;
    shop[n] = d;
    for (int i = 0; i < m; i++) {
      cin >> k[i];
    }

    sort(shop, shop + n);
    int sum = 0;
    for (int i = 0; i < m; i++) {
      int *s2 = upper_bound(shop, shop + n + 1, k[i]);
      int *s1 = s2 - 1;
      sum += min(k[i] - *s1, *s2 - k[i]);
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00462 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, m, d, k;
  int dist[100000];
  while (cin >> d && d) {
    int ans = 0;
    cin >> n >> m;
    rep(i, n - 1) { cin >> dist[i + 1]; }
    sort(dist + 1, dist + n);
    dist[0] = 0;
    dist[n] = d;
    while (m--) {
      cin >> k;
      int i = upper_bound(dist, dist + n + 1, k) - dist;
      ans += min(dist[i] - k, k - dist[i - 1]);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
using namespace std;
#define INF (1 << 29)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, m, d, k;
  int dist[100001];
  while (cin >> d && d) {
    int ans = 0;
    cin >> n >> m;
    rep(i, n - 1) { cin >> dist[i + 1]; }
    sort(dist + 1, dist + n);
    dist[0] = 0;
    dist[n] = d;
    while (m--) {
      cin >> k;
      int i = upper_bound(dist, dist + n + 1, k) - dist;
      ans += min(dist[i] - k, k - dist[i - 1]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00463 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define P pair<int, int>
using namespace std;

P e[10000];
int main() {
  int a, b, c, d;
  while (cin >> a >> b >> c >> d, a | b || c || d) {
    int f[1000], j[1000], sum = 0;
    for (int g = 0; g < a; g++)
      f[g] = g;
    for (int g = 0; g < a; g++)
      scanf("%d", &j[g]);
    for (int g = 0; g < b; g++) {
      int h, i;
      scanf("%d%d", &h, &i);
      h--;
      e[g] = P(i, h);
    }
    sort(e, e + b);
    for (int g = 0; g < b; g++) {
      swap(f[e[g].second], f[e[g].second + 1]);
    }
    int MAX = 0, k[1000];
    for (int l = 0; l < a; l++) {
      k[l] = l;
      if (f[l] < d)
        sum += j[l];
    }
    for (int g = b - 1; g >= 0; g--) {
      if (f[k[e[g].second]] < d) {
        if (f[k[e[g].second + 1]] >= d) {
          MAX = max(MAX, j[k[e[g].second]] - j[k[e[g].second + 1]]);
        }
      } else {
        if (f[k[e[g].second + 1]] < d) {
          MAX = max(MAX, j[k[e[g].second + 1]] - j[k[e[g].second]]);
        }
      }
      swap(k[e[g].second], k[e[g].second + 1]);
    }
    cout << sum - MAX << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define P pair<int, int>
using namespace std;

P e[100000];
int main() {
  int a, b, c, d;
  while (cin >> a >> b >> c >> d, a | b || c || d) {
    int f[1000], j[1000], sum = 0;
    for (int g = 0; g < a; g++)
      f[g] = g;
    for (int g = 0; g < a; g++)
      scanf("%d", &j[g]);
    for (int g = 0; g < b; g++) {
      int h, i;
      scanf("%d%d", &h, &i);
      h--;
      e[g] = P(i, h);
    }
    sort(e, e + b);
    for (int g = 0; g < b; g++) {
      swap(f[e[g].second], f[e[g].second + 1]);
    }
    int MAX = 0, k[1000];
    for (int l = 0; l < a; l++) {
      k[l] = l;
      if (f[l] < d)
        sum += j[l];
    }
    for (int g = b - 1; g >= 0; g--) {
      if (f[k[e[g].second]] < d) {
        if (f[k[e[g].second + 1]] >= d) {
          MAX = max(MAX, j[k[e[g].second]] - j[k[e[g].second + 1]]);
        }
      } else {
        if (f[k[e[g].second + 1]] < d) {
          MAX = max(MAX, j[k[e[g].second + 1]] - j[k[e[g].second]]);
        }
      }
      swap(k[e[g].second], k[e[g].second + 1]);
    }
    cout << sum - MAX << endl;
  }
}
Problem p00463 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int s[1000], d[1000], y[1000];
P p[100000];
int main() {
  int n, m, h, k;
  while (scanf("%d%d%d%d", &n, &m, &h, &k), n) {
    rep(i, n) scanf("%d", &s[i]);
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      p[i] = {b, a};
    }
    sort(p, p + m);
    iota(d, d + m, 0);
    rep(i, m) {
      swap(d[p[i].second - 1], d[p[i].second]);
      p[i] = {d[p[i].second - 1], d[p[i].second]};
      if (p[i].first > p[i].second)
        swap(p[i].first, p[i].second);
    }
    rep(i, n) y[d[i]] = s[i];
    int M = accumulate(y, y + k, 0);
    int S = M;
    rep(i, m) {
      if (p[i].first < k && p[i].second >= k)
        M = min(M, S + y[p[i].second] - y[p[i].first]);
    }
    printf("%d\n", M);
  }
}

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

int s[1000], d[1000], y[1000];
P p[100000];
int main() {
  int n, m, h, k;
  while (scanf("%d%d%d%d", &n, &m, &h, &k), n) {
    rep(i, n) scanf("%d", &s[i]);
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      p[i] = {b, a};
    }
    sort(p, p + m);
    iota(d, d + n, 0);
    rep(i, m) {
      swap(d[p[i].second - 1], d[p[i].second]);
      p[i] = {d[p[i].second - 1], d[p[i].second]};
      if (p[i].first > p[i].second)
        swap(p[i].first, p[i].second);
    }
    rep(i, n) y[d[i]] = s[i];
    int M = accumulate(y, y + k, 0);
    int S = M;
    rep(i, m) {
      if (p[i].first < k && p[i].second >= k)
        M = min(M, S + y[p[i].second] - y[p[i].first]);
    }
    printf("%d\n", M);
  }
}