Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(x) for (auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vii vector<pii>
#define mii map<int, int>
#define mll map<ll, ll>

using namespace std;

void solve() {
  int n, w;
  cin >> n >> w;
  int wt[n], val[n];
  rep(i, n) cin >> wt[i] >> val[i];
  int dp[n + 1][w + 1];
  memset(dp, 0, sizeof dp);
  dp[0][0] = 0;

  repA(i, 0, n) {
    repA(j, 0, w) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }
      if (j - wt[i - 1] >= 0)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt[i - 1]] + val[i - 1]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[3][8];
}

signed main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}


Fixed submission:
// R<3S
#include <bits/stdc++.h>
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(x) for (auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define int long long
#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vii vector<pii>
#define mii map<int, int>
#define mll map<ll, ll>

using namespace std;

void solve() {
  int n, w;
  cin >> n >> w;
  int wt[n], val[n];
  rep(i, n) cin >> wt[i] >> val[i];
  int dp[n + 1][w + 1];
  memset(dp, 0, sizeof dp);
  dp[0][0] = 0;

  repA(i, 0, n) {
    repA(j, 0, w) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }
      if (j - wt[i - 1] >= 0)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wt[i - 1]] + val[i - 1]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w];
}

signed main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}