Problem p03734 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long long dp[100][101][301];
long long w[100];
long long v[100];
long long adjusted_w[100];
int main() {
  int N;
  long long W;
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }

  for (int i = 0; i < N; i++) {
    adjusted_w[i] = w[i] - w[0];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      for (int k = 0; k <= 3 * N; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  dp[0][0][0] = 0;
  dp[0][1][0] = v[0];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      for (int k = 0; k <= 3 * N; k++) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (j > 0 && k - adjusted_w[i] >= 0 &&
            dp[i - 1][j - 1][k - adjusted_w[i]] >= 0) {
          dp[i][j][k] =
              max(dp[i][j][k], dp[i - 1][j - 1][k - adjusted_w[i]] + v[i]);
        }
      }
    }
  }
  /*
  for(int i = 0; i < N; i++){
      for(int j = 0; j <= N; j++){
          cout << j << ": ";
          for(int k = 0; k <= 3 * N; k++){
              cout << dp[i][j][k] << " ";
          }
      }
      cout << endl;
  }
  */
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= W - (w[0] * i); j++) {
      if (j <= 3 * N) {
        ans = max(ans, dp[N - 1][i][j]);
        // cout << i << ", " << j << ": " << dp[N - 1][i][j] << endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long long dp[100][101][301];
long long w[100];
long long v[100];
long long adjusted_w[100];
int main() {
  int N;
  long long W;
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }

  for (int i = 0; i < N; i++) {
    adjusted_w[i] = w[i] - w[0];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      for (int k = 0; k <= 3 * N; k++) {
        dp[i][j][k] = -1;
      }
    }
  }
  dp[0][0][0] = 0;
  dp[0][1][0] = v[0];
  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= N; j++) {
      for (int k = 0; k <= 3 * N; k++) {
        dp[i][j][k] = dp[i - 1][j][k];
        if (j > 0 && k - adjusted_w[i] >= 0 &&
            dp[i - 1][j - 1][k - adjusted_w[i]] >= 0) {
          dp[i][j][k] =
              max(dp[i][j][k], dp[i - 1][j - 1][k - adjusted_w[i]] + v[i]);
        }
      }
    }
  }
  /*
  for(int i = 0; i < N; i++){
      for(int j = 0; j <= N; j++){
          cout << j << ": ";
          for(int k = 0; k <= 3 * N; k++){
              cout << dp[i][j][k] << " ";
          }
      }
      cout << endl;
  }
  */
  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= W - (w[0] * i); j++) {
      if (j <= 3 * N) {
        ans = max(ans, dp[N - 1][i][j]);
        // cout << i << ", " << j << ": " << dp[N - 1][i][j] << endl;
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}