Problem p03163 (C++)

Buggy submission (status: Runtime Error):


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

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int long long
#define endl '\n'
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define vii vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(v) v.begin(), v.end()
#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))
#define M 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define prvec(v)                                                               \
  loop(i_u_j, 0, v.size()) cout << v[i_u_j] << " ";                            \
  cout << endl;
#define prarr(arr, n)                                                          \
  loop(i_u_j, 0, n) cout << arr[i_u_j] << " ";                                 \
  cout << endl;
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //
const int N = 107;
int n;
int wt[N], val[N], dp[N][100007];

int getval(int it, int mxw) {
  if (dp[it][mxw] != -1)
    return dp[it][mxw];

  if (mxw == 0 || it == n)
    return dp[it][mxw] = 0;
  int a = 0, b = 0;
  if (wt[it] <= mxw) {
    a = val[it] + getval(it + 1, mxw - wt[it]);
  }
  b = getval(it + 1, mxw);
  return dp[it][mxw] = max(a, b);
}
void solve() {
  int mxw;
  cin >> n >> mxw;
  loop(i, 0, n) { cin >> wt[i] >> val[i]; }
  mset(dp, -1);
  cout << getval(0, mxw);
}

int32_t main() {
  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int t = 1;
  // cin>>t;

  while (t--)
    solve();
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //


Fixed submission:


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

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int long long
#define endl '\n'
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define vii vector<int>
#define pii pair<int, int>
#define vpii vector<pii>
#define all(v) v.begin(), v.end()
#define loop(i, s, e) for (int i = s; i < e; i++)
#define rloop(i, e, s) for (int i = e; i >= s; i--)
#define mset(a, f) memset(a, f, sizeof(a))
#define M 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define prvec(v)                                                               \
  loop(i_u_j, 0, v.size()) cout << v[i_u_j] << " ";                            \
  cout << endl;
#define prarr(arr, n)                                                          \
  loop(i_u_j, 0, n) cout << arr[i_u_j] << " ";                                 \
  cout << endl;
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //
const int N = 107;
int n;
int wt[N], val[N], dp[N][100007];

int getval(int it, int mxw) {
  if (dp[it][mxw] != -1)
    return dp[it][mxw];

  if (mxw == 0 || it == n)
    return dp[it][mxw] = 0;
  int a = 0, b = 0;
  if (wt[it] <= mxw) {
    a = val[it] + getval(it + 1, mxw - wt[it]);
  }
  b = getval(it + 1, mxw);
  return dp[it][mxw] = max(a, b);
}
void solve() {
  int mxw;
  cin >> n >> mxw;
  loop(i, 0, n) { cin >> wt[i] >> val[i]; }
  mset(dp, -1);
  cout << getval(0, mxw);
}

int32_t main() {
  fastio;
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt",  "r",  stdin);
  //     freopen("output.txt", "w", stdout);
  //  #endif

  int t = 1;
  // cin>>t;

  while (t--)
    solve();
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //