Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b) ? a : b; }

// Returns the maximum value that can be put in a knapsack of capacity W
long long knapSack1(int W, int weight[], int val[], int n) {
  int k, w;
  long long **F = new long long *[n + 1];
  for (int i = 0; i < n + 1; i++)
    F[i] = new long long[W + 1];

  // Build table K[][] in bottom up manner
  for (k = 0; k <= n; k++) {
    for (w = 0; w <= W; w++) {
      if (k == 0 || w == 0)
        F[k][w] = 0;
      else if (weight[k - 1] <= w)
        F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
      else
        F[k][w] = F[k - 1][w];
      // cout<<std::setw(2)<<F[k][w]<<" ";
    }
    // cout<<endl;
  }
  long long result = F[n][W];
  for (int i = 0; i < n + 1; i++)
    delete[] F[i];
  delete[] F;
  return result;
}

long long knapSack2(int W, int weight[], int val[], int n) {
  long long F[n + 1][W + 1];
  // Build table K[][] in bottom up manner
  for (int k = 0; k <= n; k++) {
    for (int w = 0; w <= W; w++) {
      if (k == 0 || w == 0)
        F[k][w] = 0;
      else if (weight[k - 1] <= w)
        F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
      else
        F[k][w] = F[k - 1][w];
    }
  }
  return F[n][W];
}

long long knapSack3(int W, int weight[], int val[], int n) {
  long long vsum = 0;
  for (int i = 0; i < n; i++) {
    vsum += val[i];
  }

  long long *dp = new long long[vsum + 1];
  for (long long i = 0; i < vsum + 1; i++)
    dp[i] = pow(10, 18) + 5;
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (long long j = vsum; j >= val[i]; j--) {
      dp[j] = min(dp[j], dp[j - val[i]] + weight[i]);
    }
  }

  long long i;
  for (i = vsum; i >= 0; i--) {
    if (dp[i] <= W) {
      // cout << i;
      break;
    }
  }
  delete[] dp;
  return i;
}

int main() {

  std::ios::sync_with_stdio(false);

  int n, W;
  std::cin >> n >> W;
  int *val = new int[n];
  int *wt = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  std::cout << knapSack2(W, wt, val, n) << endl;
  delete[] val;
  delete[] wt;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b) ? a : b; }

// Returns the maximum value that can be put in a knapsack of capacity W
long long knapSack1(int W, int weight[], int val[], int n) {
  int k, w;
  long long **F = new long long *[n + 1];
  for (int i = 0; i < n + 1; i++)
    F[i] = new long long[W + 1];

  // Build table K[][] in bottom up manner
  for (k = 0; k <= n; k++) {
    for (w = 0; w <= W; w++) {
      if (k == 0 || w == 0)
        F[k][w] = 0;
      else if (weight[k - 1] <= w)
        F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
      else
        F[k][w] = F[k - 1][w];
      // cout<<std::setw(2)<<F[k][w]<<" ";
    }
    // cout<<endl;
  }
  long long result = F[n][W];
  for (int i = 0; i < n + 1; i++)
    delete[] F[i];
  delete[] F;
  return result;
}

long long knapSack2(int W, int weight[], int val[], int n) {
  long long F[n + 1][W + 1];
  // Build table K[][] in bottom up manner
  for (int k = 0; k <= n; k++) {
    for (int w = 0; w <= W; w++) {
      if (k == 0 || w == 0)
        F[k][w] = 0;
      else if (weight[k - 1] <= w)
        F[k][w] = max(F[k - 1][w], F[k - 1][w - weight[k - 1]] + val[k - 1]);
      else
        F[k][w] = F[k - 1][w];
    }
  }
  return F[n][W];
}

long long knapSack3(int W, int weight[], int val[], int n) {
  long long vsum = 0;
  for (int i = 0; i < n; i++) {
    vsum += val[i];
  }

  long long *dp = new long long[vsum + 1];
  for (long long i = 0; i < vsum + 1; i++)
    dp[i] = pow(10, 18) + 5;
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (long long j = vsum; j >= val[i]; j--) {
      dp[j] = min(dp[j], dp[j - val[i]] + weight[i]);
    }
  }

  long long i;
  for (i = vsum; i >= 0; i--) {
    if (dp[i] <= W) {
      // cout << i;
      break;
    }
  }
  delete[] dp;
  return i;
}

int main() {

  std::ios::sync_with_stdio(false);

  int n, W;
  std::cin >> n >> W;
  int *val = new int[n];
  int *wt = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  std::cout << knapSack3(W, wt, val, n) << endl;
  delete[] val;
  delete[] wt;

  return 0;
}