Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define trace(x) cerr << #x << ": " << x << " " << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define sa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
  cin >> a[i]
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define scan()                                                                 \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  for (int i = 0; i < n; i++)                                                  \
  cin >> a[i]
#define print(a, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
  cout << a[i] << ' '
using namespace std;
template <typename T, typename U> T max(T x, U y) { return x > y ? x : y; }
template <typename T, typename U> T min(T x, U y) { return x < y ? x : y; }
ll dp[101][1001];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, w;
  cin >> n >> w;
  ll a[n], b[n];
  ll d = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    cin >> b[i];
    d += b[i];
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= d; ++j) {
      dp[i][j] = 1e18;
    }
  }
  /*for (int i = 0; i <=n; ++i)
  {
      for (int j = 0; j <=w; ++j)
      {
              trace(dp[i][j]);
      }
  }*/
  ll ans = -1;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= d; ++j) {
      if (i == 0 and j == 0)
        dp[i][j] = 0;
      else if (i == 0)
        dp[i][j] = 1e18;
      else if (j == 0)
        dp[i][j] = 0;
      else if (b[i - 1] <= j)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - b[i - 1]] + a[i - 1]);
      else
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
    }
  }

  for (int j = 0; j <= d; ++j) {
    if (dp[n][j] <= w and ans < j)
      ans = j;
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define trace(x) cerr << #x << ": " << x << " " << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define sa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
  cin >> a[i]
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define scan()                                                                 \
  int n;                                                                       \
  cin >> n;                                                                    \
  int a[n];                                                                    \
  for (int i = 0; i < n; i++)                                                  \
  cin >> a[i]
#define print(a, n)                                                            \
  for (int i = 0; i < n; i++)                                                  \
  cout << a[i] << ' '
using namespace std;
template <typename T, typename U> T max(T x, U y) { return x > y ? x : y; }
template <typename T, typename U> T min(T x, U y) { return x < y ? x : y; }
ll dp[101][100001];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, w;
  cin >> n >> w;
  ll a[n], b[n];
  ll d = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    cin >> b[i];
    d += b[i];
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= d; ++j) {
      dp[i][j] = 1e18;
    }
  }
  /*for (int i = 0; i <=n; ++i)
  {
      for (int j = 0; j <=w; ++j)
      {
              trace(dp[i][j]);
      }
  }*/
  ll ans = -1;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= d; ++j) {
      if (i == 0 and j == 0)
        dp[i][j] = 0;
      else if (i == 0)
        dp[i][j] = 1e18;
      else if (j == 0)
        dp[i][j] = 0;
      else if (b[i - 1] <= j)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - b[i - 1]] + a[i - 1]);
      else
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
    }
  }

  for (int j = 0; j <= d; ++j) {
    if (dp[n][j] <= w and ans < j)
      ans = j;
  }

  cout << ans;
}