Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
//       THIS IS MY NINJA WAY
#include <algorithm>
#include <bits/stdc++.h>
#include <ctime>
#include <vector>

#define str string
#define dbl double
#define ll long long
#define vl vector<ll>
#define vs vector<str>
#define pll pair<ll, ll>
#define vll vector<pll>
#define sl set<ll>
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
#define fr(i, a, b) for (long long i = a; i < b; i++)
#define nfr(i, a, b) for (long long i = a; i <= b; i++)
#define psl pair<str, ll>
#define pls pair<ll, str>
#define pss pair<str, str>
#define ALL(a) a.begin(), a.end()
#define stl stack<ll>
#define coml complex<ll>
#define INF 0x3f3f3f3f3f3f3f3f
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
const ll MOD = 1e9 + 7;
using namespace std;
ll knapSack(ll W, vl wt, vl val, ll n) {
  if (n == 0 || W == 0)
    return 0;
  if (wt[n - 1] > W)
    return knapSack(W, wt, val, n - 1);
  else
    return max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1),
               knapSack(W, wt, val, n - 1));
}
int main() {
  fast
      /*#ifndef ONLINE_JUDGE
              freopen("input.txt", "r" , stdin);
              freopen("output.txt", "w" , stdout);
      #endif*/
      ll n,
      W;
  cin >> n >> W;
  vl wt(n), val(n);
  fr(i, 0, n) { cin >> wt[i] >> val[i]; }
  cout << knapSack(W, wt, val, n);
  return 0;
}


Fixed submission:
//       THIS IS MY NINJA WAY
#include <algorithm>
#include <bits/stdc++.h>
#include <ctime>
#include <vector>

#define str string
#define dbl double
#define ll long long
#define vl vector<ll>
#define vs vector<str>
#define pll pair<ll, ll>
#define vll vector<pll>
#define sl set<ll>
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
#define fast                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \
  srand(time(NULL));
#define fr(i, a, b) for (long long i = a; i < b; i++)
#define nfr(i, a, b) for (long long i = a; i <= b; i++)
#define psl pair<str, ll>
#define pls pair<ll, str>
#define pss pair<str, str>
#define ALL(a) a.begin(), a.end()
#define stl stack<ll>
#define coml complex<ll>
#define INF 0x3f3f3f3f3f3f3f3f
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
const ll MOD = 1e9 + 7;
using namespace std;
ll knapSack(ll W, vl wt, vl val, ll n) {
  ll K[n + 1][W + 1];
  fr(i, 0, n + 1) {
    fr(j, 0, W + 1) {
      if (i == 0 || j == 0)
        K[i][j] = 0;
      else if (wt[i - 1] <= j)
        K[i][j] = max(val[i - 1] + K[i - 1][j - wt[i - 1]], K[i - 1][j]);
      else
        K[i][j] = K[i - 1][j];
    }
  }
  return K[n][W];
}
int main() {
  fast
      /*#ifndef ONLINE_JUDGE
              freopen("input.txt", "r" , stdin);
              freopen("output.txt", "w" , stdout);
      #endif*/
      ll n,
      W;
  cin >> n >> W;
  vl wt(n), val(n);
  fr(i, 0, n) { cin >> wt[i] >> val[i]; }
  cout << knapSack(W, wt, val, n);
  return 0;
}