Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// It doesn't matter how slow you go, Unless you don't stop.
#include <algorithm>
#include <bitset>
#include <iostream>

#include <climits>
#include <cmath>
#include <cstring>

#include <deque>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <chrono>
#include <fstream>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> p64;
typedef vector<LL> v64;
typedef map<LL, LL> mp64;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, s, e) for (long long i = s; i <= e; i++)
#define brep(i, s, e) for (long long i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x))
#define DANGER                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
LL knapsack(LL W[], LL v[], int wght, int n) {
  LL **dp = new LL *[n];
  for (int i = 0; i <= n; i++) {
    dp[i] = new LL[wght + 1];
    for (int j = 0; j <= wght; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i <= wght; i++) {
    dp[0][i] = 0;
  }
  for (int i = 0; i < n; i++) {
    dp[i][0] = 0;
  }
  // solve
  for (int i = 1; i <= n; i++) {
    for (int w = 0; w <= wght; w++) {
      dp[i][w] = dp[i - 1][w];
      if (W[i - 1] <= w) {
        // cout<<W[i-1]<<" "<<w<<endl;
        dp[i][w] = max(dp[i][w], dp[i - 1][w - W[i - 1]] + v[i - 1]);
      }
    }
  }
  // for(int i=0;i<=n;i++)
  // {
  //     for(int j=0;j<=wght;j++)
  //     {
  //         cout<<dp[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  // cout<<endl;
  return dp[n][wght];
}
void solve() {
  int n, wght;
  cin >> n >> wght;
  LL w[n], v[n];
  for (int i = 0; i < n; i++) {
    LL x, y;
    cin >> x >> y;
    w[i] = x;
    v[i] = y;
  }
  LL ans = knapsack(w, v, wght, n);
  cout << ans << endl;
}

int main() {
  DANGER;
  ofstream out("output.txt");

  auto start = chrono::high_resolution_clock::now();

  LL t = 1;
  // cin>>t;
  rep(test, 1, t) { solve(); }

  auto stop = chrono::high_resolution_clock::now();
  // auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
  // cout << "Time taken by function: " << duration.count() << " milliseconds"
  // << endl;
}

Fixed submission:
// It doesn't matter how slow you go, Unless you don't stop.
#include <algorithm>
#include <bitset>
#include <iostream>

#include <climits>
#include <cmath>
#include <cstring>

#include <deque>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <chrono>
#include <fstream>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> p64;
typedef vector<LL> v64;
typedef map<LL, LL> mp64;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i, s, e) for (long long i = s; i <= e; i++)
#define brep(i, s, e) for (long long i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x))
#define DANGER                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
LL knapsack(LL W[], LL v[], int wght, int n) {
  LL **dp = new LL *[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = new LL[wght + 1];
    for (int j = 0; j <= wght; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i <= wght; i++) {
    dp[0][i] = 0;
  }
  for (int i = 0; i < n; i++) {
    dp[i][0] = 0;
  }
  // solve
  for (int i = 1; i <= n; i++) {
    for (int w = 0; w <= wght; w++) {
      dp[i][w] = dp[i - 1][w];
      if (W[i - 1] <= w) {
        // cout<<W[i-1]<<" "<<w<<endl;
        dp[i][w] = max(dp[i][w], dp[i - 1][w - W[i - 1]] + v[i - 1]);
      }
    }
  }
  // for(int i=0;i<=n;i++)
  // {
  //     for(int j=0;j<=wght;j++)
  //     {
  //         cout<<dp[i][j]<<" ";
  //     }
  //     cout<<endl;
  // }
  // cout<<endl;
  return dp[n][wght];
}
void solve() {
  int n, wght;
  cin >> n >> wght;
  LL w[n], v[n];
  for (int i = 0; i < n; i++) {
    LL x, y;
    cin >> x >> y;
    w[i] = x;
    v[i] = y;
  }
  LL ans = knapsack(w, v, wght, n);
  cout << ans << endl;
}

int main() {
  DANGER;
  ofstream out("output.txt");

  auto start = chrono::high_resolution_clock::now();

  LL t = 1;
  // cin>>t;
  rep(test, 1, t) { solve(); }

  auto stop = chrono::high_resolution_clock::now();
  // auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
  // cout << "Time taken by function: " << duration.count() << " milliseconds"
  // << endl;
}