Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// #define DEB1 //input from input.txt
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>

#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
// closed interval, inverse repeat
#define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--)
#define CREP(i, start, fin) for (int i = (start); i <= (fin); i++)
#define RNG(i, start, fin) for (int i = (start); i < (fin); i++)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
#define SZ(x) (int)(x).size()

using namespace std;

using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VVI = vector<VI>;
const LL LINF = 1001002003004005006ll;
/*
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,SZ(v))i>>v[j];return i;}

template<typename T>string join(const vector<T>&v)
{stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);}

template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v)
{if(SZ(v))o<<join(v);return o;}
*/

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/*
constant values
*/

// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;

// const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9

// Main program
void solve() {
  // cout << "Hello world" << endl;
  int N, W;
  cin >> N >> W;
  VLL W_vec;
  VLL V_vec;
  REP(i, N) {
    int w, v;
    cin >> w >> v;
    W_vec.PB(w);
    V_vec.PB(v);
  }
  VLL dp_old;
  REP(w, W_vec[0]) { dp_old.PB(0); }
  CREP(w, W_vec[0], W) { dp_old.PB(V_vec[0]); }
  VLL dp_new = dp_old;
  CREP(i, 1, N - 1) {
    CREP(w, 0, W) {
      dp_new[w] = max(dp_old[w - W_vec[i]] + V_vec[i], dp_old[w]);
    }
    dp_old = dp_new;
  }
  cout << dp_new[W] << endl;
  return;
}

// ----------------------------------------------
int main() {

#ifdef DEB1
  cout << "DEBUG MODE" << endl;
  ifstream in("input.txt"); // for debug
  cin.rdbuf(in.rdbuf());    // for debug
#endif

  int T = 1;

  for (int i = 0; i < T; ++i) {
    solve();
  }

  return 0;
}

Fixed submission:
// #define DEB1 //input from input.txt
// #define NDEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>

#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
// closed interval, inverse repeat
#define CIREP(i, start, fin) for (int i = (start); i >= (fin); i--)
#define CREP(i, start, fin) for (int i = (start); i <= (fin); i++)
#define RNG(i, start, fin) for (int i = (start); i < (fin); i++)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
#define SZ(x) (int)(x).size()

using namespace std;

using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VVI = vector<VI>;
const LL LINF = 1001002003004005006ll;
/*
template<typename T>inline istream& operator>>(istream&i,vector<T>&v)
{REP(j,SZ(v))i>>v[j];return i;}

template<typename T>string join(const vector<T>&v)
{stringstream s;REP(i,SZ(v))s<<' '<<v[i];return s.str().substr(1);}

template<typename T>inline ostream& operator<<(ostream&o,const vector<T>&v)
{if(SZ(v))o<<join(v);return o;}
*/

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

/*
constant values
*/

// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;

// const ll INF= 1LL<<60 ;//INF = 1001001001001; //1e9

// Main program
void solve() {
  // cout << "Hello world" << endl;
  int N, W;
  cin >> N >> W;
  VLL W_vec;
  VLL V_vec;
  REP(i, N) {
    int w, v;
    cin >> w >> v;
    W_vec.PB(w);
    V_vec.PB(v);
  }
  VLL dp_old;
  REP(w, W_vec[0]) { dp_old.PB(0); }
  CREP(w, W_vec[0], W) { dp_old.PB(V_vec[0]); }
  VLL dp_new = dp_old;
  CREP(i, 1, N - 1) {
    CREP(w, 0, W) {
      if (w - W_vec[i] < 0) {
        dp_new[w] = dp_old[w];
        continue;
      }
      dp_new[w] = max(dp_old[w - W_vec[i]] + V_vec[i], dp_old[w]);
    }
    dp_old = dp_new;
  }
  cout << dp_new[W] << endl;
  return;
}

// ----------------------------------------------
int main() {

#ifdef DEB1
  cout << "DEBUG MODE" << endl;
  ifstream in("input.txt"); // for debug
  cin.rdbuf(in.rdbuf());    // for debug
#endif

  int T = 1;

  for (int i = 0; i < T; ++i) {
    solve();
  }

  return 0;
}