Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
// If you are not doing well now just stick to it and learn new things one day
// you will be... Compete with yourself
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll) x.size()
#define all(x) (x.begin(), x.end())
#define trav(a, x) for (auto &a : x)
#define fr(i, a, b) for (ll i = a; i <= b; i++)
#define fr1(i, a) for (ll i = 0; i < a; i++)
#define frr(i, a, b) for (ll i = b; i >= a; i--)
#define frr1(i, a) for (ll i = a - 1; i >= 0; i--)
#define sorta(a, n) sort(a, a + n)
#define sortd(a, n) sort(a, a + n, greater<ll>())
#define sortva(a) sort(a.begin(), a.end())
#define sortvd(a) sort(a.begin(), a.end(), greater<ll>())
#define tc(t) while (t--)
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using vi = vector<ll>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vld = vector<ld>;
using pii = pair<ll, ll>;
using psi = pair<string, ll>;
using pci = pair<char, ll>;
using vpii = vector<pii>;
//}}}template*/
ll mod = 1e9 + 7;
ll const maxn = 1e6 + 5;
ll const inf = 1e18;
bool ss(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.first * b.second + a.second < b.first * a.second + b.second;
}
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll powm(ll x, ll n, ll M) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
ll modinverse(ll a, ll m) { return powm(a, m - 2, m); }
bool prime(ll x) {
  if (x <= 1)
    return false;
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
ll divisor(ll x) {
  ll cnt = 0;
  for (int i = 1; i <= sqrt(x); i++) {
    if (x % i == 0) {
      if (i != x / i)
        cnt += 2;
      else
        cnt += 1;
    }
  }
  return cnt;
}
vector<ll> sieve(ll n) {
  bool prim[n + 1];
  memset(prim, true, sizeof(prim));

  for (ll p = 2; p * p <= n; p++) {
    if (prim[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prim[i] = false;
    }
  }
  vector<ll> v;
  for (int i = 2; i <= n; i++)
    if (prim[i])
      v.push_back(i);
  return v;
}
bool check(string s) {
  ll l = 0, r = sz(s) - 1;
  while (l <= r) {
    if (s[l] != s[r])
      return false;
    l++;
    r--;
  }
  return true;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fio;
  ll n, w;
  cin >> n >> w;
  vi weight(n + 1, 0);
  vi value(n + 1, 0);
  ll dp[n + 1][w + 1];
  fr(i, 1, n) cin >> weight[i] >> value[i];
  fr(i, 0, n) {
    fr(j, 0, w) {
      if (i == 0 or j == 0)
        dp[i][j] = 0;
      else if (weight[i] <= j) {
        dp[i][j] = max(value[i] + dp[i - 1][j - weight[i]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w];
  return 0;
}

Fixed submission:
// Author:: MUKUL KUMAR RAI
// Institution:: Jalpaiguri Government Engineering College
// If you are not doing well now just stick to it and learn new things one day
// you will be... Compete with yourself
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
///*template{{{
#define sz(x) (ll) x.size()
#define all(x) (x.begin(), x.end())
#define trav(a, x) for (auto &a : x)
#define fr(i, a, b) for (ll i = a; i <= b; i++)
#define fr1(i, a) for (ll i = 0; i < a; i++)
#define frr(i, a, b) for (ll i = b; i >= a; i--)
#define frr1(i, a) for (ll i = a - 1; i >= 0; i--)
#define sorta(a, n) sort(a, a + n)
#define sortd(a, n) sort(a, a + n, greater<ll>())
#define sortva(a) sort(a.begin(), a.end())
#define sortvd(a) sort(a.begin(), a.end(), greater<ll>())
#define tc(t) while (t--)
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
using vi = vector<ll>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vld = vector<ld>;
using pii = pair<ll, ll>;
using psi = pair<string, ll>;
using pci = pair<char, ll>;
using vpii = vector<pii>;
//}}}template*/
ll mod = 1e9 + 7;
ll const maxn = 1e6 + 5;
ll const inf = 1e18;
bool ss(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return a.first * b.second + a.second < b.first * a.second + b.second;
}
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll powm(ll x, ll n, ll M) {
  ll result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
ll modinverse(ll a, ll m) { return powm(a, m - 2, m); }
bool prime(ll x) {
  if (x <= 1)
    return false;
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0)
      return false;
  return true;
}
ll divisor(ll x) {
  ll cnt = 0;
  for (int i = 1; i <= sqrt(x); i++) {
    if (x % i == 0) {
      if (i != x / i)
        cnt += 2;
      else
        cnt += 1;
    }
  }
  return cnt;
}
vector<ll> sieve(ll n) {
  bool prim[n + 1];
  memset(prim, true, sizeof(prim));

  for (ll p = 2; p * p <= n; p++) {
    if (prim[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prim[i] = false;
    }
  }
  vector<ll> v;
  for (int i = 2; i <= n; i++)
    if (prim[i])
      v.push_back(i);
  return v;
}
bool check(string s) {
  ll l = 0, r = sz(s) - 1;
  while (l <= r) {
    if (s[l] != s[r])
      return false;
    l++;
    r--;
  }
  return true;
}
int main() {

  fio;
  ll n, w;
  cin >> n >> w;
  vi weight(n + 1, 0);
  vi value(n + 1, 0);
  ll dp[n + 1][w + 1];
  fr(i, 1, n) cin >> weight[i] >> value[i];
  fr(i, 0, n) {
    fr(j, 0, w) {
      if (i == 0 or j == 0)
        dp[i][j] = 0;
      else if (weight[i] <= j) {
        dp[i][j] = max(value[i] + dp[i - 1][j - weight[i]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w];
  return 0;
}