Problem p03164 (C++)

Buggy submission (status: Runtime Error):

// Target Expert

/*
 * Author    : raj1307 - Raj Singh
 * Institute : Jalpaiguri Government Engineering College
 * Date      : 3.08.19
 */

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define DEBUG
#ifdef DEBUG
#define deb(...)                                                               \
  cerr << "Line:" << __LINE__ << " ";                                          \
  __f(#__VA_ARGS__, __VA_ARGS__)
#define debarr(a, n)                                                           \
  cerr << #a << " : ";                                                         \
  for (int i = 0; i < n; i++)                                                  \
    cerr << a[i] << " ";                                                       \
  cerr << endl;
#define debmat(mat, row, col)                                                  \
  cerr << #mat << " :\n";                                                      \
  for (int i = 0; i < row; i++) {                                              \
    for (int j = 0; j < col; j++)                                              \
      cerr << mat[i][j] << " ";                                                \
    cerr << endl;                                                              \
  }
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#else
#define deb(...)                                                               \
  {}
#define debarr(a, n)                                                           \
  {}
#define debmat(mat, row, col)                                                  \
  {}
#define endl "\n"
#endif

#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fr(i, a, b) for (int i = (a); i < (b); ++i)
#define fb(i, b, a) for (int i = (b); i > (a); --i)
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rr return
#define int long long
#define pb push_back
#define sz(x) ((int)x.size())
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}

const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e18 + 5;

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const int N = 5e5 + 5;
int n, W;

int cache[10005][105];
int v[105], w[105];

int dp(int sumv, int pos) {

  if (sumv == 0)
    return 0;

  else if (sumv < 0 or pos == n)
    return 1e18;

  else if (cache[sumv][pos] != -1)
    return cache[sumv][pos];

  else {

    cache[sumv][pos] =
        min(w[pos] + dp(sumv - v[pos], pos + 1), dp(sumv, pos + 1));
    return cache[sumv][pos];
  }
}

void solve() {

  cin >> n >> W;
  int sumv = 0;
  fr(i, 0, n) {
    cin >> w[i];
    cin >> v[i];
    sumv += v[i];
  }

  memset(cache, -1, sizeof(cache));

  // cout<<dp(sumv,0);
  int ans = 0;
  fr(i, 1, sumv + 1) {
    // deb(ans);
    if (dp(i, 0) <= W)
      ans = i;
  }

  cout << ans;
}

signed main() {
  fio;

  cout << fixed << setprecision(10);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}


Fixed submission:

// Target Expert

/*
 * Author    : raj1307 - Raj Singh
 * Institute : Jalpaiguri Government Engineering College
 * Date      : 3.08.19
 */

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <assert.h>
#include <complex>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

#define DEBUG
#ifdef DEBUG
#define deb(...)                                                               \
  cerr << "Line:" << __LINE__ << " ";                                          \
  __f(#__VA_ARGS__, __VA_ARGS__)
#define debarr(a, n)                                                           \
  cerr << #a << " : ";                                                         \
  for (int i = 0; i < n; i++)                                                  \
    cerr << a[i] << " ";                                                       \
  cerr << endl;
#define debmat(mat, row, col)                                                  \
  cerr << #mat << " :\n";                                                      \
  for (int i = 0; i < row; i++) {                                              \
    for (int j = 0; j < col; j++)                                              \
      cerr << mat[i][j] << " ";                                                \
    cerr << endl;                                                              \
  }
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#else
#define deb(...)                                                               \
  {}
#define debarr(a, n)                                                           \
  {}
#define debmat(mat, row, col)                                                  \
  {}
#define endl "\n"
#endif

#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define fr(i, a, b) for (int i = (a); i < (b); ++i)
#define fb(i, b, a) for (int i = (b); i > (a); --i)
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rr return
#define int long long
#define pb push_back
#define sz(x) ((int)x.size())
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a > b)
    return b;
  else
    return a;
}

const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e18 + 5;

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

const int N = 5e5 + 5;
int n, W;

int cache[100005][105];
int v[105], w[105];

int dp(int sumv, int pos) {

  if (sumv == 0)
    return 0;

  else if (sumv < 0 or pos == n)
    return 1e18;

  else if (cache[sumv][pos] != -1)
    return cache[sumv][pos];

  else {

    cache[sumv][pos] =
        min(w[pos] + dp(sumv - v[pos], pos + 1), dp(sumv, pos + 1));
    return cache[sumv][pos];
  }
}

void solve() {

  cin >> n >> W;
  int sumv = 0;
  fr(i, 0, n) {
    cin >> w[i];
    cin >> v[i];
    sumv += v[i];
  }

  memset(cache, -1, sizeof(cache));

  // cout<<dp(sumv,0);
  int ans = 0;
  fr(i, 1, sumv + 1) {
    // deb(ans);
    if (dp(i, 0) <= W)
      ans = i;
  }

  cout << ans;
}

signed main() {
  fio;

  cout << fixed << setprecision(10);
  int t = 1;
  //	cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}