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

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

using namespace std;

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

  vector<int> dp(n);
  vector<int> coins(m);
  for (int i = 0; i < m; ++i) {
    int coin;
    cin >> coin;
    coins[i] = coin;
  }

  dp[0] = 0;

  for (int val = 1; val <= n; ++val) {
    int min = numeric_limits<int>::max();
    for (auto coin : coins) {
      if (val >= coin) {
        min = dp[val - coin] < min ? dp[val - coin] : min;
      }
    }
    dp[val] = 1 + min;
  }
  cout << dp[n] << endl;
}


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

using namespace std;

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

  vector<int> dp(n + 1);
  vector<int> coins(m);
  for (int i = 0; i < m; ++i) {
    int coin;
    cin >> coin;
    coins[i] = coin;
  }

  dp[0] = 0;

  for (int val = 1; val <= n; ++val) {
    int min = numeric_limits<int>::max();
    for (auto coin : coins) {
      if (val >= coin) {
        min = dp[val - coin] < min ? dp[val - coin] : min;
      }
    }
    dp[val] = 1 + min;
  }
  cout << dp[n] << endl;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int i, j, a, b, c[20], d[50001], min = 2000000000;
int main(void) {

  scanf("%d %d", &a, &b);
  for (i = 0; i < b; i++) {
    scanf("%d", &c[i]);
  }
  for (j = 0; j <= a; j++) {
    d[j] = min;
  }
  d[0] = 0;
  for (i = 0; i < b; i++) {
    for (j = 0; j <= a; j++) {
      if (d[j] != min) {
        if (d[j] + 1 < d[j + c[i]] && j + c[i] <= a) {
          d[j + c[i]] = d[j] + 1;
        }
      }
    }
  }
  printf("%d\n", d[a]);
  return 0;
}

Fixed submission:
#include <stdio.h>
int i, j, a, b, c[20], d[50001], min = 2000000000;
int main(void) {

  scanf("%d %d", &a, &b);
  for (i = 0; i < b; i++) {
    scanf("%d", &c[i]);
  }
  for (j = 0; j <= a; j++) {
    d[j] = min;
  }
  d[0] = 0;
  for (i = 0; i < b; i++) {
    for (j = 0; j <= a; j++) {
      if (d[j] != min && j + c[i] <= a) {
        if (d[j] + 1 < d[j + c[i]]) {
          d[j + c[i]] = d[j] + 1;
        }
      }
    }
  }
  printf("%d\n", d[a]);
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

#define INF INT_MAX / 2

int main() {
  int n, m;
  int c[20];
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> c[i];
  int dp[50000 + 1];
  fill_n(dp, n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i <= m; i++)
    for (int j = c[i]; j <= n; j++)
      dp[j] = min(dp[j], dp[j - c[i]] + 1);

  cout << dp[n] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

#define INF INT_MAX / 2

int main() {
  int n, m;
  int c[20];
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> c[i];
  int dp[50000 + 1];
  fill_n(dp, n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i < m; i++)
    for (int j = c[i]; j <= n; 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 <algorithm>
#include <iostream>

using namespace std;

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

  int coins[m];
  for (int i = 0; i < m; ++i) {
    cin >> coins[i];
  }
  sort(coins, coins + m, greater<>());

  int dp[m + 1][50001];
  for (int i = 0; i <= 50000; ++i) {
    if (i == 0)
      dp[m][i] = 0;
    else
      dp[m][i] = 1'000'000'000;
  }

  for (int i = m - 1; i >= 0; --i) {
    for (int j = 0; j <= n; ++j) {
      if (j < coins[i]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        int min_val = dp[i + 1][j];
        for (int k = 0; k <= (j / coins[i]); ++k) {
          min_val = min(min_val, dp[i + 1][j - coins[i] * k] + k);
        }
        dp[i][j] = min_val;
      }
    }
  }

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


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

using namespace std;

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

  int coins[m];
  for (int i = 0; i < m; ++i) {
    cin >> coins[i];
  }
  sort(coins, coins + m, greater<>());

  int dp[m + 1][50001];
  for (int i = 0; i <= 50000; ++i) {
    if (i == 0)
      dp[m][i] = 0;
    else
      dp[m][i] = 1'000'000'000;
  }

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

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

Buggy submission (status: Runtime Error):
// DPL_1_A.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
int n, m;
int dp[10001], coin[20];
int solve() {
  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (i + coin[j] <= n)
        dp[i + coin[j]] = min(dp[i + coin[j]], dp[i] + 1);
  return dp[n];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i)
    cin >> coin[i];
  cout << solve() << endl;
  ;
  return 0;
}


Fixed submission:
// DPL_1_A.cpp
//
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
int n, m;
int dp[50001], coin[20];
int solve() {
  fill(dp, dp + n + 1, INF);
  dp[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (i + coin[j] <= n)
        dp[i + coin[j]] = min(dp[i + coin[j]], dp[i] + 1);
  return dp[n];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i)
    cin >> coin[i];
  cout << solve() << endl;
  ;
  return 0;
}
Problem p02314 (C++)

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

using namespace std;
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (int)N; i++)

#define M 20
#define N 50001

int d[M];
int DP[M][N];

int solve(int i, int x) {
  int res = N;
  if (DP[i][x] != N)
    return DP[i][x];
  for (int k = 0, kMax = x / d[i]; k <= kMax; k++)
    res = min(res, solve(i - 1, x - k * d[i]) + k);
  DP[i][x] = res;
  return res;
}

int main() {
  int n, m;

  cin >> n >> m;
  REP(i, M) cin >> d[i];

  for (int x = 0; x <= n; x++) {
    for (int i = 0; i < m; i++)
      DP[i][x] = N;
  }
  for (int k = 0, kMax = n / d[0]; k <= kMax; k++)
    DP[0][k * d[0]] = k;

  cout << solve(m - 1, n) << endl;

  return 0;
}

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

using namespace std;
typedef long long ll;

#define REP(i, N) for (int i = 0; i < (int)N; i++)

#define M 20
#define N 50001

int d[M];
int DP[M][N];

int solve(int i, int x) {
  int res = N;
  if (DP[i][x] != N)
    return DP[i][x];
  res = solve(i - 1, x);
  if (x - d[i] >= 0)
    res = min(res, solve(i, x - d[i]) + 1);
  DP[i][x] = res;
  return res;
}

int main() {
  int n, m;

  cin >> n >> m;
  REP(i, M) cin >> d[i];

  for (int x = 0; x <= n; x++) {
    for (int i = 0; i < m; i++)
      DP[i][x] = N;
  }
  for (int k = 0, kMax = n / d[0]; k <= kMax; k++)
    DP[0][k * d[0]] = k;

  cout << solve(m - 1, n) << endl;

  return 0;
}
Problem p02314 (C++)

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

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }

int n, m;
int c[21];
int dp[21][10001]; // pos,yen/min
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  rep(i, n + 1) dp[0][i] = i * c[0];
  REP(i, 1, m) {
    REP(j, 1, n + 1) {
      if (j - c[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*	rep(i,m){
                  REP(j,1,n+1)p(dp[i][j]);
                  cout<<endl;
          }*/
  P(dp[m - 1][n]);
  return 0;
}

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

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }

int n, m;
int c[21];
int dp[21][50001]; // pos,yen/min
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  rep(i, m) cin >> c[i];
  rep(i, n + 1) dp[0][i] = i * c[0];
  REP(i, 1, m) {
    REP(j, 1, n + 1) {
      if (j - c[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*	rep(i,m){
                  REP(j,1,n+1)p(dp[i][j]);
                  cout<<endl;
          }*/
  P(dp[m - 1][n]);
  return 0;
}
Problem p02314 (C++)

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

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }
int dp[21][10001];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vi c(m);
  rep(i, m) cin >> c[i];
  rep(i, m + 1) rep(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, m) {
    rep(j, n + 1) {
      if (j - c[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*REP(i,1,m+1){
          rep(j,n+1)p(dp[i][j]);
          cout<<endl;
  }*/
  P(dp[m][n]);
  return 0;
}

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

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define p(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1 << 25

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }
int dp[21][50001];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vi c(m);
  rep(i, m) cin >> c[i];
  rep(i, m + 1) rep(j, n + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, m) {
    rep(j, n + 1) {
      if (j - c[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - c[i]] + 1);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*REP(i,1,m+1){
          rep(j,n+1)p(dp[i][j]);
          cout<<endl;
  }*/
  P(dp[m][n]);
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
static const int INF = int(1e8);
using namespace std;

int dp[10001] = {};
int denom[10001] = {};
int main() {
  int n;
  int m;
  cin >> n;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> denom[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
    for (int j = 0; j <= m; j++) {
      if (i >= denom[j]) {
        dp[i] = min(dp[i], dp[i - denom[j]] + 1);
      }
    }
  }
  cout << dp[n] << "\n";
  return 0;
}

Fixed submission:
#include <iostream>
static const int INF = int(1e8);
using namespace std;

int dp[55555] = {};
int denom[55555] = {};
int main() {
  int n;
  int m;
  cin >> n;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> denom[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = INF;
    for (int j = 0; j <= m; j++) {
      if (i >= denom[j]) {
        dp[i] = min(dp[i], dp[i - denom[j]] + 1);
      }
    }
  }
  cout << dp[n] << "\n";
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Coin {
public:
  int n, m;
  vector<int> c;
  vector<vector<int>> dp;
  Coin() {}
  Coin(int n, int m) : n(n), m(m), c(m), dp(m, vector<int>(n + 1)) {}
  int solve();
};

int Coin::solve() {
  for (auto i = 0; i <= n; ++i)
    dp[0][i] = i;

  for (auto i = 0; i < m; ++i)
    dp[i][0] = 0;

  sort(c.begin(), c.end());

  for (auto i = 1; i < m; ++i) {
    for (auto j = 1; j <= n; ++j) {
      int mc = dp[i - 1][j];

      for (auto k = 1; j >= k * c[i]; ++k) {
        mc = min(mc, dp[i - 1][j - k * c[i]] + k);
      }

      dp[i][j] = mc;
    }
  }

  cout << dp[m - 1][n] << endl;

  return 0;
}

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

  Coin c(n, m);

  for (auto i = 0; i < m; ++i)
    cin >> c.c[i];

  c.solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

class Coin {
public:
  int n, m;
  vector<int> c;
  vector<vector<int>> dp;
  Coin() {}
  Coin(int n, int m) : n(n), m(m), c(m), dp(m, vector<int>(n + 1)) {}
  int solve();
};

int Coin::solve() {
  for (auto i = 0; i <= n; ++i)
    dp[0][i] = i;

  for (auto i = 0; i < m; ++i)
    dp[i][0] = 0;

  sort(c.begin(), c.end());

  for (auto i = 1; i < m; ++i) {
    for (auto j = 1; j <= n; ++j) {
      int mc = dp[i - 1][j];

      if (j >= c[i]) {
        mc = min(mc, dp[i - 1][j - c[i]] + 1);
        mc = min(mc, dp[i][j - c[i]] + 1);
      }

      dp[i][j] = mc;
    }
  }

  cout << dp[m - 1][n] << endl;

  return 0;
}

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

  Coin c(n, m);

  for (auto i = 0; i < m; ++i)
    cin >> c.c[i];

  c.solve();

  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define N 10001
#define INFINITY 2000000000
using namespace std;
const int M = 21;
void getTheNumberOfCoin();
int n, m, T[M][N], C[M], i, j, minv;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++)
    cin >> C[i];
  getTheNumberOfCoin();
  return 0;
}
void getTheNumberOfCoin() {
  for (i = 1; i <= n; i++)
    T[0][i] = INFINITY;
  for (i = 1; i <= m; i++)
    T[i][0] = 0;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (j >= C[i]) {
        T[i][j] = min(T[i - 1][j], T[i][j - C[i]] + 1);
      } else {
        T[i][j] = T[i - 1][j];
      }
    }
  }
  minv = INFINITY;
  for (i = 1; i <= m; i++) {
    minv = min(minv, T[i][n]);
  }
  cout << minv << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define N 50001
#define INFINITY 2000000000
using namespace std;
const int M = 21;
void getTheNumberOfCoin();
int n, m, T[M][N], C[M], i, j, minv;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++)
    cin >> C[i];
  getTheNumberOfCoin();
  return 0;
}
void getTheNumberOfCoin() {
  for (i = 1; i <= n; i++)
    T[0][i] = INFINITY;
  for (i = 1; i <= m; i++)
    T[i][0] = 0;
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      if (j >= C[i]) {
        T[i][j] = min(T[i - 1][j], T[i][j - C[i]] + 1);
      } else {
        T[i][j] = T[i - 1][j];
      }
    }
  }
  minv = INFINITY;
  for (i = 1; i <= m; i++) {
    minv = min(minv, T[i][n]);
  }
  cout << minv << endl;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  rep(i, m) {
    int a;
    cin >> a;
    v.pb(a);
  }
  int dp[10001];
  fill_n(dp, 10001, INF);

  dp[0] = 0;
  rep(i, n + 1) {
    rep(j, v.size()) {
      if (i - v[j] >= 0)
        dp[i] = min(dp[i - v[j]] + 1, dp[i]);
    }
  }
  FIN(dp[n]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define FOR(I, F, N) for (int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout << V << endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void) {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  rep(i, m) {
    int a;
    cin >> a;
    v.pb(a);
  }
  int dp[n + 1];
  fill_n(dp, n + 1, INF);

  dp[0] = 0;
  rep(i, n + 1) {
    rep(j, v.size()) {
      if (i - v[j] >= 0)
        dp[i] = min(dp[i - v[j]] + 1, dp[i]);
    }
  }
  FIN(dp[n]);

  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int ws[23], sum, num, dp[10003];
  cin >> sum >> num;
  for (int i = 0; i < num; i++)
    cin >> ws[i];
  for (int i = 0; i <= sum; i++)
    dp[i] = i;
  for (int i = 1; i < num; i++) {
    for (int j = ws[i]; j <= sum; j++)
      dp[j] = min(dp[j], dp[j - ws[i]] + 1);
  }
  cout << dp[sum] << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int ws[23], sum, num, dp[50003];
  cin >> sum >> num;
  for (int i = 0; i < num; i++)
    cin >> ws[i];
  for (int i = 0; i <= sum; i++)
    dp[i] = i;
  for (int i = 1; i < num; i++) {
    for (int j = ws[i]; j <= sum; j++)
      dp[j] = min(dp[j], dp[j - ws[i]] + 1);
  }
  cout << dp[sum] << endl;
}
Problem p02314 (C++)

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

int n, m;
int c[20];
int lp[50005] = {};

int solve(int x) {
  if (lp[x] != 0)
    return lp[x];
  int t = INT_MAX;
  for (int i = 1; i <= x / 2; i++)
    t = min(t, solve(i) + solve(x - i));
  lp[x] = t;
  return t;
}

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

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

int n, m;
int c[20];
int lp[50005] = {};

int solve(int x) {
  if (lp[x] != 0)
    return lp[x];
  int t = INT_MAX;
  for (int i = 0; i < m; i++)
    if (x > c[i])
      t = min(t, solve(x - c[i]) + 1);
  lp[x] = t;
  return t;
}

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

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

const int MAXN = 50000;
const int INF = 114514;

int main() {
  int N, M;
  int coin[21];
  int dp[MAXN + 1];

  memset(dp, INF, sizeof(dp));
  dp[0] = 0;

  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++)
    scanf("%d", &coin[i]);

  for (int i = 0; i <= M; i++) {
    for (int j = coin[i]; j <= N; j++) {
      dp[j] = min(dp[j], dp[j - coin[i]] + 1);
    }
  }

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

  return (0);
}

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

const int MAXN = 50000;
const int INF = 114514;

int main() {
  int N, M;
  int coin[21];
  int dp[MAXN + 1];

  memset(dp, INF, sizeof(dp));
  dp[0] = 0;

  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++)
    scanf("%d", &coin[i]);

  for (int i = 0; i < M; i++) {
    for (int j = coin[i]; j <= N; j++) {
      dp[j] = min(dp[j], dp[j - coin[i]] + 1);
    }
  }

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

  return (0);
}
Problem p02314 (C++)

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

#define F first
#define S second
#define mp make_pair
// everything go according to my plan
#define pb push_back
#define sz(a) (int)(a.size())
#define vec vector
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
#define y1 Y_U_NO_y1
#define left Y_U_NO_left
#define right Y_U_NO_right

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(string in = "s") {
  if (fopen(in.c_str(), "r"))
    freopen(in.c_str(), "r", stdin);
  else
    cerr << "Input file not found" << endl;
}
const int N = 5e4 + 1;

int dp[N];

int n, m;

int main() {
  localInput();
  Read_rap();
  cin >> n >> m;
  fill(dp + 1, dp + n + 1, MX);

  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    for (int j = 0; j + x <= n; j++)
      dp[j + x] = min(dp[j + x], dp[j] + 1);
  }
  cout << dp[n];

  cout << endl;

  return 0;
}

// Coded by Z..


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

#define F first
#define S second
#define mp make_pair
// everything go according to my plan
#define pb push_back
#define sz(a) (int)(a.size())
#define vec vector
// shimkenttin kyzdary, dzyn, dzyn, dzyn...
#define y1 Y_U_NO_y1
#define left Y_U_NO_left
#define right Y_U_NO_right

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

const int Mod = (int)1e9 + 7;
const int MX = 1073741822;
const ll MXLL = 4e18;
const int Sz = 1110111;
// a pinch of soul
inline void Read_rap() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
inline void randomizer3000() {
  unsigned int seed;
  asm("rdtsc" : "=A"(seed));
  srand(seed);
}
void files(string problem) {
  if (fopen((problem + ".in").c_str(), "r")) {
    freopen((problem + ".in").c_str(), "r", stdin);
    freopen((problem + ".out").c_str(), "w", stdout);
  }
}
void localInput(string in = "s") {
  if (fopen(in.c_str(), "r"))
    freopen(in.c_str(), "r", stdin);
  else
    cerr << "Input file not found" << endl;
}
const int N = 5e4 + 1;

int dp[N];

int n, m;

int main() {
  cin >> n >> m;
  fill(dp + 1, dp + n + 1, MX);

  for (int i = 1; i <= m; i++) {
    int x;
    cin >> x;
    for (int j = 0; j + x <= n; j++)
      dp[j + x] = min(dp[j + x], dp[j] + 1);
  }
  cout << dp[n];

  cout << endl;

  return 0;
}

// Coded by Z..
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

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

#define INF INT_MAX / 2 - 1
#define PI 2 * acos(0.0)
#define ll long long

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  int c[20];
  REP(i, m)
  cin >> c[i];
  int dp[10001];
  fill_n(dp, 10001, INF);
  dp[0] = 0;
  REP(i, m)
  FOR(j, c[i], n + 1)
  dp[j] = min(dp[j], dp[j - c[i]] + 1);
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

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

#define INF INT_MAX / 2 - 1
#define PI 2 * acos(0.0)
#define ll long long

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int n, m;
  cin >> n >> m;
  int c[20];
  REP(i, m)
  cin >> c[i];
  int dp[50001];
  fill_n(dp, 50001, INF);
  dp[0] = 0;
  REP(i, m)
  FOR(j, c[i], n + 1)
  dp[j] = min(dp[j], dp[j - c[i]] + 1);
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m;
  cin >> n >> m;
  vector<ll> c(m);
  FOR(i, 0, m) cin >> c[i];
  vector<ll> dp(n + 1);
  FOR(i, 0, n + 1) dp[i] = i;
  FOR(i, 0, m) {
    FORR(j, 0, n + 1) {
      if (j + c[j] <= n)
        dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, m;
  cin >> n >> m;
  vector<ll> c(m);
  FOR(i, 0, m) cin >> c[i];
  vector<ll> dp(n + 1);
  FOR(i, 0, n + 1) dp[i] = i;
  FOR(i, 0, m) {
    FOR(j, 0, n + 1) {
      if (j + c[i] <= n)
        dp[j + c[i]] = min(dp[j + c[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define int long long
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 999999999999999999;
using namespace std;
int n, m, a[20], dp[60001];
signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  fill(dp, dp + 60001, inf);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      int z = 1;
      while (a[i] * z + j <= n) {
        dp[j + a[i] * z] = min(dp[j + a[i] * z], dp[j] + z);
        z++;
      }
    }
  }
  cout << dp[n] << endl;
  getchar();
  getchar();
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>
#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define int long long
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf = 999999999999999999;
using namespace std;
int n, m, a[20], dp[60001];
signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  fill(dp, dp + 60001, inf);
  dp[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      dp[j + a[i]] = min(dp[j + a[i]], dp[j] + 1);
    }
  }
  cout << dp[n] << endl;
  getchar();
  getchar();
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define rep(i, a, b) for (int(i) = (a); i < (b); i++)
#define INF 1000000000
#define MAX_N 100005
using namespace std;

int main() {
  int n, m;
  int c[21], dp[21][50001];

  cin >> n >> m;
  rep(i, 0, m) cin >> c[i];

  rep(i, 0, m + 1) rep(j, 0, n + 1) dp[i][j] = INF;
  rep(i, 0, m + 1) dp[i][0] = 0;

  rep(i, 0, m + 1) {
    rep(j, 0, n + 1) {
      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);
    }
  }
  /*
  rep(i,0,m+1){
      rep(j,0,n+1){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }
  */

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define rep(i, a, b) for (int(i) = (a); i < (b); i++)
#define INF 1000000000
#define MAX_N 100005
using namespace std;

int main() {
  int n, m;
  int c[21], dp[21][50001];

  cin >> n >> m;
  rep(i, 0, m) cin >> c[i];

  rep(i, 0, m + 1) rep(j, 0, n + 1) dp[i][j] = INF;
  rep(i, 0, m + 1) dp[i][0] = 0;

  rep(i, 0, m) {
    rep(j, 0, n + 1) {
      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);
    }
  }
  /*
  rep(i,0,m+1){
      rep(j,0,n+1){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }
  */

  cout << dp[m][n] << endl;
  return 0;
}
Problem p02314 (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 main() {
  int INF = 100000000;
  int n, m;
  cin >> n >> m;
  int dp[10010];
  rep(i, 10010) dp[i] = INF;
  dp[0] = 0;
  int c[m];
  rep(i, m) cin >> c[i];
  rep(i, n + 1) {
    rep(j, m) {
      int tmp = INF;
      if (i - c[j] >= 0)
        tmp = dp[i - c[j]] + 1;
      dp[i] = min(tmp, dp[i]);
    }
  }
  cout << dp[n] << endl;
  return 0;
}


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

int main() {
  int INF = 100000000;
  int n, m;
  cin >> n >> m;
  int dp[50010];
  rep(i, 50010) dp[i] = INF;
  dp[0] = 0;
  int c[m];
  rep(i, m) cin >> c[i];
  rep(i, n + 1) {
    rep(j, m) {
      int tmp = INF;
      if (i - c[j] >= 0)
        tmp = dp[i - c[j]] + 1;
      dp[i] = min(tmp, dp[i]);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define INF (1 << 30)
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<long long> dp(n, INF);
  vector<int> c(m, 0);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto val : c) {
      if (i - val < 0)
        continue;
      dp[i] = min(dp[i], dp[i - val] + 1);
    }
  }
  cout << dp[n] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define INF (1 << 30)
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<long long> dp(n + 1, INF);
  vector<int> c(m, 0);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (auto val : c) {
      if (i - val < 0)
        continue;
      dp[i] = min(dp[i], dp[i - val] + 1);
    }
  }
  cout << dp[n] << endl;
}
Problem p02314 (C++)

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

int main() {
  int n, m;
  int dp[21][50001];
  vector<int> c;

  //??\???
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  sort(c.begin(), c.end());

  // dp?????????
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= n; i++)
    dp[0][i] = i;

  //???????¨?????????§?§£???
  for (int i = 1; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      int min_coins = dp[i - 1][j]; // ????°???????????????°
      int Cj_num = 1;               // Cj????????????????????°
      while (j - c[i] * Cj_num >= 0) {
        if (min_coins > dp[i - 1][j - c[i] * Cj_num] + Cj_num) {
          min_coins = dp[i - 1][j - c[i] * Cj_num] + Cj_num;
        }
        Cj_num++;
      }
      dp[i][j] = min_coins;
    }
  }

  cout << dp[m - 1][n] << endl;

  return 0;
}

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

int main() {
  int n, m;
  int dp[21][50001];
  vector<int> c;

  //??\???
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    c.push_back(tmp);
  }
  sort(c.begin(), c.end());

  // dp?????????
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= n; i++)
    dp[0][i] = i;

  //???????¨?????????§?§£???
  for (int i = 1; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - c[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i][j - c[i]] + 1);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }

  cout << dp[m - 1][n] << endl;

  return 0;
}
Problem p02314 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m;
  int gaku[20];
  int dp[21][50001];
  cin >> n >> m;
  for (int i = m - 1; i >= 0; i--)
    cin >> gaku[i];
  for (int i = 0; i <= 50000; i++) {
    for (int j = 0; j <= 20; j++)
      dp[j][i] = INF;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      for (int k = 0; k <= j / gaku[i - 1]; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - k * gaku[i - 1]] + k);
      }
    }
  }
  cout << dp[m][n] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define INF 1e+9
using namespace std;

int main() {
  int n, m;
  int gaku[20];
  int dp[21][50001];
  cin >> n >> m;
  for (int i = m - 1; i >= 0; i--)
    cin >> gaku[i];
  for (int i = 0; i <= 50000; i++) {
    for (int j = 0; j <= 20; j++)
      dp[j][i] = INF;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - gaku[i - 1] < 0)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = min(dp[i - 1][j], dp[i][j - gaku[i - 1]] + 1);
    }
  }
  cout << dp[m][n] << endl;
  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <string.h>
int N, W, dp[100][10005], v[105], w[105], i;

int dfs(int d, int nw) {
  int r;
  if (dp[d][nw] >= 0)
    return dp[d][nw];
  if (d == N)
    r = 0;
  else if (w[d] > nw)
    r = dfs(d + 1, nw);
  else
    r = std::max(dfs(d + 1, nw), dfs(d + 1, nw - w[d]) + v[d]);
  return dp[d][nw] = r;
}

int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%d", &N, &W);
  for (i = 0; i < N; i++)
    scanf("%d%d", &v[i], &w[i]);
  printf("%d\n", dfs(0, W));
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <string.h>
int N, W, dp[105][20005], v[105], w[105], i;

int dfs(int d, int nw) {
  int r;
  if (dp[d][nw] >= 0)
    return dp[d][nw];
  if (d == N)
    r = 0;
  else if (w[d] > nw)
    r = dfs(d + 1, nw);
  else
    r = std::max(dfs(d + 1, nw), dfs(d + 1, nw - w[d]) + v[d]);
  return dp[d][nw] = r;
}

int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%d", &N, &W);
  for (i = 0; i < N; i++)
    scanf("%d%d", &v[i], &w[i]);
  printf("%d\n", dfs(0, W));
  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, W, a, b;
vector<int> v;
vector<int> w;

int memo[100][10000];
int fun(int i, int sw) {
  int temp;
  if (memo[i][sw] >= 0)
    return memo[i][sw];
  if (i == n) {
    temp = 0;
  } else if (w[i] > sw) {
    temp = fun(i + 1, sw);
  } else {
    temp = max(fun(i + 1, sw), fun(i + 1, sw - w[i]) + v[i]);
  }
  return memo[i][sw] = temp;
}

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    w.push_back(b);
    v.push_back(a);
  }
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < W + 1; ++j) {
      memo[i][j] = -1;
    }
  }
  cout << fun(0, W) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, W, a, b;
vector<int> v;
vector<int> w;

int memo[101][10001];
int fun(int i, int sw) {
  int temp;
  if (memo[i][sw] >= 0)
    return memo[i][sw];
  if (i == n) {
    temp = 0;
  } else if (w[i] > sw) {
    temp = fun(i + 1, sw);
  } else {
    temp = max(fun(i + 1, sw), fun(i + 1, sw - w[i]) + v[i]);
  }
  return memo[i][sw] = temp;
}

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    w.push_back(b);
    v.push_back(a);
  }
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < W + 1; ++j) {
      memo[i][j] = -1;
    }
  }
  cout << fun(0, W) << endl;
  return 0;
}
Problem p02315 (C++)

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

int main() {
  int N, W;
  cin >> N >> W;

  vector<int> v(N);
  vector<int> w(W + 1);
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }

  vector<vector<int>> DP(N);
  for (int i = 0; i < N; i++) {
    DP[i].resize(W + 1);
    fill(DP[i].begin(), DP[i].end(), 0);
  }

  DP[0][w[0]] = v[0];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i]) {
        DP[i][j] = DP[i - 1][j];
      } else {
        DP[i][j] = max(DP[i - 1][j - w[i]] + v[i], DP[i - 1][j]);
      }
    }
  }

  int maxVal = 0;
  for (int i = 0; i <= W; i++) {
    maxVal = max(DP[N - 1][i], maxVal);
  }
  cout << maxVal << endl;

  return 0;
}

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

int main() {
  int N, W;
  cin >> N >> W;

  vector<int> v(N);
  vector<int> w(N);
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }

  vector<vector<int>> DP(N);
  for (int i = 0; i < N; i++) {
    DP[i].resize(W + 1);
    fill(DP[i].begin(), DP[i].end(), 0);
  }

  DP[0][w[0]] = v[0];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i]) {
        DP[i][j] = DP[i - 1][j];
      } else {
        DP[i][j] = max(DP[i - 1][j - w[i]] + v[i], DP[i - 1][j]);
      }
    }
  }

  int maxVal = 0;
  for (int i = 0; i <= W; i++) {
    maxVal = max(DP[N - 1][i], maxVal);
  }
  cout << maxVal << endl;

  return 0;
}
Problem p02315 (C++)

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

using namespace std;

long long N, W, K, v[300], w[300];
long long dp[100][10100];

int main() {

  cin >> N >> W;

  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 1; i <= W; i++)
    dp[N + 1][i] = 0;

  for (int i = N; i >= 1; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }

  /*
  for(int i=1;i<=N;i++){
    for(int j=0;j<=W;j++){
      printf("%4d",dp[i][j]);
    }
    cout<<endl;
  }
*/

  cout << dp[1][W] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

long long N, W, K, v[300], w[300];
long long dp[1000][10100];

int main() {

  cin >> N >> W;

  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }

  for (int i = 1; i <= W; i++)
    dp[N + 1][i] = 0;

  for (int i = N; i >= 1; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }

  /*
  for(int i=1;i<=N;i++){
    for(int j=0;j<=W;j++){
      printf("%4d",dp[i][j]);
    }
    cout<<endl;
  }
*/

  cout << dp[1][W] << endl;
}
Problem p02315 (C++)

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

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

const int INF = 1e9;

const int MAX_N = 110, MAX_W = 1010;
int dp[MAX_W];

int main() {
  int n, W;
  cin >> n >> W;
  vi v(n, 0), w(n, 0);
  rep(i, n) cin >> v[i] >> w[i];

  for (int i = 0; i < n; i++) {
    for (int j = W; j >= w[i]; j--) {
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
    }
  }
  cout << dp[W] << endl;
}

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

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

const int INF = 1e9;

const int MAX_N = 110, MAX_W = 10010;
int dp[MAX_W];

int main() {
  int n, W;
  cin >> n >> W;
  vi v(n, 0), w(n, 0);
  rep(i, n) cin >> v[i] >> w[i];

  for (int i = 0; i < n; i++) {
    for (int j = W; j >= w[i]; j--) {
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
    }
  }
  cout << dp[W] << endl;
}
Problem p02315 (C++)

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

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  int N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert(dp.size() <= N * N);
  }
  cout << dp[0].v << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  int N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
  }
  cout << dp[0].v << endl;
  return 0;
}
Problem p02315 (C++)

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

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (P s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (P s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert((ll)dp.size() <= N * N + 1);
  }
  cout << dp[0].v << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (P s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (P s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert((ll)dp.size() <= N * N * 2);
  }
  cout << dp[0].v << endl;
  return 0;
}