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

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

using namespace std;

struct H {
  vector<int> dat;
  void insert(int x) {
    int i = (int)dat.size();
    dat.push_back(x);
    while (i > 0) {
      auto p = (i - 1) / 2;
      if (dat[p] < dat[i]) {
        swap(dat[p], dat[i]);
        i = p;
      } else {
        break;
      }
    }
  }
  int front() { return dat[0]; }
  void remove() {
    swap(dat[0], dat[dat.size() - 1]);
    dat.pop_back();
    int len = (int)dat.size();
    int i = 0;
    while (i * 2 + 1 < len) {
      if (i * 2 + 2 < len) {
        int j = (dat[i * 2 + 1] > dat[i * 2 + 2] ? i * 2 + 1 : i * 2 + 2);
        if (dat[i] < dat[j]) {
          swap(dat[i], dat[j]);
          i = j;
        } else {
          break;
        }
      } else {
        if (dat[i] < dat[i * 2 + 1]) {
          swap(dat[i], dat[i * 2 + 1]);
          i = i * 2 + 1;
        }
      }
    }
  }
};

int main() {
  H h;
  while (1) {
    string s;
    cin >> s;
    if (s[0] == 'e') {
      if (s[1] == 'n')
        break;
      cout << h.front() << endl;
      h.remove();
    } else {
      int x;
      cin >> x;
      h.insert(x);
    }
  }
}


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

using namespace std;

struct H {
  vector<int> dat;
  void insert(int x) {
    int i = (int)dat.size();
    dat.push_back(x);
    while (i > 0) {
      auto p = (i - 1) / 2;
      if (dat[p] < dat[i]) {
        swap(dat[p], dat[i]);
        i = p;
      } else {
        break;
      }
    }
  }
  int front() { return dat[0]; }
  void remove() {
    swap(dat[0], dat[dat.size() - 1]);
    dat.pop_back();
    int len = (int)dat.size();
    int i = 0;
    while (i * 2 + 1 < len) {
      if (i * 2 + 2 < len) {
        int j = (dat[i * 2 + 1] > dat[i * 2 + 2] ? i * 2 + 1 : i * 2 + 2);
        if (dat[i] < dat[j]) {
          swap(dat[i], dat[j]);
          i = j;
        } else {
          break;
        }
      } else {
        if (dat[i] < dat[i * 2 + 1]) {
          swap(dat[i], dat[i * 2 + 1]);
          i = i * 2 + 1;
        } else {
          break;
        }
      }
    }
  }
};

int main() {
  H h;
  while (1) {
    string s;
    cin >> s;
    if (s[0] == 'e') {
      if (s[1] == 'n')
        break;
      cout << h.front() << endl;
      h.remove();
    } else {
      int x;
      cin >> x;
      h.insert(x);
    }
  }
}
Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int A[202020];
int H;
// 1-indexed
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int largest;
  if (l <= H && A[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if (r <= H && A[r] > A[largest]) {
    largest = r;
  }
  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(largest);
  }
}
void insert(int x) {
  int i = ++H;
  A[i] = x;
  // 親より大きかったら入れ替える
  while (i > 1 && A[i] > A[i / 2]) {
    swap(A[i], A[i / 2]);
    i /= 2;
  }
}
int extract() {
  int ret = A[1];
  A[1] = A[H--];
  maxHeapify(1);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  while (cin >> s && s != "end") {
    if (s == "insert") {
      int x;
      cin >> x;
      insert(x);
    } else {
      cout << extract() << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int A[2020200];
int H;
// 1-indexed
void maxHeapify(int i) {
  int l = i * 2;
  int r = i * 2 + 1;
  int largest;
  if (l <= H && A[l] > A[i]) {
    largest = l;
  } else {
    largest = i;
  }
  if (r <= H && A[r] > A[largest]) {
    largest = r;
  }
  if (largest != i) {
    swap(A[i], A[largest]);
    maxHeapify(largest);
  }
}
void insert(int x) {
  int i = ++H;
  A[i] = x;
  // 親より大きかったら入れ替える
  while (i > 1 && A[i] > A[i / 2]) {
    swap(A[i], A[i / 2]);
    i /= 2;
  }
}
int extract() {
  int ret = A[1];
  A[1] = A[H--];
  maxHeapify(1);
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  while (cin >> s && s != "end") {
    if (s == "insert") {
      int x;
      cin >> x;
      insert(x);
    } else {
      cout << extract() << endl;
    }
  }
}
Problem p02289 (C++)

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

using namespace std;

int main() {
  priority_queue<int> pq;
  string ope;
  int num;
  cin >> ope;
  while (ope != "end") {
    if (ope == "extract") {
      cout << pq.top() << endl;
      pq.pop();
    } else if (ope == "insert") {
      cin >> num;
      pq.push(num);
    }
    cin >> ope >> num;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>

using namespace std;

int main() {
  priority_queue<int> pq;
  string ope;
  int num;
  cin >> ope;
  while (ope != "end") {
    if (ope == "extract") {
      cout << pq.top() << endl;
      pq.pop();
    } else if (ope == "insert") {
      cin >> num;
      pq.push(num);
    }
    cin >> ope;
  }
  return 0;
}
Problem p02300 (C++)

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

class Point {
public:
  int x, y;
  Point(int a = 0, int b = 0) {
    int x = a;
    int y = b;
  };
  bool operator<(const Point &p) const {
    return y < p.y || (y == p.y && x < p.x);
  }
};

inline bool direct(Point &base, Point &a, Point &b) {
  return (a.x - base.x) * (b.y - base.y) - (a.y - base.y) * (b.x - base.x) < 0;
}

int main() {
  int n, i;
  int k = 2;
  scanf("%d", &n);
  vector<Point> P(n), H(n);
  for (i = 1; i <= n; ++i) {
    scanf("%d%d", &P[i - 1].x, &P[i - 1].y);
  }
  sort(P.begin(), P.end());
  H[0] = P[0];
  H[1] = P[1];
  for (i = 0; i < n - 2; ++i) {
    while (k >= 2 && direct(H[k - 2], H[k - 1], P[i + 2]))
      k--;
    H[k++] = P[i + 2];
  }
  H[k++] = P[n - 2];
  for (i = n; i > 2; i--) {
    while (k >= 2 && direct(H[k - 2], H[k - 1], P[i - 3]))
      k--;
    H[k++] = P[i - 3];
  }
  printf("%d\n", k - 1);
  for (i = 0; i < k - 1; ++i) {
    printf("%d %d\n", H[i].x, H[i].y);
  }
  return 0;
}

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

class Point {
public:
  int x, y;
  Point(int a = 0, int b = 0) {
    int x = a;
    int y = b;
  };
  bool operator<(const Point &p) const {
    return y < p.y || (y == p.y && x < p.x);
  }
};

inline bool direct(Point &base, Point &a, Point &b) {
  return (a.x - base.x) * (b.y - base.y) - (a.y - base.y) * (b.x - base.x) < 0;
}

int main() {
  int n, i;
  int k = 2;
  scanf("%d", &n);
  vector<Point> P(n), H(2 * n);
  for (i = 1; i <= n; ++i) {
    scanf("%d%d", &P[i - 1].x, &P[i - 1].y);
  }
  sort(P.begin(), P.end());
  H[0] = P[0];
  H[1] = P[1];
  for (i = 0; i < n - 2; ++i) {
    while (k >= 2 && direct(H[k - 2], H[k - 1], P[i + 2]))
      k--;
    H[k++] = P[i + 2];
  }
  H[k++] = P[n - 2];
  for (i = n; i > 2; i--) {
    while (k >= 2 && direct(H[k - 2], H[k - 1], P[i - 3]))
      k--;
    H[k++] = P[i - 3];
  }
  printf("%d\n", k - 1);
  for (i = 0; i < k - 1; ++i) {
    printf("%d %d\n", H[i].x, H[i].y);
  }
  return 0;
}
Problem p02300 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
#define push push_back
#define sz stack.size()
#define m make_pair
#define N_MAX 10001
#define x second
#define y first
using namespace std;
typedef complex<int> point;
typedef pair<int, int> P;

bool ccw(point p0, point p1, point p2) {
  point temp;
  p1 -= p0;
  p2 -= p0;
  temp = p1;
  p1 *= conj(temp);
  p2 *= conj(temp);
  if (abs(temp) == 0)
    return true;
  p1 /= abs(temp);
  if (p2.imag() > 0)
    return false;
  if (p2.imag() < 0)
    return true;
  p2 /= abs(temp);
  return true;
}

int main() {
  int n;
  P p[N_MAX];
  vector<P> stack;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].x >> p[i].y;
  sort(p, p + n);
  stack.push(p[0]);
  stack.push(p[1]);
  for (int i = 2; i < n; i++) {
    point c = point(p[i].x, p[i].y);
    while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y),
                           point(stack[sz - 1].x, stack[sz - 1].y), c))
      stack.pop_back();
    stack.push(p[i]);
  }
  for (int i = n - 2; i >= 0; i--) {
    point c = point(p[i].x, p[i].y);
    while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y),
                           point(stack[sz - 1].x, stack[sz - 1].y), c))
      stack.pop_back();
    stack.push(p[i]);
  }
  stack.erase(stack.begin());
  cout << stack.size() << endl;
  for (int i = sz - 1; i >= 0; i--)
    cout << stack[i].x << ' ' << stack[i].y << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <iostream>
#include <vector>
#define push push_back
#define sz stack.size()
#define m make_pair
#define N_MAX 100001
#define x second
#define y first
using namespace std;
typedef complex<int> point;
typedef pair<int, int> P;

bool ccw(point p0, point p1, point p2) {
  point temp;
  p1 -= p0;
  p2 -= p0;
  temp = p1;
  p1 *= conj(temp);
  p2 *= conj(temp);
  if (abs(temp) == 0)
    return true;
  p1 /= abs(temp);
  if (p2.imag() > 0)
    return false;
  if (p2.imag() < 0)
    return true;
  p2 /= abs(temp);
  return true;
}

int main() {
  int n;
  P p[N_MAX];
  vector<P> stack;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i].x >> p[i].y;
  sort(p, p + n);
  stack.push(p[0]);
  stack.push(p[1]);
  for (int i = 2; i < n; i++) {
    point c = point(p[i].x, p[i].y);
    while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y),
                           point(stack[sz - 1].x, stack[sz - 1].y), c))
      stack.pop_back();
    stack.push(p[i]);
  }
  for (int i = n - 2; i >= 0; i--) {
    point c = point(p[i].x, p[i].y);
    while (sz >= 2 && !ccw(point(stack[sz - 2].x, stack[sz - 2].y),
                           point(stack[sz - 1].x, stack[sz - 1].y), c))
      stack.pop_back();
    stack.push(p[i]);
  }
  stack.erase(stack.begin());
  cout << stack.size() << endl;
  for (int i = sz - 1; i >= 0; i--)
    cout << stack[i].x << ' ' << stack[i].y << endl;
  return 0;
}
Problem p02303 (C++)

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

using namespace std;

#define MAX_N 100000
const double INF = 1e50;

typedef pair<double, double> P;

int N;
P A[MAX_N];

bool compare_y(const P &a, const P &b) { return a.second < b.second; }

double closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].first;
  double d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare_y);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (fabs(a[i].first - x >= d))
      continue;
    for (int j = 0; j < (int)b.size(); j++) {
      double dx = a[i].first - b[b.size() - j - 1].first;
      double dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, sqrt(dx * dx + dy * dy));
    }
    b.push_back(a[i]);
  }
  return d;
}

void solve() {
  sort(A, A + N);
  printf("%.8f\n", closest_pair(A, N));
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%lf%lf", &A[i].first, &A[i].second);
  }
  solve();
  return 0;
}

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

using namespace std;

#define MAX_N 100000
const double INF = 1e50;

typedef pair<double, double> P;

int N;
P A[MAX_N];

bool compare_y(const P &a, const P &b) { return a.second < b.second; }

double closest_pair(P *a, int n) {
  if (n <= 1)
    return INF;
  int m = n / 2;
  double x = a[m].first;
  double d = min(closest_pair(a, m), closest_pair(a + m, n - m));
  inplace_merge(a, a + m, a + n, compare_y);

  vector<P> b;
  for (int i = 0; i < n; i++) {
    if (fabs(a[i].first - x) >= d)
      continue;
    for (int j = 0; j < (int)b.size(); j++) {
      double dx = a[i].first - b[b.size() - j - 1].first;
      double dy = a[i].second - b[b.size() - j - 1].second;
      if (dy >= d)
        break;
      d = min(d, sqrt(dx * dx + dy * dy));
    }
    b.push_back(a[i]);
  }
  return d;
}

void solve() {
  sort(A, A + N);
  printf("%.8f\n", closest_pair(A, N));
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%lf%lf", &A[i].first, &A[i].second);
  }
  solve();
  return 0;
}
Problem p02303 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;
typedef pair<double, double> pp;
vector<pp> a;

#define sqr(x) ((x) * (x))
double Plus(double x) { return x < 0 ? -x : x; }
double dist(pp x, pp y) {
  return sqr(x.first - y.first) + sqr(x.second - y.second);
}

double go(int l, int r) {
  if (r - l == 1)
    return dist(a[l], a[r]);
  if (r - l == 2)
    return min(dist(a[l], a[l + 1]),
               min(dist(a[l], a[l + 2]), dist(a[l + 1], a[l + 2])));

  int m = (l + r) / 2;
  double d = min(go(l, m), go(m + 1, r));

  vector<pp> v;
  for (auto i = l; i <= r; i++)
    if (sqr(a[i].first - a[m].first) < d)
      v.emplace_back(a[i].second, a[i].first);
  sort(v.begin(), v.end());
  for (auto i = v.begin(); i != v.end(); i++)
    for (auto j = i + 1; j != v.end(); j++) {
      d = min(d, dist(*i, *j));
    }
  return d;
}

int main() {
  int i, n;
  double x, y;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%lf%lf", &x, &y), a.emplace_back(x, y);
  sort(a.begin(), a.end());
  printf("%lf ", sqrt(go(0, n - 1)));
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;
typedef pair<double, double> pp;
vector<pp> a;

#define sqr(x) ((x) * (x))
double Plus(double x) { return x < 0 ? -x : x; }
double dist(pp x, pp y) {
  return sqr(x.first - y.first) + sqr(x.second - y.second);
}

double go(int l, int r) {
  if (r - l == 1)
    return dist(a[l], a[r]);
  if (r - l == 2)
    return min(dist(a[l], a[l + 1]),
               min(dist(a[l], a[l + 2]), dist(a[l + 1], a[l + 2])));

  int m = (l + r) / 2;
  double d = min(go(l, m), go(m + 1, r));

  vector<pp> v;
  for (auto i = l; i <= r; i++)
    if (sqr(a[i].first - a[m].first) < d)
      v.emplace_back(a[i].second, a[i].first);
  sort(v.begin(), v.end());
  for (auto i = v.begin(); i != v.end(); i++)
    for (auto j = i + 1; j != v.end(); j++) {
      if (sqr(j->first - i->first) > d)
        break;
      d = min(d, dist(*i, *j));
    }
  return d;
}

int main() {
  int i, n;
  double x, y;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%lf%lf", &x, &y), a.emplace_back(x, y);
  sort(a.begin(), a.end());
  printf("%lf ", sqrt(go(0, n - 1)));
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int main() {
  int N, W;
  int v[1005];
  int dp[1005] = {0};
  cin >> N >> W;
  rep(i, W) { cin >> v[i]; }
  rep(i, N + 1) { dp[i] = i; }
  rep(i, W) {
    rep(j, N + 1) {
      if ((j - v[i]) > -1) {
        dp[j] = min(dp[j - v[i]] + 1, dp[j]);
      }
    }
  }
  cout << dp[N] << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int main() {
  int N, W;
  int v[30];
  int dp[50005] = {0};
  cin >> N >> W;
  rep(i, W) { cin >> v[i]; }
  rep(i, N + 1) { dp[i] = i; }
  rep(i, W) {
    rep(j, N + 1) {
      if ((j - v[i]) > -1) {
        dp[j] = min(dp[j - v[i]] + 1, dp[j]);
      }
    }
  }
  cout << dp[N] << endl;
}
Problem p02314 (C++)

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef long long ll;
#define mod 1000000
#define INF 10000000

int main() {
  int n, m;
  cin >> n >> m;
  vi C(m);
  for (int i = 0; i < m; i++) {
    cin >> C[i];
  }
  vi dp(2 * n + 10, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    int c = C[i];
    for (int j = n; j >= 0; j--) {
      for (int k = 1; j + k * c <= n; k++) {
        dp[j + k * c] = min(dp[j + k * c], k + dp[j]);
      }
    }
    /*for (int j = 0; j <= n; j++) {
            cout << dp[j] << " ";
    }cout << endl;*/
  }
  cout << dp[n] << endl;
  return 0;
}

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef long long ll;
#define mod 1000000
#define INF 10000000

int main() {
  int n, m;
  cin >> n >> m;
  vi C(m);
  for (int i = 0; i < m; i++) {
    cin >> C[i];
  }
  vi dp(2 * n + 10, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    int c = C[i];
    for (int j = 0; j <= n; j++) {
      dp[j + c] = min(dp[j + c], 1 + dp[j]);
    }
    /*for (int j = 0; j <= n; j++) {
            cout << dp[j] << " ";
    }cout << endl;*/
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++)

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

using namespace std;

int n, m;
const int MAX_N = 50000, MAX_M = 20, INF = 1000000;
int c[MAX_M + 1];
int dp[MAX_M + 1][MAX_N + 2];

void solve() {
  for (int j = 0; j <= n; j++) {
    dp[0][j] = INF;
  }

  for (int i = 0; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (j < c[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
      }
    }
  }
  cout << dp[m][n] << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  solve();
  return 0;
}

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

using namespace std;

int n, m;
const int MAX_N = 50000, MAX_M = 20, INF = 1000000;
int c[MAX_M + 2];
int dp[MAX_M + 2][MAX_N + 2];

void solve() {
  for (int j = 0; j <= n; j++) {
    dp[0][j] = INF;
  }

  for (int i = 0; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (j < c[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
      }
    }
  }
  cout << dp[m][n] << endl;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  solve();
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  ;

  vector<int> ms(m);
  for (auto &coin : ms) {
    cin >> coin;
  }
  sort(ms.begin(), ms.end(), greater<int>());

  array<int, 10001> dp = {0};
  FOR_LE(i, 1, n) {
    int min_c = INT_MAX;
    FOR_LT(c, 0, m) {
      if (i - ms[c] < 0)
        continue;
      min_c = min(min_c, dp[i - ms[c]] + 1);
    }
    dp[i] = min_c;
  }

  cout << dp[n] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (int i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (int i = beg; end <= i; i--)

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  ;

  vector<int> ms(m);
  for (auto &coin : ms) {
    cin >> coin;
  }
  sort(ms.begin(), ms.end(), greater<int>());

  array<int, 50001> dp = {0};
  FOR_LE(i, 1, n) {
    int min_c = INT_MAX;
    FOR_LT(c, 0, m) {
      if (i - ms[c] < 0)
        continue;
      min_c = min(min_c, dp[i - ms[c]] + 1);
    }
    dp[i] = min_c;
  }

  cout << dp[n] << endl;

  return 0;
}
Problem p02314 (C++)

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

int main() {
  int i, j, n, m, t[10001], c[21];
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> c[i];
  for (j = 0; j <= n; j++)
    t[j] = 10001;
  t[0] = 0;

  for (i = 0; i < m; i++) {
    for (j = c[i]; j <= n; j++) {
      t[j] = min(t[j], t[j - c[i]] + 1);
    }
  }

  cout << t[n] << endl;
  ;

  return 0;
}

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

int main() {
  int i, j, n, m, t[50001], c[21];
  cin >> n >> m;
  for (i = 0; i < m; i++)
    cin >> c[i];
  for (j = 0; j <= n; j++)
    t[j] = 10001;
  t[0] = 0;

  for (i = 0; i < m; i++) {
    for (j = c[i]; j <= n; j++) {
      t[j] = min(t[j], t[j - c[i]] + 1);
    }
  }

  cout << t[n] << endl;
  ;

  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int totalValueOfShares; // target sum
  int size;               // number of coin types

  cin >> totalValueOfShares >> size;

  vector<int> numOfShares(size);
  vector<int> values(totalValueOfShares);

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

  values[0] = 0;

  int minValue = 0x7FFFFFFF;

  for (int i = 0; i < size; i++) {
    if (numOfShares[i] < minValue) {
      minValue = numOfShares[i];
    }
  }

  for (int i = 1; i <= totalValueOfShares; i++) {
    values[i] = 0x7FFFFFFF;
  }

  for (int i = minValue; i <= totalValueOfShares; i++) {
    for (int j = 0; j < size; j++)
      if (numOfShares[j] <= i) {
        int sum = values[i - numOfShares[j]];
        if (sum < 0x7FFFFFFF && sum + 1 < values[i])
          values[i] = sum + 1;
      }
  }
  if (values[totalValueOfShares] == 0x7FFFFFFF) {
    cout << 0;
  }
  cout << values[totalValueOfShares] << "\r\n";
}


Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int totalValueOfShares; // target sum
  int size;               // number of coin types

  cin >> totalValueOfShares >> size;

  vector<int> numOfShares(size);
  vector<int> values(totalValueOfShares + 1);

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

  values[0] = 0;

  int minValue = 0x7FFFFFFF;

  for (int i = 0; i < size; i++) {
    if (numOfShares[i] < minValue) {
      minValue = numOfShares[i];
    }
  }

  for (int i = 1; i <= totalValueOfShares; i++) {
    values[i] = 0x7FFFFFFF;
  }

  for (int i = minValue; i <= totalValueOfShares; i++) {
    for (int j = 0; j < size; j++)
      if (numOfShares[j] <= i) {
        int sum = values[i - numOfShares[j]];
        if (sum < 0x7FFFFFFF && sum + 1 < values[i])
          values[i] = sum + 1;
      }
  }
  if (values[totalValueOfShares] == 0x7FFFFFFF) {
    cout << 0;
  }
  cout << values[totalValueOfShares] << "\r\n";
}
Problem p02314 (C++)

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

typedef long long LL;
ostream &debug = cout;

/* AOJ DPL_1_A */

const int N = 25;
const int M = 50005;
const int INF = 0x3f3f3f3f;

int n, m, a[N], dp[M];

signed main(void) {
  /* input */
  {
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= n; i++)
      scanf("%d", a + i);
  }
  /* dynamic programming */
  {
    memset(dp, 0x3f, sizeof(dp)), dp[0] = 0;
    for (int i = 1, sum = 0; i <= n; sum += a[i++])
      for (int j = 0; j <= m; j++)
        dp[a[i] + j] = min(dp[a[i] + j], dp[j] + 1);
  }
  /* output */
  { printf("%d\n", dp[m]); }
  //	system("pause");
}

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

typedef long long LL;
ostream &debug = cout;

/* AOJ DPL_1_A */

const int N = 25;
const int M = 60005;
const int INF = 0x3f3f3f3f;

int n, m, a[N], dp[M];

signed main(void) {
  /* input */
  {
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= n; i++)
      scanf("%d", a + i);
  }
  /* dynamic programming */
  {
    memset(dp, 0x3f, sizeof(dp)), dp[0] = 0;
    for (int i = 1, sum = 0; i <= n; sum += a[i++])
      for (int j = 0; j <= m; j++)
        dp[a[i] + j] = min(dp[a[i] + j], dp[j] + 1);
  }
  /* output */
  { printf("%d\n", dp[m]); }
  //	system("pause");
}
Problem p02314 (C++)

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

using namespace std;
int n, m;
int c[21];
int dp[21][50001];
int main() {
  cin >> n >> m;
  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < 21; i++) {
    for (int k = 1; k < 50001; k++) {
      dp[i][k] = 20000;
    }
  }
  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= m; i++) {
    for (int k = 1; k <= 500000; k++) {
      if (k >= c[i])
        dp[i][k] = min(dp[i][k - c[i]] + 1, dp[i - 1][k]);
      else
        dp[i][k] = dp[i - 1][k];
    }
  }

  cout << dp[m][n] << endl;
}

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

using namespace std;
int n, m;
int c[21];
int dp[21][50001];
int main() {
  cin >> n >> m;
  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < 21; i++) {
    for (int k = 1; k < 50001; k++) {
      dp[i][k] = 20000;
    }
  }
  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= m; i++) {
    for (int k = 1; k <= 50000; k++) {
      if (k >= c[i])
        dp[i][k] = min(dp[i][k - c[i]] + 1, dp[i - 1][k]);
      else
        dp[i][k] = dp[i - 1][k];
    }
  }

  cout << dp[m][n] << endl;
}
Problem p02314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define MAX_NUM_COINS 20
#define MAX_PRICE 50000
#define CALC_MIN(a, b) (a < b ? a : b)

int coin[MAX_NUM_COINS];
int memo[MAX_NUM_COINS][MAX_PRICE + 1];

int is_less_than(const void *a, const void *b) { return *(int *)a < *(int *)b; }

int solve_sub_question(int index, int num_coin, int price) {
  if (memo[index][price] != -1) {
    return memo[index][price];
  }
  if (index == num_coin - 1) {
    memo[index][price] = price;
    return price;
  }
  int ans = INT_MAX;
  for (int i = 0; i <= price / coin[index]; i++) {
    int total_coin = i;
    int current_price = price - coin[index] * i;
    total_coin += solve_sub_question(index + 1, num_coin, current_price);
    ans = CALC_MIN(total_coin, ans);
  }
  memo[index][price] = ans;
  return ans;
}

int main() {
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < MAX_NUM_COINS; i++) {
    memo[i][0] = 0;
  }

  int price, num_coin;
  scanf("%d%d", &price, &num_coin);
  for (int i = 0; i < num_coin; i++) {
    scanf("%d", &coin[i]);
  }
  qsort(coin, num_coin, sizeof(int), is_less_than);

  printf("%d\n", solve_sub_question(0, num_coin, price));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

#define MAX_NUM_COINS 20
#define MAX_PRICE 50000
#define CALC_MIN(a, b) (a < b ? a : b)

int coin[MAX_NUM_COINS];
int memo[MAX_NUM_COINS][MAX_PRICE + 1];

int is_less_than(const void *a, const void *b) { return *(int *)a < *(int *)b; }

int solve_sub_question(int index, int num_coin, int price) {
  if (memo[index][price] != -1) {
    return memo[index][price];
  }
  if (index == num_coin - 1) {
    memo[index][price] = price;
    return price;
  }
  if (price % coin[index] == 0) {
    memo[index][price] = price / coin[index];
    return price / coin[index];
  }
  int ans = INT_MAX;
  for (int i = 0; i <= price / coin[index]; i++) {
    int total_coin = i;
    int current_price = price - coin[index] * i;
    total_coin += solve_sub_question(index + 1, num_coin, current_price);
    ans = CALC_MIN(total_coin, ans);
  }
  memo[index][price] = ans;
  return ans;
}

int main() {
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < MAX_NUM_COINS; i++) {
    memo[i][0] = 0;
  }

  int price, num_coin;
  scanf("%d%d", &price, &num_coin);
  for (int i = 0; i < num_coin; i++) {
    scanf("%d", &coin[i]);
  }
  qsort(coin, num_coin, sizeof(int), is_less_than);

  printf("%d\n", solve_sub_question(0, num_coin, price));
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
// #define int long long
#define SIZE 21
#define VALUE 10001
const int infty = 999;
// #define _DEBUG
using namespace std;
int N, M, Cs[SIZE], T[SIZE][VALUE];
int solve() {
  for (int i = 0; i <= M; i++) {
    for (int n = 0; n <= N; n++) {
      if (n == 0) {
        T[i][n] = 0;
      } else if (i == 0) {
        T[i][n] = infty;
      } else if (n < Cs[i - 1]) {
        T[i][n] = T[i - 1][n];
      } else {
        T[i][n] = min(T[i - 1][n],
                      min(T[i - 1][n - Cs[i - 1]], T[i][n - Cs[i - 1]]) + 1);
      }
    }
  }
#ifdef _DEBUG
  for (int i = 0; i <= M; i++) {
    if (i > 0)
      cout << Cs[i - 1] << "\t";
    else
      cout << "i\\n\t";
    for (int n = 0; n <= N; n++) {
      cout << T[i][n] << "\t";
    }
    cout << endl;
  }
#endif
  return T[M][N];
}
signed main() {
  while (cin >> N >> M) {
    for (int i = 0; i < M; i++) {
      cin >> Cs[i];
    }
    cout << solve() << endl;
  }
}


Fixed submission:
#include <iostream>
// #define int long long
#define SIZE 21
#define VALUE 50001
const int infty = 99999;
// #define _DEBUG
using namespace std;
int N, M, Cs[SIZE], T[SIZE][VALUE];
int solve() {
  for (int i = 0; i <= M; i++) {
    for (int n = 0; n <= N; n++) {
      if (n == 0) {
        T[i][n] = 0;
      } else if (i == 0) {
        T[i][n] = infty;
      } else if (n < Cs[i - 1]) {
        T[i][n] = T[i - 1][n];
      } else {
        T[i][n] = min(T[i - 1][n],
                      min(T[i - 1][n - Cs[i - 1]], T[i][n - Cs[i - 1]]) + 1);
      }
    }
  }
#ifdef _DEBUG
  for (int i = 0; i <= M; i++) {
    if (i > 0)
      cout << Cs[i - 1] << "\t";
    else
      cout << "i\\n\t";
    for (int n = 0; n <= N; n++) {
      cout << T[i][n] << "\t";
    }
    cout << endl;
  }
#endif
  return T[M][N];
}
signed main() {
  while (cin >> N >> M) {
    for (int i = 0; i < M; i++) {
      cin >> Cs[i];
    }
    cout << solve() << endl;
  }
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <stdio.h>

using namespace std;

const int N_MAX = 10001, INF = 100000;

int dp[N_MAX], n, m, i, j, d[20];

int main() {

  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d", &d[i]);
  }

  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (i = 0; i <= n; i++) {
    for (j = 0; j < m; j++) {
      if (d[j] <= i) {
        dp[i] = min(dp[i - d[j]] + 1, dp[i]);
      }
    }
  }

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <stdio.h>

using namespace std;

const int N_MAX = 50001, INF = 100000;

int dp[N_MAX], n, m, i, j, d[20];

int main() {

  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%d", &d[i]);
  }

  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (i = 0; i <= n; i++) {
    for (j = 0; j < m; j++) {
      if (d[j] <= i) {
        dp[i] = min(dp[i - d[j]] + 1, dp[i]);
      }
    }
  }

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

  return 0;
}
Problem p02314 (C++)

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

typedef long long ll;
#define INF 1000000

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> c;
  for (ll i = 0; i < m; i++) {
    ll tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  ll dp[n + 1]; // dp[i]:=i????????????????????????????????????????°??????°
  for (ll i = 0; i <= n; i++)
    dp[i] = INF;
  dp[0] = 0;
  for (ll i = 0; i < c.size(); i++) {
    for (ll j = 0; j <= n; j++) {
      ll mini = dp[j];
      for (ll k = 1; j - c[i] * k >= 0; k++) {
        if (mini > dp[j - c[i] * k] + k)
          mini = dp[j - c[i] * k] + k;
      }
      dp[j] = mini;
    }
  }
  cout << dp[n] << endl;
  return 0;
}

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

typedef long long ll;
#define INF 1000000

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> c;
  for (ll i = 0; i < m; i++) {
    ll tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  ll dp[n + 1]; // dp[i]:=i????????????????????????????????????????°??????°
  for (ll i = 0; i <= n; i++)
    dp[i] = INF;
  dp[0] = 0;
  for (ll i = 0; i < c.size(); i++) {
    for (ll j = 0; j <= n; j++) {
      if (c[i] <= j)
        dp[j] = min(dp[j], dp[j - c[i]] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
#define N_MAX 50000
#define M_MAX 20
#define NIL 1000000000

int DP[N_MAX + 1][M_MAX + 1];
int C[M_MAX + 1];

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);

  for (int i = 1; i <= m; i++) {
    scanf("%d", &C[i]);
  }

  for (int i = 0; i <= n; i++) {
    for (int k = 0; k <= m; k++) {
      DP[i][k] = NIL;
    }
  }
  for (int k = 0; k <= m; k++) {
    DP[0][k] = 0;
  }

  for (int k = 1; k <= m; k++) {
    for (int i = 0; i <= n; i++) {
      int coin_n = 0;
      while (i - C[k] * coin_n >= 0) {
        DP[i][k] = min(DP[i][k], DP[i - C[k] * coin_n][k - 1] + coin_n);
        if (DP[i][k] < coin_n) {
          break;
        }
        coin_n++;
      }
      // printf("DP[%d][%d]: %d\n", i, k, DP[i][k]);
    }
  }

  printf("%d\n", DP[n][m]);
}


Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
#define N_MAX 50000
#define M_MAX 20
#define NIL 1000000000

int DP[N_MAX + 1][M_MAX + 1];
int C[M_MAX + 1];

int main(void) {
  int n, m;
  scanf("%d %d", &n, &m);

  for (int i = 1; i <= m; i++) {
    scanf("%d", &C[i]);
  }

  for (int i = 0; i <= n; i++) {
    for (int k = 0; k <= m; k++) {
      DP[i][k] = NIL;
    }
  }
  for (int k = 0; k <= m; k++) {
    DP[0][k] = 0;
  }

  for (int k = 1; k <= m; k++) {
    for (int i = 0; i <= n; i++) {
      if (i - C[k] < 0) {
        DP[i][k] = DP[i][k - 1];
      } else {
        DP[i][k] = min(DP[i][k - 1], DP[i - C[k]][k] + 1);
      }
      // printf("DP[%d][%d]: %d\n", i, k, DP[i][k]);
    }
  }

  printf("%d\n", DP[n][m]);
}
Problem p02314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>

using namespace std;

const long long int MOD = 1000000007;
const long long int INF = 100000000;
long long int N, M;
long long int C[30];
long long int dp[21][100000];

int DP(int n, int v) {
  if (n < 0 || v < 0)
    return INF;

  if (dp[n][v] != -1)
    return dp[n][v];
  return min(DP(n - 1, v), DP(n, v - C[n]) + 1);
}

int main() {
  cin >> N >> M;

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

  memset(dp, -1, sizeof(dp));

  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  cout << DP(M - 1, N) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>

using namespace std;

const long long int MOD = 1000000007;
const long long int INF = 100000000;
long long int N, M;
long long int C[30];
long long int dp[21][100000];

int DP(int n, int v) {
  if (n < 0 || v < 0)
    return INF;

  if (dp[n][v] != -1)
    return dp[n][v];
  return dp[n][v] = min(DP(n - 1, v), DP(n, v - C[n]) + 1);
}

int main() {
  cin >> N >> M;

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

  memset(dp, -1, sizeof(dp));

  for (int i = 0; i < 21; i++) {
    dp[i][0] = 0;
  }
  cout << DP(M - 1, N) << endl;

  return 0;
}
Problem p02314 (C++)

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

#include <algorithm>

using namespace std;

int ans[50000 + 1];

int coin[20];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int i, j;
  for (i = 0; i < m; ++i) {
    scanf("%d", &coin[i]);

    ++ans[coin[i]];
  }

  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (ans[i] + coin[j] <= n) {
        ans[i + coin[j]] =
            ans[i + coin[j]] ? min(ans[i + coin[j]], ans[i] + 1) : ans[i] + 1;
      }
    }
  }

  printf("%d\n", ans[n]);

  return 0;
}

Fixed submission:
#include <stdio.h>

#include <algorithm>

using namespace std;

int ans[50000 + 1];

int coin[20];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);

  int i, j;
  for (i = 0; i < m; ++i) {
    scanf("%d", &coin[i]);

    ++ans[coin[i]];
  }

  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (i + coin[j] <= n) {
        ans[i + coin[j]] =
            ans[i + coin[j]] ? min(ans[i + coin[j]], ans[i] + 1) : ans[i] + 1;
      }
    }
  }

  printf("%d\n", ans[n]);

  return 0;
}
Problem p02314 (C++)

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

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

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

const double EPS = 1e-10;

const int INF = 1000 * 1000 * 1000;
const int N = 500000;
const int M = 20;
int n, m;
int dp[M + 1][N + 1];
int c[N + 1];

int main() {
  int i, j, k;
  scanf("%d%d ", &n, &m);
  for (i = 0; i < m; i++)
    scanf("%d ", c + i + 1);

  for (i = 0; i <= m; i++)
    for (j = 0; j <= n; j++)
      dp[i][j] = INF;

  for (j = 0; j <= n; j++)
    if (!(j % c[1]))
      dp[1][j] = j / c[1];

  for (i = 2; i <= m; i++)
    for (j = 0; j <= n; j++)
      for (k = 0; j - c[i] * k >= 0; k++)
        dp[i][j] = min(dp[i][j], dp[i - 1][j - c[i] * k] + k);
  printf("%d\n", dp[m][n]);

  return 0;
}

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

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

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

const double EPS = 1e-10;

const int INF = 1000 * 1000 * 1000;
const int N = 500000;
const int M = 20;
int n, m;
int dp[M + 1][N + 1];
int c[N + 1];

int main() {
  int i, j, k;
  scanf("%d%d ", &n, &m);
  for (i = 0; i < m; i++)
    scanf("%d ", c + i + 1);

  for (i = 0; i <= m; i++)
    for (j = 0; j <= n; j++)
      dp[i][j] = INF;

  for (j = 0; j <= n; j++)
    if (!(j % c[1]))
      dp[1][j] = j / c[1];

  for (i = 2; i <= m; i++)
    for (j = 0; j <= n; j++) {
      if (j - c[i] < 0)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
      //      for (k=0; j-c[i]*k>=0; k++)
      //        dp[i][j] = min(dp[i][j], dp[i-1][j-c[i]*k]+k);
    }
  printf("%d\n", dp[m][n]);

  return 0;
}
Problem p02314 (C++)

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

#define MAX_N 70

int main() {
  int dp[MAX_N];
  int n, m, c[100];

  memset(dp, -1, sizeof(dp));

  cin >> n >> m;

  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= m; i++) {
    dp[c[i]] = 1;
  }

  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j <= n; j++) {
      if (dp[j] != -1) {
        for (int k = 1; k <= m; k++) {
          if (dp[j + c[k]] == -1 || dp[j + c[k]] > dp[j] + 1) {
            dp[j + c[k]] = dp[j] + 1;
          }
        }
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}

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

#define MAX_N 1000000

int main() {
  int dp[MAX_N];
  int n, m, c[100];

  memset(dp, -1, sizeof(dp));

  cin >> n >> m;

  for (int i = 1; i <= m; i++) {
    cin >> c[i];
  }

  for (int i = 1; i <= m; i++) {
    dp[c[i]] = 1;
  }

  for (int i = 1; i <= 20; i++) {
    for (int j = 1; j <= n; j++) {
      if (dp[j] != -1) {
        for (int k = 1; k <= m; k++) {
          if (dp[j + c[k]] == -1 || dp[j + c[k]] > dp[j] + 1) {
            dp[j + c[k]] = dp[j] + 1;
          }
        }
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  constexpr int INF = 1e9;
  int n, m;
  cin >> n >> m;
  vector<int> dp(n + 1, INF), d(m);
  for (auto i = 0; i < m; i++) {
    cin >> d[i];
    dp[d[i]] = 1;
  }
  for (auto i = 0; i < n + 1; i++) {
    for (auto &j : d) {
      if (i + j > n + 1)
        continue;
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  constexpr int INF = 1e9;
  int n, m;
  cin >> n >> m;
  vector<int> dp(n + 1, INF), d(m);
  for (auto i = 0; i < m; i++) {
    cin >> d[i];
    dp[d[i]] = 1;
  }
  for (auto i = 0; i < n + 1; i++) {
    for (auto &j : d) {
      if (i + j >= n + 1)
        continue;
      dp[i + j] = min(dp[i + j], dp[i] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
///
// File:  dpl_1_a.cpp
// Author: ymiyamoto
//
// Created on Wed Nov  1 01:41:22 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;
#define MAX_N 10001
static vector<int32_t> coins;
static uint32_t dp[MAX_N];

int32_t main() {
  int32_t n, m;
  cin >> n >> m;
  for (int32_t i = 0; i < m; i++) {
    int32_t c;
    cin >> c;
    coins.push_back(c);
  }

  for (int32_t i = 0; i <= n; i++) {
    dp[i] = UINT32_MAX;
    dp[0] = 0;
    for (int32_t j = 0; j < (int32_t)coins.size(); j++) {
      if (i - coins[j] >= 0) {
        dp[i] = min(dp[i], 1 + dp[i - coins[j]]);
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}

Fixed submission:
///
// File:  dpl_1_a.cpp
// Author: ymiyamoto
//
// Created on Wed Nov  1 01:41:22 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;
#define MAX_N 50001
static vector<int32_t> coins;
static uint32_t dp[MAX_N];

int32_t main() {
  int32_t n, m;
  cin >> n >> m;
  for (int32_t i = 0; i < m; i++) {
    int32_t c;
    cin >> c;
    coins.push_back(c);
  }

  for (int32_t i = 0; i <= n; i++) {
    dp[i] = UINT32_MAX;
    dp[0] = 0;
    for (int32_t j = 0; j < (int32_t)coins.size(); j++) {
      if (i - coins[j] >= 0) {
        dp[i] = min(dp[i], 1 + dp[i - coins[j]]);
      }
    }
  }

  cout << dp[n] << endl;

  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define M 21
#define N 50010
using namespace std;
int dp[N];

int Min(int &a, int b) { return a = min(a, b); }

int main() {
  int n, m;
  cin >> n >> m;

  int coin[M];
  for (int i = 0; i < m; i++)
    cin >> coin[i];

  for (int i = 0; i < N; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j <= n; j++)
      Min(dp[j + coin[i]], dp[j] + 1);

  cout << dp[n] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define M 21
#define N 100010
using namespace std;
int dp[N];

int Min(int &a, int b) { return a = min(a, b); }

int main() {
  int n, m;
  cin >> n >> m;

  int coin[M];
  for (int i = 0; i < m; i++)
    cin >> coin[i];

  for (int i = 0; i < N; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < m; i++)
    for (int j = 0; j <= n; j++)
      Min(dp[j + coin[i]], dp[j] + 1);

  cout << dp[n] << endl;

  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>
using namespace std;
int main() {
  int n, m, t;
  int c[21];
  int T[21][10001];
  cin >> n >> m;
  int counter = 0;
  for (int k = 0; k < m; ++k) {
    cin >> t;
    c[k] = t;
    if (c[k] > n) {
      c[k] = c[k + 1];
      counter++;
    }
    for (int j = 1; j <= n; ++j) {
      T[k][0] = 0;
      T[0][j] = 100000;
    }
  }
  int m1 = m - counter;
  for (int i = 1; i <= m1; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (c[i - 1] <= j) {
        T[i][j] = min(T[i - 1][j], T[i][j - c[i - 1]] + 1);
      } else
        T[i][j] = T[i - 1][j];
    }
  }
  cout << T[m1][n] << endl;
}

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, m, t;
  int c[21];
  int T[21][100001];
  cin >> n >> m;
  int counter = 0;
  for (int k = 0; k < m; ++k) {
    cin >> t;
    c[k] = t;
    if (c[k] > n) {
      c[k] = c[k + 1];
      counter++;
    }
    for (int j = 1; j <= n; ++j) {
      T[k][0] = 0;
      T[0][j] = 100000;
    }
  }
  int m1 = m - counter;
  for (int i = 1; i <= m1; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (c[i - 1] <= j) {
        T[i][j] = min(T[i - 1][j], T[i][j - c[i - 1]] + 1);
      } else
        T[i][j] = T[i - 1][j];
    }
  }
  cout << T[m1][n] << endl;
}
Problem p02314 (C++)

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

using namespace std;

unsigned short dp[20][50010];

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> c(M);
  for (auto &i : c)
    cin >> i;
  sort(c.begin(), c.end());
  for (int j = 0; j <= N; j++)
    dp[0][j] = j;
  int m;
  for (int i = 1; i < M; i++) {
    for (int j = 0; j <= N; j++) {
      m = dp[i - 1][j];
      for (int k = 1; j >= k * c[i]; k++)
        m = min(m, dp[i - 1][j - k * c[i]] + k);
      dp[i][j] = m;
    }
  }
  cout << dp[M - 1][N] << endl;
  return 0;
}


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

using namespace std;

unsigned short dp[20][50010];

int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> c(M);
  for (auto &i : c)
    cin >> i;
  sort(c.begin(), c.end());
  for (int j = 0; j <= N; j++)
    dp[0][j] = j;
  int m;
  for (int i = 1; i < M; i++) {
    for (int j = 0; j <= N; j++) {
      m = dp[i - 1][j];
      for (int k = j / c[i]; j / c[i] - k < 10 && k > 0; k--)
        m = min(m, dp[i - 1][j - k * c[i]] + k);
      dp[i][j] = m;
    }
  }
  cout << dp[M - 1][N] << endl;
  return 0;
}