Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define vi vector<int>
#define LLMAX 4000000000000000000LL
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
struct edge {
  int t, f, w;
  bool operator<(const edge &e) const { return w > e.w; }
  edge(int from, int to, int weight) {
    t = to;
    f = from;
    w = weight;
  }
};
#define ii pair<int, int>
#define MOD 1000000007
const double EPS = (1e-5);
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
int findXOR(int n) {
  switch (n % 4) {
  case 0:
    return n;
  case 1:
    return 1;
  case 2:
    return n + 1;
  case 3:
    return 0;
  }
}
int rangeXOR(int l, int r) { return (findXOR(l - 1) ^ findXOR(r)); }
int getbit(int mask, int bit) { return (mask & (1 << bit)); }
void setbit(int &mask, int bit, int val) {
  if (val)
    mask |= (1 << bit);
  else
    mask &= ~(1 << bit);
}
// ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
// ll lcm(ll a, ll b){return a * (b / gcd(a, b));}
const int N = 1e5 + 10;
const int M = 101;
const ll INF = 1e9;
int w[M], v[M];
ll dp[2][M];
void solve() {
  int n, W;
  cin >> n >> W;
  for (int i = 0; i < n; ++i)
    cin >> w[i] >> v[i];
  int x = 0;
  dp[x][w[0]] = v[0];
  for (int i = 1; i < n; ++i) {
    x ^= 1;
    for (int j = 0; j <= W; ++j) {
      if (j + w[i] <= W) {
        dp[x][j + w[i]] = max(dp[x][j + w[i]], v[i] + dp[x ^ 1][j]);
      }
      dp[x][j] = max(dp[x][j], dp[x ^ 1][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= W; ++i)
    ans = max(ans, dp[x][i]);
  cout << ans << '\n';
}
int main(void) {
  // freopen("commandos.in", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc = 1;
  // cin >> tc;
  // scanf("%d", &tc);
  while (tc--) {
    solve();
  }
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define vi vector<int>
#define LLMAX 4000000000000000000LL
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
struct edge {
  int t, f, w;
  bool operator<(const edge &e) const { return w > e.w; }
  edge(int from, int to, int weight) {
    t = to;
    f = from;
    w = weight;
  }
};
#define ii pair<int, int>
#define MOD 1000000007
const double EPS = (1e-5);
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
int findXOR(int n) {
  switch (n % 4) {
  case 0:
    return n;
  case 1:
    return 1;
  case 2:
    return n + 1;
  case 3:
    return 0;
  }
}
int rangeXOR(int l, int r) { return (findXOR(l - 1) ^ findXOR(r)); }
int getbit(int mask, int bit) { return (mask & (1 << bit)); }
void setbit(int &mask, int bit, int val) {
  if (val)
    mask |= (1 << bit);
  else
    mask &= ~(1 << bit);
}
// ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
// ll lcm(ll a, ll b){return a * (b / gcd(a, b));}
const int N = 1e5 + 10;
const int M = 101;
const ll INF = 1e9;
int w[M], v[M];
ll dp[2][N];
void solve() {
  int n, W;
  cin >> n >> W;
  for (int i = 0; i < n; ++i)
    cin >> w[i] >> v[i];
  int x = 0;
  dp[x][w[0]] = v[0];
  for (int i = 1; i < n; ++i) {
    x ^= 1;
    for (int j = 0; j <= W; ++j) {
      if (j + w[i] <= W) {
        dp[x][j + w[i]] = max(dp[x][j + w[i]], v[i] + dp[x ^ 1][j]);
      }
      dp[x][j] = max(dp[x][j], dp[x ^ 1][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i <= W; ++i)
    ans = max(ans, dp[x][i]);
  cout << ans << '\n';
}
int main(void) {
  // freopen("commandos.in", "r", stdin);
  // freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc = 1;
  // cin >> tc;
  // scanf("%d", &tc);
  while (tc--) {
    solve();
  }
}