Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/*
    Written by Nitrogens
    Desire for getting accepted!!
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 1e2 + 5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2)                                                           \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3)                                                       \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3        \
       << " = " << x3 << endl

ll w[maxn], v[maxn];
ll dp[100][100005];

int main() {
  // ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);

  int n, W;
  scanf("%d%d", &n, &W);

  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &w[i], &v[i]);

  memset(dp, 0, sizeof(dp));
  ll answer = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (j >= w[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
      answer = max(answer, dp[i][j]);
    }
  }
  printf("%lld\n", answer);

  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}


Fixed submission:
/*
    Written by Nitrogens
    Desire for getting accepted!!
*/
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;

const int maxn = 1e2 + 5;
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const db PI = acos(-1);
const db ERR = 1e-10;

#define Se second
#define Fi first
#define pb push_back
#define dbg(x) cout << #x << " = " << (x) << endl
#define dbg2(x1, x2)                                                           \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << endl
#define dbg3(x1, x2, x3)                                                       \
  cout << #x1 << " = " << x1 << " " << #x2 << " = " << x2 << " " << #x3        \
       << " = " << x3 << endl

ll w[maxn], v[maxn];
ll dp[105][100005];

int main() {
  // ios::sync_with_stdio(false);
  // freopen("a.txt","r",stdin);
  // freopen("b.txt","w",stdout);

  int n, W;
  scanf("%d%d", &n, &W);

  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &w[i], &v[i]);

  memset(dp, 0, sizeof(dp));
  ll answer = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (j >= w[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
      answer = max(answer, dp[i][j]);
    }
  }
  printf("%lld\n", answer);

  // cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" <<
  // endl;
  return 0;
}