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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define szz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = (a), _for = (b); i < _for; i++)
#define REP(i, n) FOR(i, 0, (n))
#define _ << " " <<
const ll MOD = 1000000007ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll SUB(ll a, ll b) { return a - b >= 0ll ? a - b : a - b + MOD; }
inline ll ADD(ll a, ll b) { return a + b >= MOD ? a + b - MOD : a + b; }
inline ll MUL(ll a, ll b) { return a * b % MOD; }

int n, W;
ll dp[103][100003];
ll w[103], v[103];

ll f(int i, ll j) {
  if (i == 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ret = 0;
  REP(k, i) {
    if (j - w[k] >= 0) {
      ret = max(ret, v[k] + f(k, j - w[k]));
    }
  }
  dp[i][j] = ret;
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> W;
  REP(i, n) cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(ll) * 103 * 100003);
  cout << f(n, W) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define szz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = (a), _for = (b); i < _for; i++)
#define REP(i, n) FOR(i, 0, (n))
#define _ << " " <<
const ll MOD = 1000000007ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll SUB(ll a, ll b) { return a - b >= 0ll ? a - b : a - b + MOD; }
inline ll ADD(ll a, ll b) { return a + b >= MOD ? a + b - MOD : a + b; }
inline ll MUL(ll a, ll b) { return a * b % MOD; }

int n, W;
ll dp[103][100003];
ll w[103], v[103];

ll f(int i, ll j) {
  if (i == 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ret = 0;
  if (j - w[i - 1] >= 0)
    ret = max(ret, v[i - 1] + f(i - 1, j - w[i - 1]));
  ret = max(ret, f(i - 1, j));
  dp[i][j] = ret;
  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> W;
  REP(i, n) cin >> w[i] >> v[i];
  memset(dp, -1, sizeof(ll) * 103 * 100003);
  cout << f(n, W) << endl;
}
Problem p03163 (C++)

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

int abs(int a, int b) {
  if (a >= b)
    return a - b;
  else
    return b - a;
}

ll dp[2][10005];

int main() {
  int n, w;
  cin >> n >> w;
  rep(i, w + 1) dp[0][i] == 0;
  int wi;
  ll vi;
  rep(i, n) {
    cin >> wi >> vi;
    rep(j, w + 1) {
      if (j < wi)
        dp[1][j] = dp[0][j];
      else
        dp[1][j] = max(dp[0][j - wi] + vi, dp[0][j]);
    }
    rep(j, w + 1) dp[0][j] = dp[1][j];
  }
  ll max = 0;
  rep(i, w) if (dp[1][i + 1] > max) max = dp[1][i + 1];
  cout << max << endl;
}


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

int abs(int a, int b) {
  if (a >= b)
    return a - b;
  else
    return b - a;
}

ll dp[2][100005];

int main() {
  int n, w;
  cin >> n >> w;
  rep(i, w + 1) dp[0][i] == 0;
  int wi;
  ll vi;
  rep(i, n) {
    cin >> wi >> vi;
    rep(j, w + 1) {
      if (j < wi)
        dp[1][j] = dp[0][j];
      else
        dp[1][j] = max(dp[0][j - wi] + vi, dp[0][j]);
    }
    rep(j, w + 1) dp[0][j] = dp[1][j];
  }
  ll max = 0;
  rep(i, w) if (dp[1][i + 1] > max) max = dp[1][i + 1];
  cout << max << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, w;
int arr[100][2];
long long dp[100][100000];
long long inf;
long long go(int i, int sum) {

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (arr[i - 1][0] <= j)
        dp[i][j] =
            max(arr[i - 1][1] + dp[i - 1][j - arr[i - 1][0]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  return dp[n][w];
}
int main() {
  cin >> n >> w;
  inf = 5 * 100000000000000;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 2; j++)
      cin >> arr[i][j];
  long long res = go(0, 0);
  if (res > 0)
    cout << res;
  else
    cout << 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, w;
int arr[100][2];
long long dp[105][100010];
long long inf;
long long go(int i, int sum) {

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      if (arr[i - 1][0] <= j)
        dp[i][j] =
            max(arr[i - 1][1] + dp[i - 1][j - arr[i - 1][0]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  return dp[n][w];
}
int main() {
  cin >> n >> w;
  inf = 5 * 100000000000000;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 2; j++)
      cin >> arr[i][j];
  long long res = go(0, 0);
  if (res > 0)
    cout << res;
  else
    cout << 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define MOD 1000000007
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define N_MAX 100
#define W_MAX 100000
ll dp[N_MAX][W_MAX];

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N + 1), v(N + 1);
  for (int i = 1; i <= N; ++i)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= N; ++i) {
    for (int j = 0; j < w[i]; ++j) {
      chmax(dp[i][j], dp[i - 1][j]);
    }
    for (int j = w[i]; j <= W; ++j) {
      chmax(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
      chmax(dp[i][j], dp[i - 1][j]);
    }
  }
  ll ans = 0;
  // Output DP table
  //  rep(i,N+1) {
  //    rep(j,W+1) {
  //      cout << dp[i][j] << " ";
  //    }
  //    cout << endl;
  //  }
  rep(i, N + 1) chmax(ans, dp[i][W]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define MOD 1000000007
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
#define N_MAX 200
#define W_MAX 200000
ll dp[N_MAX][W_MAX];

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N + 1), v(N + 1);
  for (int i = 1; i <= N; ++i)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= N; ++i) {
    for (int j = 0; j < w[i]; ++j) {
      chmax(dp[i][j], dp[i - 1][j]);
    }
    for (int j = w[i]; j <= W; ++j) {
      chmax(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
      chmax(dp[i][j], dp[i - 1][j]);
    }
  }
  ll ans = 0;
  // Output DP table
  //  rep(i,N+1) {
  //    rep(j,W+1) {
  //      cout << dp[i][j] << " ";
  //    }
  //    cout << endl;
  //  }
  rep(i, N + 1) chmax(ans, dp[i][W]);
  cout << ans << endl;
}
Problem p03163 (C++)

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

using namespace std;
#define vi vector<ll>
typedef long long ll;
void read() { freopen("in.txt", "r", stdin); }

ll ks(ll num_items, ll cap, const vector<ll> &w, const vector<ll> &v) {
  ll dp[num_items + 1][cap + 1];

  for (ll i = 0; i <= num_items; i++) {
    dp[i][0] = 0;
  }

  for (ll i = 0; i <= cap; i++) {
    dp[0][i] = 0;
  }

  for (ll i = 1; i <= num_items; i++) {
    for (ll j = 1; j <= cap; j++) {
      ll curr_value = v[i - 1];
      ll curr_wt = w[i - 1];
      if (curr_wt > j) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(curr_value + dp[i - 1][j - curr_wt], dp[i - 1][j]);
      }
    }
  }
  return dp[num_items][cap];
}

int main() {
  read();
  ll num_items, cap;
  cin >> num_items >> cap;
  vector<ll> w(num_items), v(num_items);

  for (ll i = 0; i < num_items; i++) {
    cin >> w[i] >> v[i];
  }

  cout << ks(num_items, cap, w, v);
  return 0;
}


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

using namespace std;
#define vi vector<ll>
typedef long long ll;
void read() { freopen("in.txt", "r", stdin); }

ll ks(ll num_items, ll cap, const vector<ll> &w, const vector<ll> &v) {
  ll dp[num_items + 1][cap + 1];

  for (ll i = 0; i <= num_items; i++) {
    dp[i][0] = 0;
  }

  for (ll i = 0; i <= cap; i++) {
    dp[0][i] = 0;
  }

  for (ll i = 1; i <= num_items; i++) {
    for (ll j = 1; j <= cap; j++) {
      ll curr_value = v[i - 1];
      ll curr_wt = w[i - 1];
      if (curr_wt > j) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(curr_value + dp[i - 1][j - curr_wt], dp[i - 1][j]);
      }
    }
  }
  return dp[num_items][cap];
}

int main() {
  // read();
  ll num_items, cap;
  cin >> num_items >> cap;
  vector<ll> w(num_items), v(num_items);

  for (ll i = 0; i < num_items; i++) {
    cin >> w[i] >> v[i];
  }

  cout << ks(num_items, cap, w, v);
  return 0;
}
Problem p03163 (C++)

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

#define ll long long

using namespace std;

vector<pair<int, int>> A;

ll relaxed(int i, ll cap) {
  ll result = 0;
  while (i < A.size()) {
    if (A[i].first <= cap) {
      cap -= A[i].first;
      result += A[i].second;
      i++;
    } else {
      // result += (A[i].second * cap + A[i].first - 1) / A[i].first;
      result += A[i].second;
      break;
    }
  }
  return result;
}

ll backtrack(int i, ll cap) {
  if (i >= A.size())
    return 0;
  if (A[i].first > cap)
    return backtrack(i + 1, cap);
  ll a = A[i].second + backtrack(i + 1, cap - A[i].first);
  ll b = relaxed(i + 1, cap);
  if (a >= b)
    return a;
  return max(a, backtrack(i + 1, cap));
}

int main() {
  std::ios::sync_with_stdio(false);
  ll n, w, a, b;
  cin >> n >> w;
  A.resize(n);
  for (int i = 0; i < n; i++)
    cin >> A[i].first >> A[i].second;
  sort(A.begin(), A.end(), [](const pair<ll, ll> &x, const pair<ll, ll> &y) {
    if (x.second * y.first == y.second * x.first)
      return x.first < y.first;
    return x.second * y.first > y.second * x.first;
  });
  cout << backtrack(0, w) << "\n";

  return 0;
}


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

#define ll long long

using namespace std;

vector<pair<int, int>> A;

ll relaxed(int i, ll cap) {
  ll result = 0;
  while (i < A.size()) {
    if (A[i].first <= cap) {
      cap -= A[i].first;
      result += A[i].second;
      i++;
    } else {
      result += (A[i].second * cap + A[i].first - 1) / A[i].first;
      // result += A[i].second;
      break;
    }
  }
  return result;
}

ll backtrack(int i, ll cap) {
  if (i >= A.size())
    return 0;
  if (A[i].first > cap)
    return backtrack(i + 1, cap);
  ll a = A[i].second + backtrack(i + 1, cap - A[i].first);
  ll b = relaxed(i + 1, cap);
  if (a >= b)
    return a;
  return max(a, backtrack(i + 1, cap));
}

int main() {
  std::ios::sync_with_stdio(false);
  ll n, w, a, b;
  cin >> n >> w;
  A.resize(n);
  for (int i = 0; i < n; i++)
    cin >> A[i].first >> A[i].second;
  sort(A.begin(), A.end(), [](const pair<ll, ll> &x, const pair<ll, ll> &y) {
    if (x.second * y.first == y.second * x.first)
      return x.first < y.first;
    return x.second * y.first > y.second * x.first;
  });
  cout << backtrack(0, w) << "\n";

  return 0;
}
Problem p03163 (C++)

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

using namespace std;
using lli = long long int;

vector<lli> input(int N) {
  vector<lli> r(N);
  for (int i = 0; i < N; i++)
    cin >> r[i];
  return r;
}

int main() {
  lli N, W;
  cin >> N >> W;
  lli dp[100][100000];
  for (int i = 0; i < N; i++) {
    lli w, v;
    cin >> w >> v;
    for (lli x = 0; x <= W; x++) {
      if (x < w)
        dp[i + 1][x] = dp[i][x];
      else
        dp[i + 1][x] = max(dp[i][x], dp[i][x - w] + v);
    }
  }

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


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

using namespace std;
using lli = long long int;

vector<lli> input(int N) {
  vector<lli> r(N);
  for (int i = 0; i < N; i++)
    cin >> r[i];
  return r;
}

int main() {
  lli N, W;
  cin >> N >> W;
  lli dp[110][100010];
  for (int i = 0; i < N; i++) {
    lli w, v;
    cin >> w >> v;
    for (lli x = 0; x <= W; x++) {
      if (x < w)
        dp[i + 1][x] = dp[i][x];
      else
        dp[i + 1][x] = max(dp[i][x], dp[i][x - w] + v);
    }
  }

  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
bool compare(pair<int, int> p1, pair<int, int> p2) {
  if (p1.first == p2.first)
    return p1.second < p2.second;
  return p1.first < p2.first;
}
int32_t main() {
  IOS
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, W, w, v;
  cin >> n >> W;
  vector<pair<int, int>> item;
  item.push_back({0, 0});
  for (int i = 0; i < n; i++) {
    cin >> w >> v;
    item.push_back(make_pair(w, v));
  }
  sort(item.begin(), item.end(), compare);
  // for(int i=1;i<=n;i++) cout<<item[i].first<<" "<<item[i].second<<endl;
  int dp[n + 1][W + 1];
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= W; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - item[j].first >= 0)
        dp[j][i] =
            max(dp[j - 1][i], item[j].second + dp[j - 1][i - item[j].first]);
      else
        dp[j][i] = dp[j - 1][i];
    }
  }
  // for(int i=0;i<=n;i++) {for(int j=0;j<=W;j++) cout<<dp[i][j]<<" ";
  // cout<<endl;}
  cout << dp[n][W];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
bool compare(pair<int, int> p1, pair<int, int> p2) {
  if (p1.first == p2.first)
    return p1.second < p2.second;
  return p1.first < p2.first;
}
int32_t main() {
  IOS
      // #ifndef ONLINE_JUDGE
      // freopen("input.txt", "r", stdin);
      // freopen("output.txt", "w", stdout);
      // #endif
      int n,
      W, w, v;
  cin >> n >> W;
  vector<pair<int, int>> item;
  item.push_back({0, 0});
  for (int i = 0; i < n; i++) {
    cin >> w >> v;
    item.push_back(make_pair(w, v));
  }
  sort(item.begin(), item.end(), compare);
  // for(int i=1;i<=n;i++) cout<<item[i].first<<" "<<item[i].second<<endl;
  int dp[n + 1][W + 1];
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= W; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= n; j++) {
      if (i - item[j].first >= 0)
        dp[j][i] =
            max(dp[j - 1][i], item[j].second + dp[j - 1][i - item[j].first]);
      else
        dp[j][i] = dp[j - 1][i];
    }
  }
  // for(int i=0;i<=n;i++) {for(int j=0;j<=W;j++) cout<<dp[i][j]<<" ";
  // cout<<endl;}
  cout << dp[n][W];
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

int main() {
  int n, w;
  cin >> n >> w;
  pair<long long, long long> arr[n];
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    arr[i] = make_pair(a, b);
  }
  long long dp[n + 1][w + 1];
  for (int i = 0; i <= n; ++i) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= w; ++i) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (j - arr[i - 1].first >= 0) {
        dp[i][j] = max(arr[i - 1].second + dp[i - 1][j - arr[i - 1].first],
                       dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // long long val = 0;
  // for (int i = 0; i < n + 1; ++i) {
  //     for (int j = 0; j <= w; ++j)
  //     {
  //         val = max(val, dp[i][j]);
  //         cout << dp[i][j] << "\t";
  //     }
  //     cout << endl;
  // }
  cout << dp[n][w] << endl;
  return 1;
}


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

using namespace std;

int main() {
  int n, w;
  cin >> n >> w;
  pair<long long, long long> arr[n];
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    arr[i] = make_pair(a, b);
  }
  long long dp[n + 1][w + 1];
  for (int i = 0; i <= n; ++i) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= w; ++i) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (j - arr[i - 1].first >= 0) {
        dp[i][j] = max(arr[i - 1].second + dp[i - 1][j - arr[i - 1].first],
                       dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // long long val = 0;
  // for (int i = 0; i < n + 1; ++i) {
  //     for (int j = 0; j <= w; ++j)
  //     {
  //         val = max(val, dp[i][j]);
  //         cout << dp[i][j] << "\t";
  //     }
  //     cout << endl;
  // }
  cout << dp[n][w] << endl;
  return 0;
}
Problem p03163 (C++)

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

using namespace std;
long long dp[2][(unsigned int)(1e5 + 1)];
int main() {
  int n, w;
  cin >> n >> w;
  pair<long long, long long> arr[n];
  for (int i = 0; i < n; ++i) {
    long long a, b;
    cin >> a >> b;
    arr[i] = make_pair(a, b);
  }
  for (int i = 0; i <= 1; ++i) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= w; ++i) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (j - arr[i - 1].first >= 0) {
        dp[i % 2][j] =
            max(arr[i - 1].second + dp[(i + 1) % 2][j - arr[i - 1].first],
                dp[(i + 1) % 2][j]);
      } else {
        dp[i % 2][j] = dp[(i + 1) % 2][j];
      }
      // cout << dp[i % 2 ][j] << "\t";
    }
    // cout << endl;
  }
  // long long val = 0;
  // for (int i = 0; i < n + 1; ++i) {
  //     for (int j = 0; j <= w; ++j)
  //     {
  //         val = max(val, dp[i][j]);
  //         cout << dp[i][j] << "\t";
  //     }
  //     cout << endl;
  // }
  cout << dp[n % 2][w] << endl;
  return 1;
}

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

using namespace std;
long long dp[2][(unsigned int)(1e5 + 1)];
int main() {
  int n, w;
  cin >> n >> w;
  pair<long long, long long> arr[n];
  for (int i = 0; i < n; ++i) {
    long long a, b;
    cin >> a >> b;
    arr[i] = make_pair(a, b);
  }
  for (int i = 0; i <= 1; ++i) {
    dp[i][0] = 0;
  }
  for (int i = 0; i <= w; ++i) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (j - arr[i - 1].first >= 0) {
        dp[i % 2][j] =
            max(arr[i - 1].second + dp[(i + 1) % 2][j - arr[i - 1].first],
                dp[(i + 1) % 2][j]);
      } else {
        dp[i % 2][j] = dp[(i + 1) % 2][j];
      }
      // cout << dp[i % 2 ][j] << "\t";
    }
    // cout << endl;
  }
  // long long val = 0;
  // for (int i = 0; i < n + 1; ++i) {
  //     for (int j = 0; j <= w; ++j)
  //     {
  //         val = max(val, dp[i][j]);
  //         cout << dp[i][j] << "\t";
  //     }
  //     cout << endl;
  // }
  cout << dp[n % 2][w] << endl;
  return 0;
}
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
#!/usr/bin/env python3
def main():
    import numpy as np

    N, W = map(int, input().split())
    products = [list(map(int, input().split())) for _ in range(N)]

    dp = np.zeros((N * 2, (W + 1)), dtype=np.int64)
    for n in range(N):
        for w in range(W + 1):
            can_w, can_v = products[n]
            if w >= can_w:
                dp[n + 1][w] = max(dp[n + 1][w], dp[n][w - can_w] + can_v)
            dp[n + 1][w] = max(dp[n + 1][w], dp[n][w])
    print(np.max(dp))


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
def main():
    import numpy as np

    N, W = map(int, input().split())
    products = [list(map(int, input().split())) for _ in range(N)]

    dp = np.zeros(W + 1, dtype=np.int64)
    for can_w, can_v in products:
        dp[can_w:] = np.maximum(dp[can_w:], dp[:-can_w] + can_v)

    print(np.max(dp))


if __name__ == "__main__":
    main()
Problem p03163 (Python)

Buggy submission (status: Runtime Error):
N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 110 for _ in range(100100)]


for i in range(N):
    for sum_w in range(W + 1):
        if wv[i][0] <= sum_w:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])


Fixed submission:
N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 100100 for _ in range(110)]


for i in range(N):
    for sum_w in range(W + 1):
        if wv[i][0] <= sum_w:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;
using pll = pair<long long, long long>;
typedef long long ll;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int N;
  long W;
  long long weight[110];
  long long value[110];

  long long dp[110][110] = {0};

  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> weight[i] >> value[i];

  for (int i = 0; i < N; ++i) {
    for (int sum_w = 0; sum_w <= W; ++sum_w) {

      if (sum_w >= weight[i]) {
        chmax(dp[i + 1][sum_w], value[i] + dp[i][sum_w - weight[i]]);
      }

      chmax(dp[i + 1][sum_w], dp[i][sum_w]);
    }
  }

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


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;
using pll = pair<long long, long long>;
typedef long long ll;
#define min(a, b) ((a) < (b) ? (a) : (b))
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int N;
  long W;
  long long weight[110];
  long long value[110];

  long long dp[110][100010] = {0};

  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> weight[i] >> value[i];

  for (int i = 0; i < N; ++i) {
    for (int sum_w = 0; sum_w <= W; ++sum_w) {

      if (sum_w >= weight[i]) {
        chmax(dp[i + 1][sum_w], value[i] + dp[i][sum_w - weight[i]]);
      }

      chmax(dp[i + 1][sum_w], dp[i][sum_w]);
    }
  }

  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using ld = long double;
using Vl = vector<ll>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vs = vector<string>;

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

ll N, W;
Vl w(N), v(N);
ll dp[102][100002]; // 0

int main() {
  cin >> N >> W;
  w.resize(N);
  v.resize(N);
  REP(i, N) cin >> w[i] >> v[i];

  REP(i, N) REP(wi, W + 1) {
    if (wi >= w[i]) {
      dp[i + 1][wi] = max(dp[i][wi - w[i]] + v[i], dp[i][wi]);
    } else {
      dp[i + i][wi] = dp[i][wi];
    }
  }

  cout << dp[N][W];

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using ld = long double;
using Vl = vector<ll>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vs = vector<string>;

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

ll N, W;
Vl w(N), v(N);
ll dp[102][100002]; // 0

int main() {
  cin >> N >> W;
  w.resize(N);
  v.resize(N);
  REP(i, N) cin >> w[i] >> v[i];

  REP(i, N) REP(wi, W + 1) {
    if (wi >= w[i]) {
      dp[i + 1][wi] = max(dp[i][wi - w[i]] + v[i], dp[i][wi]);
    } else {
      dp[i + 1][wi] = dp[i][wi];
    }
  }

  cout << dp[N][W];

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k;
int a[N];
int b[N];
long long dp[105][N];

long long f(int idx, int weight) {
  if (weight > k)
    return -1e9;
  if (idx >= n)
    return 0;
  long long &ret = dp[idx][weight];
  ret = max(f(idx + 1, weight + a[idx]) + b[idx], f(idx + 1, weight));
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  memset(dp, -1, sizeof dp);
  cout << f(0, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k;
int a[N];
int b[N];
long long dp[105][N];

long long f(int idx, int weight) {
  if (weight > k)
    return -1e9;
  if (idx >= n)
    return 0;
  long long &ret = dp[idx][weight];
  if (ret != -1)
    return ret;
  ret = max(f(idx + 1, weight + a[idx]) + b[idx], f(idx + 1, weight));
  return ret;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  memset(dp, -1, sizeof dp);
  cout << f(0, 0) << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define endl '\n'
#define N 100005
using namespace std;
ll w[N], v[N], n, have;
ll dp[105][N];
ll rec(int can, int i) {
  if (i == n)
    return 0;
  if (dp[can][i] != -1)
    return dp[can][i];
  ll ans = 0;
  if (can >= w[i])
    ans = max(v[i] + rec(can - w[i], i + 1), rec(can, i + 1));
  else
    ans = rec(can, i + 1);
  dp[can][i] = ans;
  return dp[can][i];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> have;
  for (int i = 0; i <= have; i++) {
    for (int j = 0; j <= n; j++)
      dp[i][j] = -1;
  }

  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  cout << rec(have, 0) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define endl '\n'
#define N 100005
using namespace std;
ll w[N], v[N], n, have;
ll dp[N][105];
ll rec(int can, int i) {
  if (i == n)
    return 0;
  if (dp[can][i] != -1)
    return dp[can][i];
  ll ans = 0;
  if (can >= w[i])
    ans = max(v[i] + rec(can - w[i], i + 1), rec(can, i + 1));
  else
    ans = rec(can, i + 1);
  dp[can][i] = ans;
  return dp[can][i];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> have;
  for (int i = 0; i <= have; i++) {
    for (int j = 0; j <= n; j++)
      dp[i][j] = -1;
  }

  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  cout << rec(have, 0) << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9;

int main() {
  int N, W, w[100], v[100];
  cin >> N >> W;
  for (int i(0); i < N; i++)
    cin >> w[i] >> v[i];
  ll dp[101][10001] = {
      0}; // i番目までを使って重さw以下になるように選んだ時の価値の最大値
  for (int i(0); i < N; i++) {
    for (int j(0); j <= W; j++) {
      if (w[i] > j)
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }
  }
  cout << dp[N][W] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9;

int main() {
  int N, W, w[100], v[100];
  cin >> N >> W;
  for (int i(0); i < N; i++)
    cin >> w[i] >> v[i];
  ll dp[101][100001] = {
      0}; // i番目までを使って重さw以下になるように選んだ時の価値の最大値
  for (int i(0); i < N; i++) {
    for (int j(0); j <= W; j++) {
      if (w[i] > j)
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;
int w[101], v[101];
ll dp[101][100001] = {0};

int main() {
  int N, W;
  cin >> N >> W;
  for (int i(1); i <= N; i++)
    cin >> w[i] >> v[i];
  for (int ni(1); ni <= N; ni++) {
    for (int wi(0); wi <= W; wi++) {
      if (wi - w[ni] >= 0) {
        dp[ni][wi] = max(dp[ni - 1][wi], dp[ni - 1][wi - w[ni]] + v[ni]);
      } else {
        dp[ni][wi] = dp[ni - 1][wi];
      }
    }
  }
  for (int wi(0); wi <= W; wi++) {
    for (int ni(0); ni <= N; ni++) {
      cerr << dp[ni][wi] << " ";
    }
    cerr << endl;
  }
  cout << dp[N][W] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;
int w[101], v[101];
ll dp[101][100001] = {0};

int main() {
  int N, W;
  cin >> N >> W;
  for (int i(1); i <= N; i++)
    cin >> w[i] >> v[i];
  for (int ni(1); ni <= N; ni++) {
    for (int wi(0); wi <= W; wi++) {
      if (wi - w[ni] >= 0) {
        dp[ni][wi] = max(dp[ni - 1][wi], dp[ni - 1][wi - w[ni]] + v[ni]);
      } else {
        dp[ni][wi] = dp[ni - 1][wi];
      }
    }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

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

#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pf push_front
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
const int MAXN = 1e2 + 2;
const int MAX = 1e5 + 5;
int N, W, w[MAXN], b[MAXN];
ll pd[MAXN];

ll solve() {
  REP(i, 1, N)
  PER(j, W, 1) if (w[i] <= j) pd[j] = max(pd[j], pd[j - w[i]] + b[i]);
  return pd[W];
}

int main(int argc, char **argv) {
  scanf("%d%d", &N, &W);
  REP(i, 1, N) scanf("%d%d", &w[i], &b[i]);
  printf("%lld\n", solve());
  return 0;
}


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

#define REP(i, a, b) for (int i = (a); i <= (b); i++)
#define PER(i, a, b) for (int i = (a); i >= (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(S) (S).begin(), (S).end()
#define pf push_front
#define pb push_back
#define mk make_pair
#define S second
#define F first
typedef long long ll;
typedef long double lf;
typedef pair<int, int> ii;
const int MAXN = 1e2 + 2;
const int MAX = 1e5 + 5;
int N, W, w[MAXN], b[MAXN];
ll pd[MAX];

ll solve() {
  REP(i, 1, N)
  PER(j, W, 1) if (w[i] <= j) pd[j] = max(pd[j], pd[j - w[i]] + b[i]);
  return pd[W];
}

int main(int argc, char **argv) {
  scanf("%d%d", &N, &W);
  REP(i, 1, N) scanf("%d%d", &w[i], &b[i]);
  printf("%lld\n", solve());
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vll vector<ll>
ll val[105], wt[105], ti[105][105];

ll func(ll len, ll w) {
  if (len <= 0 || w <= 0)
    return 0;

  if (ti[len][w] != -1)
    return ti[len][w];

  if (wt[len] <= w) {
    return ti[len][w] =
               max(val[len] + func(len - 1, w - wt[len]), func(len - 1, w));
  } else
    return ti[len][w] = func(len - 1, w);
}

int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, w;
  cin >> n >> w;
  memset(ti, -1, sizeof ti);
  for (ll i = 1; i <= n; i++)
    cin >> wt[i] >> val[i];

  ll ans = func(n, w);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vll vector<ll>
ll val[105], wt[105], ti[105][100005];

ll func(ll len, ll w) {
  if (len <= 0 || w <= 0)
    return 0;

  if (ti[len][w] != -1)
    return ti[len][w];

  if (wt[len] <= w) {
    return ti[len][w] =
               max(val[len] + func(len - 1, w - wt[len]), func(len - 1, w));
  } else
    return ti[len][w] = func(len - 1, w);
}

int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, w;
  cin >> n >> w;
  memset(ti, -1, sizeof ti);
  for (ll i = 1; i <= n; i++)
    cin >> wt[i] >> val[i];

  ll ans = func(n, w);
  cout << ans << endl;

  return 0;
}
Problem p03163 (C++)

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

using namespace std;

typedef long long ll;

ll w[100], v[100], dp[101][100001];

int main() {
  int n, W;
  cin >> n >> W;
  for (int i = 0; i < n; ++i)
    cin >> w[i] >> v[i];

  for (int i = 0; i < n; ++i) {
    for (int wi = 0; wi <= W; ++wi) {
      dp[i + 1][wi] = max(dp[i + 1][wi], dp[i][wi]);
      dp[i + 1][wi + w[i]] = max(dp[i + 1][wi + w[i]], dp[i][wi] + v[i]);
    }
  }

  ll ans = 0LL;
  for (int wi = 0; wi <= W; ++wi) {
    ans = max(ans, dp[n][wi]);
  }
  cout << ans << "\n";
}

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

using namespace std;

typedef long long ll;

ll w[100], v[100], dp[101][100001];

int main() {
  int n, W;
  cin >> n >> W;
  for (int i = 0; i < n; ++i)
    cin >> w[i] >> v[i];

  for (int i = 0; i < n; ++i) {
    for (int wi = 0; wi <= W; ++wi) {
      dp[i + 1][wi] = max(dp[i + 1][wi], dp[i][wi]);
      if (wi + w[i] <= W)
        dp[i + 1][wi + w[i]] = max(dp[i + 1][wi + w[i]], dp[i][wi] + v[i]);
    }
  }

  ll ans = 0LL;
  for (int wi = 0; wi <= W; ++wi) {
    ans = max(ans, dp[n][wi]);
  }
  cout << ans << "\n";
}
Problem p03163 (C++)

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

using namespace std;

typedef long long ll;

ll dp[100][100005];

int main(int argc, char *argv[]) {
  int N, W, w[100005], v[100005];

  cin >> N >> W;

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

  cout << dp[N][W] << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

typedef long long ll;

ll dp[105][100005];

int main(int argc, char *argv[]) {
  int N, W, w[100005], v[100005];

  cin >> N >> W;

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

  cout << dp[N][W] << endl;

  return 0;
}
Problem p03163 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, w;
  cin >> n >> w;
  ll dp[n + 1][w + 1];
  ll a[n + 1], b[n + 1];
  for (ll i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  for (ll i = 0; i <= w; i++)
    dp[0][i] = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j <= w; j++) {
      // if(j>=a[i])
      dp[i][j] = max((b[i] + dp[i - 1][j - a[i]]), dp[i - 1][j]);
    }
  }
  cout << dp[n][w];
  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, w;
  cin >> n >> w;
  ll dp[n + 1][w + 1];
  ll a[n + 1], b[n + 1];
  for (ll i = 1; i <= n; i++)
    cin >> a[i] >> b[i];
  for (ll i = 0; i <= w; i++)
    dp[0][i] = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j <= w; j++) {
      if (j >= a[i])
        dp[i][j] = max((b[i] + dp[i - 1][j - a[i]]), dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 5;
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  int W;
  cin >> W;
  vector<int> w(n + 1);
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  ll dp[n + 1][W + 1];
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      dp[i][j] = 0;
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 5;
int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  int W;
  cin >> W;
  vector<int> w(n + 1);
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  ll dp[n + 1][W + 1];
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= W; j++) {
      dp[i][j] = 0;
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (w[i] <= j)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][W] << endl;
  return 0;
}
Problem p03163 (C++)

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

// int h[100001];

unsigned long long dp[100001] = {};
/*
int solve(int i){
        if(i == 0) return 0;
        if(i == 1) return 0;
        if(memo[i] != -1) return memo[i];
        int m = INT_MAX;
        for(int d = 1; d <= k; d++){
                if(i - d < 0) break;
                m = min(solve(i-d, k)+abs(h[i]-h[i-d]), m);
        }
        return memo[i] = m;
}
*/

int main() {

  int n;
  cin >> n;

  int w;
  cin >> w;

  pair<int, int> wv[10];
  for (int i = 0; i < n; i++) {
    cin >> wv[i].first >> wv[i].second;
  }

  dp[0] = 0;

  unsigned long long ans = 0;

  for (int j = 0; j < n; j++) {
    for (int i = w - wv[j].first; i >= 0; i--) {
      dp[i + wv[j].first] = max(dp[i] + wv[j].second, dp[i + wv[j].first]);
      ans = max(ans, dp[i + wv[j].first]);
    }
  }

  cout << ans << endl;

  return 0;
}


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

// int h[100001];

unsigned long long dp[100001] = {};
/*
int solve(int i){
        if(i == 0) return 0;
        if(i == 1) return 0;
        if(memo[i] != -1) return memo[i];
        int m = INT_MAX;
        for(int d = 1; d <= k; d++){
                if(i - d < 0) break;
                m = min(solve(i-d, k)+abs(h[i]-h[i-d]), m);
        }
        return memo[i] = m;
}
*/

int main() {

  int n;
  cin >> n;

  int w;
  cin >> w;

  pair<int, int> wv[100];
  for (int i = 0; i < n; i++) {
    cin >> wv[i].first >> wv[i].second;
  }

  dp[0] = 0;

  unsigned long long ans = 0;

  for (int j = 0; j < n; j++) {
    for (int i = w - wv[j].first; i >= 0; i--) {
      dp[i + wv[j].first] = max(dp[i] + wv[j].second, dp[i + wv[j].first]);
      ans = max(ans, dp[i + wv[j].first]);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03163 (C++)

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

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

  // dp[i][j]: i 番目までの品物から重さの和が j 以下になるように
  //           選んだときの価値の最大値
  long dp[101][10001];
  for (int j = 0; j <= W; ++j)
    dp[0][j] = 0;
  for (int i = 1; i <= N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j >= w[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[N][W] << endl;

  return 0;
}

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

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

  // dp[i][j]: i 番目までの品物から重さの和が j 以下になるように
  //           選んだときの価値の最大値
  long dp[101][100001];
  for (int j = 0; j <= W; ++j)
    dp[0][j] = 0;
  for (int i = 1; i <= N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j >= w[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[N][W] << endl;

  return 0;
}
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
N, M = map(int, input().split())
W, V = [], []
for n in range(N):
    w, v = map(int, input().split())
    W.append(w)
    V.append(v)


def f(n, m):
    if n == 0 or m <= 0:
        return 0
    elif m < W[n - 1]:
        return f(n - 1, m)
    else:
        return max(f(n - 1, m), f(n - 1, m - W[n - 1]) + V[n - 1])


print(f(N, M))


Fixed submission:
N, M = map(int, input().split())
W, V = [], []
for n in range(N):
    w, v = map(int, input().split())
    W.append(w)
    V.append(v)
f = [[0 for m in range(M + 1)] for n in range(N + 1)]
for n in range(N):
    for m in range(M + 1):
        if m < W[n]:
            f[n + 1][m] = f[n][m]
        else:
            f[n + 1][m] = max(f[n][m], f[n][m - W[n]] + V[n])
print(f[N][M])
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
import numpy as np

n, W = map(int, input().split())
wv = np.array([list(map(int, input().split())) for i in range(n)])

dp = np.zeros((n + 1, W + 1), dtype=np.int)


def chmax(a, b):
    if a > b:
        return a
    else:
        return b


for i in range(n):
    for sum_w in range(W + 1):
        if sum_w >= wv[i, 0]:
            dp[i + 1][sum_w] = chmax(
                dp[i + 1][sum_w], dp[i][sum_w - wv[i, 0]] + wv[i, 1]
            )
        dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w])

print(int(dp[n, W]))


Fixed submission:
import numpy as np

n, W = map(int, input().split())
wv = np.array([list(map(int, input().split())) for i in range(n)])

dp = np.zeros((n + 1, W + 1), dtype=np.int)


def chmax(a, b):
    if a > b:
        return a
    else:
        return b


# # 遅い
# for i in range(n):
#     for sum_w in range(W + 1):
#         if sum_w >= wv[i, 0]:
#             dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w - wv[i, 0]] + wv[i, 1])
#         dp[i + 1][sum_w] = chmax(dp[i + 1][sum_w], dp[i][sum_w])

for i in range(0, n):
    w, v = wv[i, :]
    dp[i + 1, w:] = np.maximum(dp[i, w:], dp[i, :-w] + v)
    dp[i + 1, :] = np.maximum(dp[i + 1, :], dp[i, :])

print(int(dp[n, W]))
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    N, W = map(int, input().split())
    wi, vi = map(int, input().split())
    dp = [0] * wi + [vi] * (W + 1 - wi)
    for _ in range(1, N):
        wi, vi = map(int, input().split())
        dp[wi:] = [max(i + vi, j) for i, j in zip(dp, dp[wi:])]
    print(dp[-1])


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N, W = map(int, input().split())
    wi, vi = map(int, input().split())
    dp = [0] * wi + [vi] * (W + 1 - wi)
    for _ in range(1, N):
        wi, vi = map(int, input().split())
        for i in range(W, wi - 1, -1):
            t = dp[i - wi] + vi
            if t > dp[i]:
                dp[i] = t
    print(dp[-1])


if __name__ == "__main__":
    main()
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
N, W = [int(_) for _ in input().split()]
WV = sorted([[int(_) for _ in input().split()] for _ in range(N)], reverse=True)
dp = {}
dp[0] = 0
for w, v in WV:
    dp_o = dp.copy()
    for k in dp_o:
        if k + w <= W:
            dp[k + w] = max(dp_o.get(k + w, 0), dp_o[k] + v)
print(max([v for k, v in dp.items()]))


Fixed submission:
N, W = [int(_) for _ in input().split()]
WV = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = 0
for w, v in WV:
    dp_o = dp.copy()
    for k in dp_o:
        if k + w <= W:
            dp[k + w] = max(dp_o.get(k + w, 0), dp_o[k] + v)
print(max([v for k, v in dp.items()]))
Problem p03163 (Python)

Buggy submission (status: Runtime Error):
# using dp with memoization, TLE
from sys import stdin

N, W = map(int, input().split())
m = stdin.read().splitlines()
s = [[int(i) for i in line.split()] for line in m]

dp = [0] * (W + 1)
next_dp = [0] * (W + 1)
for n in range(1, N + 1):
    for w in range(0, W + 1):
        if w >= s[n - 1][0]:
            next_dp[w] = max(next_dp[w], dp[w - s[n - 1][0]] + s[n - 1][1])
    dp = next_dp.copy()
print(dp[W])


Fixed submission:
# using dp with memoization, TLE
from sys import stdin

N, W = map(int, input().split())
m = stdin.read().splitlines()
s = [[int(i) for i in line.split()] for line in m]

dp = [0] * (W + 1)
next_dp = [0] * (W + 1)
for n in range(1, N + 1):
    for w in range(0, W + 1):
        if w >= s[n - 1][0]:
            next_dp[w] = max(next_dp[w], dp[w], dp[w - s[n - 1][0]] + s[n - 1][1])
    dp = next_dp[:]
print(dp[W])
Problem p03163 (Python)

Buggy submission (status: Time Limit Exceeded):
def knapsack1(weight_value, w):
    cur = [0] * (w + 1)
    nxt = cur.copy()

    for weight, value in weight_value:
        for i in range(1, weight):
            nxt[i] = cur[i]
        for i in range(weight, w + 1):
            nxt[i] = max(cur[i], cur[i - weight] + value)
        cur, nxt = nxt, cur
    return cur[w]


def main():
    n, w = [int(x) for x in input().split()]
    weight_value = [None] * n
    for i in range(n):
        weight_value[i] = [int(x) for x in input().split()]
    return knapsack1(weight_value, w)


print(main())


Fixed submission:
def knapsack1(weight_value, w):
    cur = [0] * (w + 1)
    nxt = [0] * (w + 1)

    for weight, value in weight_value:
        for i in range(1, weight):
            nxt[i] = cur[i]
        for i in range(weight, w + 1):
            nxt[i] = max(cur[i], cur[i - weight] + value)
        cur, nxt = nxt, cur
    return cur[w]


def main():
    n, w = [int(x) for x in input().split()]
    weight_value = [None] * n
    for i in range(n):
        weight_value[i] = [int(x) for x in input().split()]
    return knapsack1(weight_value, w)


print(main())