Problem p03163 (C++)

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

#define Rushia_mywife                                                          \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define rep(i, head, n) for (int i = (head); i < n; i++)
#define F first
#define S second
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define mem(x, i) memset((x), (i), sizeof((x)))
#define odd(x) ((x)&1)
#define LG(x) (int)log2((x))

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using ld = long double;

mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

#define int long long
int qpow(int x, int powcnt, int tomod) {
  int res = 1;
  for (; powcnt; powcnt >>= 1, x = (x * x) % tomod)
    if (1 & powcnt)
      res = (res * x) % tomod;
  return (res % tomod);
}
int gcd(int aaa, int bbb) { return (bbb == 0 ? aaa : gcd(bbb, aaa % bbb)); }
int lcm(int aaa, int bbb) { return (aaa / gcd(aaa, bbb)) * bbb; }
int C(int x) { return x * (x - 1) / 2; }
// --------------------------------------**

int n, w;
int a[110];
int c[110];
int dp[110];

void solve() {
  cin >> n >> w;
  rep(i, 0, n) {
    cin >> c[i] >> a[i];
    for (int k = w; k >= 0; k--) {
      if (k - c[i] < 0)
        break;
      dp[k] = max(dp[k], dp[k - c[i]] + a[i]);
    }
  }
  cout << dp[w] << '\n';
}

signed main() {
  Rushia_mywife
  // int t; cin >> t;
  // while(t--)
  solve();
}


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

#define Rushia_mywife                                                          \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);
#define rep(i, head, n) for (int i = (head); i < n; i++)
#define F first
#define S second
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define mem(x, i) memset((x), (i), sizeof((x)))
#define odd(x) ((x)&1)
#define LG(x) (int)log2((x))

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using ld = long double;

mt19937 mt_rand(time(0));
const int mod = 1000000007;
const int hnum = 998244353;
const ld PI = acos(-1);

#define int long long
int qpow(int x, int powcnt, int tomod) {
  int res = 1;
  for (; powcnt; powcnt >>= 1, x = (x * x) % tomod)
    if (1 & powcnt)
      res = (res * x) % tomod;
  return (res % tomod);
}
int gcd(int aaa, int bbb) { return (bbb == 0 ? aaa : gcd(bbb, aaa % bbb)); }
int lcm(int aaa, int bbb) { return (aaa / gcd(aaa, bbb)) * bbb; }
int C(int x) { return x * (x - 1) / 2; }
// --------------------------------------**

int n, w;
int a[110];
int c[110];
int dp[110000];

void solve() {
  cin >> n >> w;
  rep(i, 0, n) {
    cin >> c[i] >> a[i];
    for (int k = w; k >= 0; k--) {
      if (k - c[i] < 0)
        break;
      dp[k] = max(dp[k], dp[k - c[i]] + a[i]);
    }
  }
  cout << dp[w] << '\n';
}

signed main() {
  Rushia_mywife
  // int t; cin >> t;
  // while(t--)
  solve();
}