Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define pb push_back
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ini(a, i) memset(a, i, sizeof(a))
#define forf(i, a, b) for (int i = a; i < b; i++)
#define forb(i, a, b) for (int i = b - 1; i >= a; i--)
#define debug(ar, n)                                                           \
  for (int i = 0; i < n; ++i)                                                  \
    cout << ar[i] << ' ';                                                      \
  cout << endl;
const int MAX_VALUE = LONG_MAX;
const int MIN_VALUE = LONG_MIN;
const ull U_MAX_VALUE = ULLONG_MAX;
typedef pair<int, int> pi;
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}

ll mod_power(ll x, ull y, ll p) {
  ll res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

ll find_no_of_bits(ll n) {
  ll bits = 0;
  while (n) {
    n /= 2;
    bits++;
  }
  return bits;
}

void solve() {
  ll n, w;
  cin >> n >> w;
  ll arr[n + 1][2];
  for (ll i = 1; i <= n; i++)
    cin >> arr[i][0] >> arr[i][1];
  ll dp[n + 1][w + 1];
  ini(dp, 0);
  bool picked[n];
  ini(picked, 0);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - arr[i][0] >= 0)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - arr[i][0]] + arr[i][1]);
      // cout << dp[i][j] << " ";
    }
    // cout << "\n";
  }
  cout << dp[n][w];
}
signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast_io;
  // ll t = 1;
  // cin >> t;
  // while (t--)
  // {
  solve();
  cout << "\n";
  // }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define pb push_back
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ini(a, i) memset(a, i, sizeof(a))
#define forf(i, a, b) for (int i = a; i < b; i++)
#define forb(i, a, b) for (int i = b - 1; i >= a; i--)
#define debug(ar, n)                                                           \
  for (int i = 0; i < n; ++i)                                                  \
    cout << ar[i] << ' ';                                                      \
  cout << endl;
const int MAX_VALUE = LONG_MAX;
const int MIN_VALUE = LONG_MIN;
const ull U_MAX_VALUE = ULLONG_MAX;
typedef pair<int, int> pi;
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}

ll mod_power(ll x, ull y, ll p) {
  ll res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

ll find_no_of_bits(ll n) {
  ll bits = 0;
  while (n) {
    n /= 2;
    bits++;
  }
  return bits;
}

void solve() {
  ll n, w;
  cin >> n >> w;
  ll arr[n + 1][2];
  for (ll i = 1; i <= n; i++)
    cin >> arr[i][0] >> arr[i][1];
  ll dp[n + 1][w + 1];
  ini(dp, 0);
  bool picked[n];
  ini(picked, 0);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - arr[i][0] >= 0)
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - arr[i][0]] + arr[i][1]);
      // cout << dp[i][j] << " ";
    }
    // cout << "\n";
  }
  cout << dp[n][w];
}
signed main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  fast_io;
  // ll t = 1;
  // cin >> t;
  // while (t--)
  // {
  solve();
  cout << "\n";
  // }
}