Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define M 1000000007
#define pb push_back
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define NAYAN                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);

int dp[102][10005];
int n, w;
int val[100001], wt[100001];

/*int solve(int i,int V)
{
    //cout << i << " " << V  << endl;
    if(V==0)
        return 0;
    if(V<0 || i==n)
        return 99999999999999;
    if(dp[i][V]!=-1)
        return dp[i][V];
    int x=solve(i+1,V);
    int y=wt[i]+solve(i+1,V-val[i]);
    dp[i][V]=min(x,y);
    return dp[i][V];

}*/
signed main() {
  boost
          // #ifndef ONLINE_JUDGE
          // NAYAN
          //  #endif

          // int n;
          cin >>
      n >> w;
  //  cout << n << endl;

  int i, j;
  for (i = 1; i <= n; i++) {
    cin >> wt[i] >> val[i];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= 100000; j++) {
      dp[i][j] = 1000000000000;
    }
  }
  dp[0][0] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 100000; j >= 0; j--) {
      // dp[i][j]=999999999999;
      if (val[i] <= j)
        dp[i][j] = min(dp[i - 1][j], wt[i] + dp[i - 1][j - val[i]]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  int ans;
  for (i = 100000; i >= 0; i--) {

    ans = dp[n][i];
    //  cout << ans << " " << w << endl;
    if (ans <= w)
      break;
  }
  cout << i << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define M 1000000007
#define pb push_back
#define boost                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define NAYAN                                                                  \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);

int dp[102][100005];
int n, w;
int val[100001], wt[100001];

/*int solve(int i,int V)
{
    //cout << i << " " << V  << endl;
    if(V==0)
        return 0;
    if(V<0 || i==n)
        return 99999999999999;
    if(dp[i][V]!=-1)
        return dp[i][V];
    int x=solve(i+1,V);
    int y=wt[i]+solve(i+1,V-val[i]);
    dp[i][V]=min(x,y);
    return dp[i][V];

}*/
signed main() {
  boost
          // #ifndef ONLINE_JUDGE
          // NAYAN
          //  #endif

          // int n;
          cin >>
      n >> w;
  //  cout << n << endl;

  int i, j;
  for (i = 1; i <= n; i++) {
    cin >> wt[i] >> val[i];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= 100000; j++) {
      dp[i][j] = 1000000000000;
    }
  }
  dp[0][0] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 100000; j >= 0; j--) {
      // dp[i][j]=999999999999;
      if (val[i] <= j)
        dp[i][j] = min(dp[i - 1][j], wt[i] + dp[i - 1][j - val[i]]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  int ans;
  for (i = 100000; i >= 0; i--) {

    ans = dp[n][i];
    //  cout << ans << " " << w << endl;
    if (ans <= w)
      break;
  }
  cout << i << endl;
}