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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
char str[310];
int dp[310][310];
int rev[310][310];
int main() {
  while (1) {
    scanf("%s", str);
    if (str[0] == '#')
      return 0;
    int ret = 0;
    string ans = "";
    int len = strlen(str);
    for (int i = 0; str[i + 1]; i++) {
      int n = i + 1;
      int m = len - m;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++)
          dp[j][k] = -999999;
      dp[0][0] = 0;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++) {
          if (j < n && dp[j + 1][k] < dp[j][k]) {
            dp[j + 1][k] = dp[j][k];
            rev[j + 1][k] = 1;
          }
          if (k < m && dp[j][k + 1] < dp[j][k]) {
            dp[j][k + 1] = dp[j][k];
            rev[j][k + 1] = 2;
          }
          if (j < n && k < m && dp[j + 1][k + 1] < dp[j][k] + 1 &&
              str[j] == str[i + 1 + k]) {
            dp[j + 1][k + 1] = dp[j][k] + 1;
            rev[j + 1][k + 1] = 0;
          }
        }
      if (ret < dp[n][m]) {
        ret = dp[n][m];
        string to = "";
        int tn = n;
        int tm = m;
        while (tn && tm) {
          if (rev[tn][tm] == 1)
            tn--;
          else if (rev[tn][tm] == 2)
            tm--;
          else {
            tn--;
            tm--;
            to = str[tn] + to;
          }
        }
        ans = to;
      }
    }
    // printf("%d ",ret);
    printf("%s\n", ans.c_str());
  }
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
char str[310];
int dp[310][310];
int rev[310][310];
int main() {
  while (1) {
    scanf("%s", str);
    if (str[0] == '#')
      return 0;
    int ret = 0;
    string ans = "";
    int len = strlen(str);
    for (int i = 0; str[i + 1]; i++) {
      int n = i + 1;
      int m = len - n;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++)
          dp[j][k] = -999999;
      dp[0][0] = 0;
      for (int j = 0; j <= n; j++)
        for (int k = 0; k <= m; k++) {
          if (j < n && dp[j + 1][k] < dp[j][k]) {
            dp[j + 1][k] = dp[j][k];
            rev[j + 1][k] = 1;
          }
          if (k < m && dp[j][k + 1] < dp[j][k]) {
            dp[j][k + 1] = dp[j][k];
            rev[j][k + 1] = 2;
          }
          if (j < n && k < m && dp[j + 1][k + 1] < dp[j][k] + 1 &&
              str[j] == str[i + 1 + k]) {
            dp[j + 1][k + 1] = dp[j][k] + 1;
            rev[j + 1][k + 1] = 0;
          }
        }
      if (ret < dp[n][m]) {
        ret = dp[n][m];
        string to = "";
        int tn = n;
        int tm = m;
        while (tn && tm) {
          if (rev[tn][tm] == 1)
            tn--;
          else if (rev[tn][tm] == 2)
            tm--;
          else {
            tn--;
            tm--;
            to = str[tn] + to;
          }
        }
        ans = to;
      }
    }
    // printf("%d ",ret);
    printf("%s\n", ans.c_str());
  }
}
Problem p01223 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }

int main(void) {

  int t, n, h[101], i, up = 0, down = 0;

  scanf("%d", &t);

  while (t--, scanf("%d", &n)) {
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = up = down = 0; i < n - 1; i++) {
      up = max(up, h[i + 1] - h[i]);
      down = min(down, h[i + 1] - h[i]);
    }
    printf("%d %d\n", up, -down);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }

int main(void) {

  int t, n, h[101], i, up = 0, down = 0;

  scanf("%d", &t);

  while (t--) {
    scanf("%d", &n);
    for (i = 0; i < n; i++)
      scanf("%d", &h[i]);
    for (i = up = down = 0; i < n - 1; i++) {
      up = max(up, h[i + 1] - h[i]);
      down = min(down, h[i + 1] - h[i]);
    }
    printf("%d %d\n", up, -down);
  }
  return 0;
}
Problem p01223 (C++)

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

using namespace std;

typedef pair<int, int> P;

P solve(int n, int h[]) {
  P answer(0, 0);
  for (int i = 0; i < n - 1; ++i) {
    if (h[i] < h[i + 1]) {
      answer.first = max(answer.first, h[i + 1] - h[i]);
    }
    if (h[i] > h[i + 1]) {
      answer.second = max(answer.second, h[i] - h[i + 1]);
    }
  }
  return answer;
}

int main() {
  int t, n;
  int h[100];
  P answer;
  cin >> t;
  for (int case_num = 0; case_num < t; ++t) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }
    answer = solve(n, h);
    cout << answer.first << ' ' << answer.second << endl;
  }
  return 0;
}

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

using namespace std;

typedef pair<int, int> P;

P solve(int n, int h[]) {
  P answer(0, 0);
  for (int i = 0; i < n - 1; ++i) {
    if (h[i] < h[i + 1]) {
      answer.first = max(answer.first, h[i + 1] - h[i]);
    }
    if (h[i] > h[i + 1]) {
      answer.second = max(answer.second, h[i] - h[i + 1]);
    }
  }
  return answer;
}

int main() {
  int t, n;
  int h[100];
  P answer;
  cin >> t;
  for (int case_num = 0; case_num < t; ++case_num) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> h[i];
    }
    answer = solve(n, h);
    cout << answer.first << ' ' << answer.second << endl;
  }
  return 0;
}
Problem p01224 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define r(i, n) for (int i = 1; i < n; i++)
using namespace std;
int main() {
  long long n, p, a, b;
  while (cin >> n, n) {
    long long s = 1;
    if (n == 1)
      s--;
    for (int i = 2; i < n / 2; i++)
      if (n % i == 0)
        s += i + n / i;
    if (n < s)
      cout << "abundant number" << endl;
    else if (n > s)
      cout << "deficient number" << endl;
    else
      cout << "perfect number" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define r(i, n) for (int i = 1; i < n; i++)
using namespace std;
int main() {
  long long n, p, a, b;
  while (cin >> n, n) {
    long long s = 1;
    if (n == 1)
      s--;
    for (int i = 2; i * i < n; i++)
      if (n % i == 0)
        s += i + n / i;
    if (n < s)
      cout << "abundant number" << endl;
    else if (n > s)
      cout << "deficient number" << endl;
    else
      cout << "perfect number" << endl;
  }
}
Problem p01224 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main() {
  long long int N, i, j, yaku;
  while (scanf("%d", &N), N) {
    yaku = 0;
    for (i = 1; i * i <= N; i++) {
      if (N % i == 0) {
        yaku += i + N / i;
      }
      if (N == i * i) {
        yaku -= i;
      }
      if (yaku > 2 * N)
        break;
    }
    // printf("%d\n",yaku);
    if (yaku > 2 * N) {
      puts("abundant number");
    } else if (yaku == 2 * N) {
      puts("perfect number");
    } else {
      puts("deficient number");
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  long long int N, i, j, yaku;
  while (scanf("%lld", &N)) {
    if (N == 0)
      break;
    yaku = 0;
    for (i = 1; i * i <= N; i++) {
      if (N % i == 0) {
        yaku += i + N / i;
      }
      if (N == i * i) {
        yaku -= i;
      }
      if (yaku > 2 * N)
        break;
    }
    // printf("%d\n",yaku);
    if (yaku > 2 * N) {
      puts("abundant number");
    } else if (yaku == 2 * N) {
      puts("perfect number");
    } else {
      puts("deficient number");
    }
  }
  return 0;
}
Problem p01224 (C++)

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

int main() {
  int n;

  while (cin >> n, n != 0) {
    int sum = 0;
    for (int i = 1; i <= n / 2; i++) {
      if (n % i == 0)
        sum += i;
    }
    if (sum < n)
      cout << "deficient number" << endl;
    if (sum == n)
      cout << "perfect number" << endl;
    if (sum > n)
      cout << "abundant number" << endl;
  }
  return 0;
}

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

int main() {
  int n;

  while (cin >> n, n != 0) {
    if (n == 1) {
      cout << "deficient number" << endl;
      continue;
    }
    int sum = 1;
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        if (n / i != i) {
          sum += (i + n / i);
        } else {
          sum += i;
        }
      }
    }
    if (sum < n)
      cout << "deficient number" << endl;
    if (sum == n)
      cout << "perfect number" << endl;
    if (sum > n)
      cout << "abundant number" << endl;
  }
  return 0;
}
Problem p01224 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#pragma warning(disable : 4996)
int n;
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    int ret = 0;
    for (int i = 1; i < n; i++) {
      if (n % i == 0)
        ret += i;
    }
    if (n > ret) {
      printf("deficient number\n");
    } else if (n == ret) {
      printf("perfect number\n");
    } else {
      printf("abundant number\n");
    }
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#pragma warning(disable : 4996)
int n;
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    int ret = n > 1 ? 1 : 0;
    for (int i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        if (i * i != n) {
          ret += i + n / i;
        } else {
          ret += i;
        }
      }
    }
    if (n > ret) {
      printf("deficient number\n");
    } else if (n == ret) {
      printf("perfect number\n");
    } else {
      printf("abundant number\n");
    }
  }
  return 0;
}
Problem p01224 (C++)

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

int main() {
  long long int n, sum;
  while (cin >> n, n) {
    int N = n;
    sum = 1;
    for (int i = 1; i <= n; i++) {
      if (n % i == 0) {
        int j = 1, temp = 1;
        while (n % i == 0 && i != 1) {
          n /= i;
          temp += (int)pow(i, j++);
        }
        //	  cout <<"temp"<< temp << endl;
        sum *= temp;
      }
    }
    sum -= N;
    //        cout << sum << endl;
    if (sum > N)
      cout << "abundant number" << endl;
    else if (sum < N)
      cout << "deficient number" << endl;
    else
      cout << "perfect number" << endl;
  }
  return 0;
}

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

int main() {
  int n, sum;
  while (cin >> n, n) {
    int N = n;
    sum = 1;
    for (int i = 1; i <= n; i++) {
      if (n % i == 0) {
        int j = 1, temp = 1;
        while (n % i == 0 && i != 1) {
          n /= i;
          temp += (int)pow(i, j++);
        }
        //	  cout <<"temp"<< temp << endl;
        sum *= temp;
      }
    }
    sum -= N;
    //        cout << sum << endl;
    if (sum > N)
      cout << "abundant number" << endl;
    else if (sum < N)
      cout << "deficient number" << endl;
    else
      cout << "perfect number" << endl;
  }
  return 0;
}
Problem p01224 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  long long a = 6;
  while (cin >> a && a != 0) {
    long long sum = 1, a2 = a;
    int i = 2;
    while (a != 1) {
      if (a % i == 0) {
        int j = 1;
        long long sum2 = 1;
        while (a % i == 0) {
          sum2 += (pow(i, j));
          j++;
          a /= i;
        }
        sum *= sum2;
      }
      i++;
    }
    sum -= a2;
    if (a2 == sum)
      cout << "perfect number" << endl;
    else if (a2 > sum)
      cout << "deficient number" << endl;
    else
      cout << "abundant number" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  long long a = 6;
  while (cin >> a && a != 0) {
    long long sum = 1, a2 = a;
    int i = 2;
    while (a != 1) {
      if (a % i == 0) {
        int j = 1;
        long long sum2 = 1;
        while (a % i == 0) {
          sum2 += (pow(i, j));
          j++;
          a /= i;
        }
        sum *= sum2;
      }
      if (i == 2)
        i++;
      else
        i += 2;
    }
    sum -= a2;
    if (a2 == sum)
      cout << "perfect number" << endl;
    else if (a2 > sum)
      cout << "deficient number" << endl;
    else
      cout << "abundant number" << endl;
  }
}
Problem p01224 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

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

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    int sum = 0;
    for (int i = 1; i <= n / 2; i++) {
      if (n % i == 0)
        sum += i;
    }
    if (sum == n)
      cout << "perfect number" << endl;
    else if (sum < n)
      cout << "deficient number" << endl;
    else if (sum > n)
      cout << "abundant number" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

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

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    int sum = 0;
    for (int i = 1; i <= sqrt(n); i++) {
      if (n % i == 0) {
        if (i != n)
          sum += i;
        if (i * i != n && i != 1)
          sum += n / i;
      }
    }
    if (sum == n)
      cout << "perfect number" << endl;
    else if (sum < n)
      cout << "deficient number" << endl;
    else if (sum > n)
      cout << "abundant number" << endl;
  }
  return 0;
}
Problem p01224 (C++)

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

using namespace std;

int main(void) {

  long long n;

  while (cin >> n, n) {

    if (n == 1) {
      cout << "deficient number" << endl;
      continue;
    }

    long long total = 1;
    long long a = 0;

    for (int i = 2; a != i && i < n; i++) {
      if (n % i == 0) {
        total += i;
        total += (n / i);
        a = n / i;
        if (a == i) {
          total -= i;
          break;
        }
      }
      if (total > n)
        break;
    }
    // cout << "a = " << a << endl;
    // cout << total << "    ";
    if (n == total)
      cout << "perfect number" << endl;
    if (n < total)
      cout << "abundant number" << endl;
    if (n > total)
      cout << "deficient number" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main(void) {

  long long n;

  while (cin >> n, n) {

    if (n == 1) {
      cout << "deficient number" << endl;
      continue;
    }

    long long total = 1;
    long long a = 0;

    for (int i = 2; a != i && i < n / 2; i++) {
      if (n % i == 0) {
        total += i;
        total += (n / i);
        a = n / i;
        if (a == i) {
          total -= i;
          break;
        }
      }
      if (total > n)
        break;
    }
    // cout << "a = " << a << endl;
    // cout << total << "    ";
    if (n == total)
      cout << "perfect number" << endl;
    if (n < total)
      cout << "abundant number" << endl;
    if (n > total)
      cout << "deficient number" << endl;
  }
  return 0;
}
Problem p01224 (C++)

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

int main() {
  int n;
  while (cin >> n) {
    if (!n)
      break;
    int count = 0;
    for (int i = 1; i <= n / 2; i++) {
      if (n % i == 0)
        count += i;
    }
    if (count == n)
      cout << "perfect number" << endl;
    else if (count > n)
      cout << "abundant number" << endl;
    else
      cout << "deficient number" << endl;
  }
  return 0;
}

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

int main() {
  int n;
  while (cin >> n) {
    if (!n)
      break;
    int count = 0;
    for (int i = 1; i * i < n; i++) {
      if (i == 1)
        count++;
      else if (n % i == 0) {
        count += (i + n / i);
      }
    }
    if (count == n)
      cout << "perfect number" << endl;
    else if (count > n)
      cout << "abundant number" << endl;
    else
      cout << "deficient number" << endl;
  }
  return 0;
}
Problem p01227 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MP make_pair
#define PB push_back
#define ALL(a) (a).begin(), (a).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
int main() {
  int times;
  cin >> times;
  REP(i, times) {
    int n, k;
    cin >> n >> k;
    int x[100100];
    REP(j, n) cin >> x[j];
    vector<int> v;
    REP(j, n - 1) { v.PB(x[j + 1] - x[j]); }
    sort(ALL(v));
    reverse(ALL(v));
    int btw = x[n - 1] - x[0];
    REP(j, k - 1) { btw -= v[j]; }
    cout << max(0, btw) << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define MP make_pair
#define PB push_back
#define ALL(a) (a).begin(), (a).end()
#define REP(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef pair<int, int> P;
int main() {
  int times;
  cin >> times;
  REP(i, times) {
    int n, k;
    cin >> n >> k;
    int x[100100];
    REP(j, n) cin >> x[j];
    vector<int> v;
    REP(j, n - 1) { v.PB(x[j + 1] - x[j]); }
    sort(ALL(v));
    reverse(ALL(v));
    int btw = x[n - 1] - x[0];
    REP(j, min(n - 1, k - 1)) { btw -= v[j]; }
    cout << max(0, btw) << endl;
  }
}
Problem p01227 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> dist;
    int pre;
    dist.push_back(0);
    REP(i, n) {
      int tmp;
      cin >> tmp;
      if (i)
        dist.push_back(tmp - pre);
      pre = tmp;
    }
    SORT(dist);
    REP(i, n - 1) dist[i + 1] += dist[i];
    cout << dist[n - k] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int main(void) {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> dist;
    int pre;
    dist.push_back(0);
    REP(i, n) {
      int tmp;
      cin >> tmp;
      if (i)
        dist.push_back(tmp - pre);
      pre = tmp;
    }
    SORT(dist);
    REP(i, n - 1) dist[i + 1] += dist[i];
    cout << dist[max(n - k, 0)] << endl;
  }
  return 0;
}
Problem p01227 (C++)

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

int main() {
  int t;
  cin >> t;
  for (int a = 0; a < t; a++) {
    int n, k, x[100000], sum;
    vector<int> vec;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      if (i)
        vec.push_back(x[i] - x[i - 1]);
    }
    sum = x[n - 1] - x[0];
    sort(vec.begin(), vec.end(), greater<int>());
    for (int i = 0; i < k - 1; i++) {
      sum -= vec[i];
    }
    cout << sum << endl;
  }
  return 0;
}

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

int main() {
  int t;
  cin >> t;
  for (int a = 0; a < t; a++) {
    int n, k, x[100000], sum;
    vector<int> vec;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
      if (i)
        vec.push_back(x[i] - x[i - 1]);
    }
    sum = x[n - 1] - x[0];
    sort(vec.begin(), vec.end(), greater<int>());
    for (int i = 0; i < min(n, k) - 1; i++) {
      sum -= vec[i];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01227 (C++)

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

int main() {
  int set, i, j, p, q, n, k, input[10010], st, en, sa[10010], cont;

  scanf("%d", &set);
  for (q = 0; q < set; q++) {
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i++) {
      scanf("%d", &input[i]);
    }
    st = input[0];
    en = input[n - 1];
    cont = en - st;
    for (i = 1; i < n; i++) {
      sa[i - 1] = input[i] - input[i - 1];
    }
    sort(sa, sa + n - 1, greater<int>());
    for (i = 0; i < k - 1; i++) {
      cont -= sa[i];
    }
    if (cont < 0) {
      cont = 0;
    }
    printf("%d\n", cont);
  }
}

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

int main() {
  int set, i, j, p, q, n, k, input[100100], st, en, sa[100100], cont;

  scanf("%d", &set);
  for (q = 0; q < set; q++) {
    scanf("%d%d", &n, &k);
    for (i = 0; i < n; i++) {
      scanf("%d", &input[i]);
    }
    st = input[0];
    en = input[n - 1];
    cont = en - st;
    for (i = 1; i < n; i++) {
      sa[i - 1] = input[i] - input[i - 1];
    }
    sort(sa, sa + n - 1, greater<int>());
    for (i = 0; i < k - 1; i++) {
      cont -= sa[i];
    }
    if (cont < 0) {
      cont = 0;
    }
    printf("%d\n", cont);
  }
}
Problem p01227 (C++)

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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; t++) {
    int n, k;
    cin >> n >> k;
    int x[n], gap[n - 1];
    for (int j = 0; j < n; j++)
      cin >> x[j];
    for (int j = 0; j < n - 1; j++)
      gap[j] = x[j + 1] - x[j];
    sort(&gap[0], &gap[n - 1]);
    int sum = 0;
    if ((n - 1) - (k - 1) > 0) {
      for (int j = 0; j < (n - 1) - (k - 1); j++)
        sum += gap[j];
    }
    cout << sum << endl;
  }
  return 0;
}

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

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, k;
    cin >> n >> k;
    int x[n], gap[n - 1];
    for (int j = 0; j < n; j++)
      cin >> x[j];
    for (int j = 0; j < n - 1; j++)
      gap[j] = x[j + 1] - x[j];
    sort(&gap[0], &gap[n - 1]);
    int sum = 0;
    if ((n - 1) - (k - 1) > 0) {
      for (int j = 0; j < (n - 1) - (k - 1); j++)
        sum += gap[j];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01227 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;

/* typedef */

/* global variables */

int xs[MAX_N], dxs[MAX_N];

/* subroutines */

/* main */

int main() {
  int tn;
  cin >> tn;

  while (tn--) {
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
      cin >> xs[i];
    sort(xs, xs + n);

    for (int i = 0; i < n - 1; i++)
      dxs[i] = xs[i + 1] - xs[i];
    sort(dxs, dxs + n - 1);

    int sum = 0;
    for (int i = 0; i < n - k; i++)
      sum += dxs[i];

    cout << sum << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

/* global variables */

int xs[MAX_N], dxs[MAX_N];

/* subroutines */

/* main */

int main() {
  int tn;
  cin >> tn;

  while (tn--) {
    int n, k;
    cin >> n >> k;

    for (int i = 0; i < n; i++)
      cin >> xs[i];
    sort(xs, xs + n);

    for (int i = 0; i < n - 1; i++)
      dxs[i] = xs[i + 1] - xs[i];
    sort(dxs, dxs + n - 1);

    int sum = 0;
    for (int i = 0; i < n - k; i++)
      sum += dxs[i];

    cout << sum << endl;
  }

  return 0;
}
Problem p01227 (C++)

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

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

using namespace std;

typedef vector<int> vi;

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    rep(i, n) cin >> x[i];
    sort(all(x));
    vi d(n - 1);
    int s = 0;
    rep(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      s += d[i];
    }
    sort(rall(d));

    rep(i, k - 1) { s -= d[i]; }
    s = max(0, s);
    cout << s << endl;
  }
}

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

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()

using namespace std;

typedef vector<int> vi;

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n, k;
    cin >> n >> k;
    vi x(n);
    rep(i, n) cin >> x[i];
    sort(all(x));
    vi d(n - 1);
    int s = 0;
    rep(i, n - 1) {
      d[i] = x[i + 1] - x[i];
      s += d[i];
    }
    sort(rall(d));

    int end = min(k - 1, n - 1);

    rep(i, end) { s -= d[i]; }
    s = max(0, s);
    cout << s << endl;
  }
}
Problem p01227 (C++)

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

int main() {
  int t;
  std::cin >> t;
  std::vector<int> space;
  space.reserve(100000);

  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    if (k >= n) {
      // テゥツ?ゥテ・ツスツ禿」ツ?ォテ、ツクツ?・ツ崢榲・ツ?・テ・ツ環崚」ツ?凖」ツつ?
      std::cin >> n;
      std::cout << 0 << std::endl;
      continue;
    }

    space.resize(n - 1);
    int before_pos, road_size, first_pos;
    std::cin >> before_pos;
    first_pos = before_pos;
    for (int j = 1; j < n; ++j) {
      int new_pos;
      std::cin >> new_pos;

      if (j == n - 1)
        road_size = new_pos;

      space[j - 1] = new_pos - before_pos;
      before_pos = new_pos;
    }

    std::sort(space.begin(), space.end(), std::greater<int>());

    std::cout << road_size - first_pos -
                     std::accumulate(space.begin(), space.begin() + k - 1, 0)
              << std::endl;

    space.clear();
  }
}

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

int main() {
  int t;
  std::cin >> t;
  std::vector<int> space;
  space.reserve(100000);

  for (int i = 0; i < t; ++i) {
    int n, k;
    std::cin >> n >> k;
    if (k >= n) {
      // テゥツ?ゥテ・ツスツ禿」ツ?ォテ、ツクツ?・ツ崢榲・ツ?・テ・ツ環崚」ツ?凖」ツつ?
      for (int j = 0; j < n; ++j)
        std::cin >> k;
      std::cout << 0 << std::endl;
      continue;
    }

    space.resize(n - 1);
    int before_pos, road_size, first_pos;
    std::cin >> before_pos;
    first_pos = before_pos;
    for (int j = 1; j < n; ++j) {
      int new_pos;
      std::cin >> new_pos;

      if (j == n - 1)
        road_size = new_pos;

      space[j - 1] = new_pos - before_pos;
      before_pos = new_pos;
    }

    std::sort(space.begin(), space.end(), std::greater<int>());

    std::cout << road_size - first_pos -
                     std::accumulate(space.begin(), space.begin() + k - 1, 0)
              << std::endl;

    space.clear();
  }
}
Problem p01229 (C++)

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

int n, maxi;
int Ea[81], dp[81][321][321];

int rec(int idx, int prev, int now) {
  if (idx == n - 1)
    return now;
  if (dp[idx][prev][now])
    return dp[idx][prev][now];
  int res = 0;
  res = max(res, rec(idx + 1, now, Ea[idx + 1]));
  for (int i = 0; i < n; i++) {
    if (now > 0)
      res = max(res, rec(idx + 1, now - 1, prev + Ea[idx + 1]));
  }
  return dp[idx][prev][now] = res;
}

int main() {
  int N;
  cin >> N;
  while (N--) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> Ea[i];
    cout << ((n == 1) ? Ea[0] : rec(1, Ea[0], Ea[1])) << endl;
  }
}

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

int n, maxi;
int Ea[81], dp[81][321][321];

int rec(int idx, int prev, int now) {
  if (idx == n - 1)
    return now;
  if (dp[idx][prev][now])
    return dp[idx][prev][now];
  int res = 0;
  res = max(res, rec(idx + 1, now, Ea[idx + 1]));
  if (now > 0)
    res = max(res, rec(idx + 1, now - 1, prev + Ea[idx + 1]));
  return dp[idx][prev][now] = res;
}

int main() {
  int N;
  cin >> N;
  while (N--) {
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> Ea[i];
    cout << ((n == 1) ? Ea[0] : rec(1, Ea[0], Ea[1])) << endl;
  }
}
Problem p01229 (C++)

Buggy submission (status: Memory Limit Exceeded):
// 15
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int p[400][400][81];

int dfs(vector<int> v) {
  if (v.size() <= 2) {
    return v.back();
  } else {
    if (p[v[0]][v[1]][v.size()]++) {
      return 0;
    } else {
      int n = dfs(vector<int>(v.begin() + 1, v.end()));
      if (v[1]) {
        v[2] += v[0];
        v[1]--;
        n = max(n, dfs(vector<int>(v.begin() + 1, v.end())));
      }
      return n;
    }
  }
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    int l;
    cin >> l;
    vector<int> v(l);
    for (int i = 0; i < l; i++) {
      cin >> v[i];
    }
    fill(p[0][0], p[400][0], false);
    cout << dfs(v) << endl;
  }
  return 0;
}

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

using namespace std;

bool p[400][400][81];

int dfs(vector<int> v) {
  if (v.size() <= 2) {
    return v.back();
  } else {
    if (p[v[0]][v[1]][v.size()]++) {
      return 0;
    } else {
      int n = dfs(vector<int>(v.begin() + 1, v.end()));
      if (v[1]) {
        v[2] += v[0];
        v[1]--;
        n = max(n, dfs(vector<int>(v.begin() + 1, v.end())));
      }
      return n;
    }
  }
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    int l;
    cin >> l;
    vector<int> v(l);
    for (int i = 0; i < l; i++) {
      cin >> v[i];
    }
    fill(p[0][0], p[400][0], false);
    cout << dfs(v) << endl;
  }
  return 0;
}
Problem p01244 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

using ld = long double;

vector<int> primes;
void hurui(const int amax) {
  static bool flag = false;
  if (flag)
    return;
  vector<int> sos;
  sos = vector<int>(amax + 1, true);
  sos[0] = false;
  sos[1] = false;
  for (int i = 2; i <= amax; ++i) {
    if (sos[i]) {
      for (int j = 2 * i; j <= amax; j += i)
        sos[j] = false;
    }
  }
  for (int i = 0; i <= amax; ++i) {
    if (sos[i]) {
      primes.push_back(i);
    }
  }
  flag = true;
}

long long int K;

ld solve(long long int now_num, int pr_id, int use_max, ld now_val) {
  if (use_max == 0) {
    return now_val;
  }
  if (primes.size() == pr_id) {
    return now_val;
  } else {
    ld ans = now_val;
    int now_use = 0;
    long long int pr_sum = 1;
    long long int div_sum = 1;
    ld next_val = now_val * pr_sum / div_sum;
    long long int next_num = now_num;
    while (next_num <= K) {
      ans = max(ans, solve(next_num, pr_id + 1, now_use, next_val));

      now_use++;
      div_sum *= primes[pr_id];
      pr_sum += div_sum;
      next_val = now_val * pr_sum / div_sum;
      next_num *= primes[pr_id];
    }

    return ans;
  }
}
int main() {
  hurui(1e3);
  int case_id = 0;
  while (true) {
    cin >> K;
    ld ans = solve(1, 0, 1000, 1);
    cout << setprecision(10) << fixed << ans << endl;
  }

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;

using ld = long double;

vector<int> primes;
void hurui(const int amax) {
  static bool flag = false;
  if (flag)
    return;
  vector<int> sos;
  sos = vector<int>(amax + 1, true);
  sos[0] = false;
  sos[1] = false;
  for (int i = 2; i <= amax; ++i) {
    if (sos[i]) {
      for (int j = 2 * i; j <= amax; j += i)
        sos[j] = false;
    }
  }
  for (int i = 0; i <= amax; ++i) {
    if (sos[i]) {
      primes.push_back(i);
    }
  }
  flag = true;
}

long long int K;

ld solve(long long int now_num, int pr_id, int use_max, ld now_val) {
  if (use_max == 0) {
    return now_val;
  }
  if (primes.size() == pr_id) {
    return now_val;
  } else {
    ld ans = now_val;
    int now_use = 0;
    long long int pr_sum = 1;
    long long int div_sum = 1;
    ld next_val = now_val * pr_sum / div_sum;
    long long int next_num = now_num;
    while (next_num <= K) {
      ans = max(ans, solve(next_num, pr_id + 1, now_use, next_val));

      now_use++;
      div_sum *= primes[pr_id];
      pr_sum += div_sum;
      next_val = now_val * pr_sum / div_sum;
      next_num *= primes[pr_id];
    }

    return ans;
  }
}
int main() {
  hurui(1e3);
  int case_id = 0;
  while (true) {
    cin >> K;
    if (!K)
      break;
    ld ans = solve(1, 0, 1000, 1);
    cout << setprecision(10) << fixed << ans << endl;
  }

  return 0;
}
Problem p01248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX - 1;

int memo[1111];

int func(int pos, const int &n, const int &width, const vector<int> &a) {
  if (memo[pos] != -1)
    return memo[pos];
  if (pos >= n)
    return 0;
  int w = 0;
  int res = INF;
  int p;
  int pp = -1;
  for (p = pos; p < n; p++) {
    w += a[p];
    int tmp = func(p + 1, n, width, a) + abs(width - w);
    if (res > tmp) {
      pp = p;
      res = tmp;
    }
  }
  if (pp + 1 == n)
    return max(0, w - width);
  memo[pos] = res;
  return res;
}

int main(void) {
  int n, w;
  int Case = 1;
  while (cin >> n >> w, n | w) {
    for (int i = 0; i < 1111; i++)
      memo[i] = -1;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cout << "Case " << Case++ << ": " << func(0, n, w, a) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX - 1;

int memo[1111];

int func(int pos, const int &n, const int &width, const vector<int> &a) {
  if (memo[pos] != -1)
    return memo[pos];
  if (pos >= n)
    return 0;
  int w = 0;
  int res = INF;
  int p;
  int pp = -1;
  for (p = pos; p < n; p++) {
    w += a[p];
    int tmp = func(p + 1, n, width, a) + abs(width - w);
    if (res > tmp) {
      pp = p;
      res = tmp;
    }
  }
  if (pp + 1 == n) {
    memo[pos] = max(0, w - width);
    return memo[pos];
  }
  memo[pos] = res;
  return res;
}

int main(void) {
  int n, w;
  int Case = 1;
  while (cin >> n >> w, n | w) {
    for (int i = 0; i < 1111; i++)
      memo[i] = -1;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cout << "Case " << Case++ << ": " << func(0, n, w, a) << endl;
  }
  return 0;
}
Problem p01250 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define PI 3.14159265358979323846264
int main() {
  while (true) {
    long double p;
    cin >> p;
    for (long double i = 0; i < 20000000; i += 1) {
      long double q1 = (PI - p) * i;
      long double q2 = (PI + p) * i;
      int r1 = q1, r2 = q2;
      if (r1 < r2) {
        cout << r2 << '/' << (int)i << endl;
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#define PI 3.14159265358979323846264
int main() {
  while (true) {
    long double p;
    cin >> p;
    if (p == 0)
      break;
    for (long double i = 0; i < 20000000; i += 1) {
      long double q1 = (PI - p) * i;
      long double q2 = (PI + p) * i;
      int r1 = q1, r2 = q2;
      if (r1 < r2) {
        cout << r2 << '/' << (int)i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p01250 (C++)

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

const int MAX = pow(2, 30);

int main() {
  while (true) {
    double r;
    cin >> r;
    if (r == 0) {
      break;
    }
    for (int i = 2; i < MAX; i++) {
      for (int j = 1; j < MAX; j++) {
        double diff = (double)i / j - M_PI;
        if (-r <= diff && diff <= r) {
          cout << i << "/" << j << endl;
          goto end;
        } else if (diff < -r) {
          break;
        }
      }
    }
  end:;
  }
}

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

const int MAX = pow(2, 30);

int main() {
  while (true) {
    double r;
    cin >> r;
    if (r == 0) {
      break;
    }
    for (int i = 2; i < MAX; i++) {
      int j = (int)floor(i / (M_PI + r));
      for (; j < MAX; j++) {
        double diff = (double)i / j - M_PI;
        if (-r <= diff && diff <= r) {
          cout << i << "/" << j << endl;
          goto end;
        } else if (diff < -r) {
          break;
        }
      }
    }
  end:;
  }
}
Problem p01253 (C++)

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

int main() {
  int N, T;
  while (cin >> N >> T, (N || T)) {
    string s;
    cin >> s;

    double dp[1000][2] = {0};
    dp[0][0] = 1.0;

    for (int i = 1; i <= T; i++) {
      for (int j = 0; j < N; j++)
        dp[j][i % 2] = 0.0;

      for (int j = 0; j < N; j++)
        for (int k = 1; k <= 6; k++)
          dp[(j + k) % N][i % 2] += dp[j][(i + 1) % 2] / 6.0;
    }

    double res = 0;
    for (int i = 0; i < N; i++) {
      double sum = 0;
      for (int j = 0; j < N; j++)
        if (s[(i + j) % N] == 'R')
          sum += dp[j][T % 2];
      res = max(res, sum);
    }

    cout.setf(ios::fixed);
    cout.precision(10);

    cout << res << endl;
  }
}

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

int main() {
  int N, T;
  while (cin >> N >> T, (N || T)) {
    string s;
    cin >> s;

    double dp[2005][2] = {0};
    dp[0][0] = 1.0;

    for (int i = 1; i <= T; i++) {
      for (int j = 0; j < N; j++)
        dp[j][i % 2] = 0.0;

      for (int j = 0; j < N; j++)
        for (int k = 1; k <= 6; k++)
          dp[(j + k) % N][i % 2] += dp[j][(i + 1) % 2] / 6.0;
    }

    double res = 0;
    for (int i = 0; i < N; i++) {
      double sum = 0;
      for (int j = 0; j < N; j++)
        if (s[(i + j) % N] == 'R')
          sum += dp[j][T % 2];
      res = max(res, sum);
    }

    cout.setf(ios::fixed);
    cout.precision(10);

    cout << res << endl;
  }
}
Problem p01254 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF (1 << 21)

struct NODE {
  int to, cost, flag;
  NODE(int to, int cost, int flag) : to(to), cost(cost), flag(flag){};
};
bool operator<(const NODE &a, const NODE &b) {
  if (a.cost != b.cost)
    return a.cost > b.cost;
  else
    return a.flag > b.flag;
}
int main() {
  int n, m, s, t;
  while (cin >> n && n) {
    cin >> s >> t >> m;
    s--, t--;
    vector<int> v[n];
    vector<pair<int, int>> e[n];
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      v[a].push_back(b);
      e[b].push_back(make_pair(a, i));
    }
    bool memo[2][1000] = {0};
    priority_queue<NODE> Q;
    Q.push(NODE(s, 0, 0));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (memo[!!q.flag][q.to])
        memo[!!q.flag][q.to] = true;
      else
        memo[!!q.flag][q.to] = true;
      if (q.to == t) {
        cout << q.cost << " " << q.flag << endl;
        break;
      }
      for (int i = 0; i < v[q.to].size(); i++)
        Q.push(NODE(v[q.to][i], q.cost + 1, q.flag));
      if (!q.flag)
        for (int i = 0; i < e[q.to].size(); i++)
          Q.push(NODE(e[q.to][i].first, q.cost + 1, e[q.to][i].second + 1));
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF (1 << 21)

struct NODE {
  int to, cost, flag;
  NODE(int to, int cost, int flag) : to(to), cost(cost), flag(flag){};
};
bool operator<(const NODE &a, const NODE &b) {
  if (a.cost != b.cost)
    return a.cost > b.cost;
  else
    return a.flag > b.flag;
}
int main() {
  int n, m, s, t;
  while (cin >> n && n) {
    cin >> s >> t >> m;
    s--, t--;
    vector<int> v[n];
    vector<pair<int, int>> e[n];
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      v[a].push_back(b);
      e[b].push_back(make_pair(a, i));
    }
    bool memo[2][1000] = {0};
    priority_queue<NODE> Q;
    Q.push(NODE(s, 0, 0));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (memo[!!q.flag][q.to])
        continue;
      else
        memo[!!q.flag][q.to] = true;
      if (q.to == t) {
        cout << q.cost << " " << q.flag << endl;
        break;
      }
      for (int i = 0; i < v[q.to].size(); i++)
        Q.push(NODE(v[q.to][i], q.cost + 1, q.flag));
      if (!q.flag)
        for (int i = 0; i < e[q.to].size(); i++)
          Q.push(NODE(e[q.to][i].first, q.cost + 1, e[q.to][i].second + 1));
    }
  }
}
Problem p01257 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, cost[100001], op[1023];
void dfs(int sum) {
  rep(i, (1 << n) - 1) if (cost[sum + op[i]] > cost[sum] + 1)
      cost[sum + op[i]] = cost[sum] + 1,
                 dfs(sum + op[i]);
}
int main() {
  while (cin >> n >> m, n) {
    int coin[10], s = 0;
    rep(i, n) cin >> coin[i];
    rep(i, 1 << n) if (i > 0) {
      s = 0;
      rep(j, n) if (i & 1 << j) s += coin[j];
      op[i - 1] = s;
    }
    sort(op, op + (1 << n) - 1, greater<int>());
    rep(i, m + 1) cost[i] = 1 << 20;
    cost[0] = 0;
    dfs(0);
    cout << cost[m] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, cost[100001], op[1023];
void dfs(int sum) {
  rep(i,
      (1 << n) - 1) if (sum + op[i] <= m && cost[sum + op[i]] > cost[sum] + 1)
      cost[sum + op[i]] = cost[sum] + 1,
                 dfs(sum + op[i]);
}
int main() {
  while (cin >> n >> m, n) {
    int coin[10], s = 0;
    rep(i, n) cin >> coin[i];
    rep(i, 1 << n) if (i > 0) {
      s = 0;
      rep(j, n) if (i & 1 << j) s += coin[j];
      op[i - 1] = s;
    }
    sort(op, op + (1 << n) - 1, greater<int>());
    rep(i, m + 1) cost[i] = 1 << 20;
    cost[0] = 0;
    dfs(0);
    cout << cost[m] << endl;
  }
  return 0;
}
Problem p01257 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000;
using namespace std;
int n, m, a[11];
vector<int> coin;
int used[11];
void mk_coin(int sum, int k) {
  for (int i = k; i < n; i++) {
    if (used[i] == 1)
      continue;
    used[i] = 1;
    coin.push_back(sum + a[i]);
    mk_coin(sum + a[i], i + 1);
    used[i] = 0;
  }
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    coin.clear();
    for (int i = 0; i < n; i++)
      cin >> a[i];
    mk_coin(0, 0);

    int dp[100010] = {};
    for (int i = 1; i <= m; i++)
      dp[i] = INF;
    for (int i = 0; i < coin.size(); i++)
      for (int j = 0; j <= m; j++)
        dp[j + coin[i]] = min(dp[j] + 1, dp[j + coin[i]]);
    cout << dp[m] << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define INF 1000000000;
using namespace std;
int n, m, a[11];
vector<int> coin;
int used[11];
void mk_coin(int sum, int k) {
  for (int i = k; i < n; i++) {
    if (used[i] == 1)
      continue;
    used[i] = 1;
    coin.push_back(sum + a[i]);
    mk_coin(sum + a[i], i + 1);
    used[i] = 0;
  }
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    coin.clear();
    for (int i = 0; i < n; i++)
      cin >> a[i];
    mk_coin(0, 0);

    int dp[100010] = {};
    for (int i = 1; i <= m; i++)
      dp[i] = INF;
    for (int i = 0; i < coin.size(); i++)
      for (int j = 0; j <= m - coin[i]; j++)
        dp[j + coin[i]] = min(dp[j] + 1, dp[j + coin[i]]);
    cout << dp[m] << endl;
  }
  return 0;
}
Problem p01261 (C++)

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

using namespace std;
typedef long long ll;

ll pascal[61][61] = {{0ll}};

int main() {
  for (int i = 0; i <= 60; ++i) {
    pascal[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
    }
  }
  while (true) {
    int n, x;
    ll m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    --m;
    if (m == 0) {
      cout << string(n, '0') << endl;
      continue;
    }
    for (x = 0; x < n; ++x) {
      if (m >= pascal[n][x]) {
        m -= pascal[n][x];
      } else {
        break;
      }
    }
    string answer(n, '0');
    for (int y = x - 1, z = n; y > 0; --y) {
      for (int w = y; w < z; ++w) {
        if (m >= pascal[w - 1][y - 1]) {
          m -= pascal[w - 1][y - 1];
        } else {
          answer[w] = '1';
          z = w;
          break;
        }
      }
    }
    answer[m] = '1';
    reverse(answer.begin(), answer.end());
    cout << answer << endl;
  }
  return 0;
}

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

using namespace std;
typedef long long ll;

ll pascal[61][61] = {{0ll}};

int main() {
  for (int i = 0; i <= 60; ++i) {
    pascal[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
    }
  }
  while (true) {
    int n, x;
    ll m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    --m;
    if (m == 0) {
      cout << string(n, '0') << endl;
      continue;
    }
    for (x = 0; x < n; ++x) {
      if (m >= pascal[n][x]) {
        m -= pascal[n][x];
      } else {
        break;
      }
    }
    string answer(n, '0');
    for (int y = x - 1, z = n; y > 0; --y) {
      for (int w = y; w < z; ++w) {
        if (m >= pascal[w][y]) {
          m -= pascal[w][y];
        } else {
          answer[w] = '1';
          z = w;
          break;
        }
      }
    }
    answer[m] = '1';
    reverse(answer.begin(), answer.end());
    cout << answer << endl;
  }
  return 0;
}