Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;

// 見た個数、選んだ個数、重さ
ll dp[101][101][401];

int main() {
  long long W;
  long long N;
  scanf("%lld", &N);
  vector<long long> w(N - 1 + 1);
  vector<long long> v(N - 1 + 1);
  scanf("%lld", &W);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &w[i]);
    scanf("%lld", &v[i]);
    if (i > 0)
      w[i] -= w[0] - 1;
  }
  ll w0 = w[0];
  w[0] = 1;
  // rep(i, N) {
  // 	cout << w[i] << endl;
  // }
  rep(i, 101) {
    rep(j, 101) {
      rep(k, 401) { dp[i][j][k] = -INF; }
    }
  }
  dp[0][0][0] = 0;
  rep(i, N) {
    rep(j, N + 1) {
      rep(k, 401) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        if (k - w[i] >= 0)
          dp[i + 1][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k - w[i]] + v[i]);
      }
    }
  }
  ll res = 0;
  rep(j, N + 1) {
    rep(k, 401) {
      if (k <= W - (w0 - 1) * j)
        res = max(res, dp[N][j][k]);
    }
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;

// 見た個数、選んだ個数、重さ
ll dp[101][101][401];

int main() {
  long long W;
  long long N;
  scanf("%lld", &N);
  vector<long long> w(N - 1 + 1);
  vector<long long> v(N - 1 + 1);
  scanf("%lld", &W);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &w[i]);
    scanf("%lld", &v[i]);
    if (i > 0)
      w[i] -= w[0] - 1;
  }
  ll w0 = w[0];
  w[0] = 1;
  // rep(i, N) {
  // 	cout << w[i] << endl;
  // }
  rep(i, 101) {
    rep(j, 101) {
      rep(k, 401) { dp[i][j][k] = -INF; }
    }
  }
  dp[0][0][0] = 0;
  rep(i, N) {
    rep(j, N + 1) {
      rep(k, 401) {
        dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        if (k - w[i] >= 0 && j + 1 <= N)
          dp[i + 1][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k - w[i]] + v[i]);
      }
    }
  }
  ll res = 0;
  rep(j, N + 1) {
    rep(k, 401) {
      if (k <= W - (w0 - 1) * j)
        res = max(res, dp[N][j][k]);
    }
  }
  cout << res << endl;
  return 0;
}