Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int fINF = 0x3f3f3f3f;
const int nINF = 1 << 31;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

const int N = 100 + 10, C = 1e5 + 10;
int n, c, v[N], w[N];
ll dp[N][C];

signed main() {
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(7);
  memset(dp, -1, sizeof(dp));
  cin >> n >> c;
  FOR(i, 1, n) cin >> w[i] >> v[i];
  dp[0][c] = 0;
  FOR(i, 1, n) {
    FORD(j, c, 0) {
      if (dp[i - 1][j] != -1) {
        if (j - w[i] >= 0)
          dp[i][j - w[i]] = max(dp[i][j - w[j]], dp[i - 1][j] + v[i]);
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      }
    }
  }
  ll ans = 0;
  FOR(i, 0, c) ans = max(ans, dp[n][i]);
  cout << ans << endl;

  return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting min to 0 instead of -INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
// #define int long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int fINF = 0x3f3f3f3f;
const int nINF = 1 << 31;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

const int N = 100 + 10, C = 1e5 + 10;
int n, c, v[N], w[N];
ll dp[N][C];

signed main() {
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(7);
  memset(dp, -1, sizeof(dp));
  cin >> n >> c;
  FOR(i, 1, n) cin >> w[i] >> v[i];
  dp[0][c] = 0;
  FOR(i, 1, n) {
    FORD(j, c, 0) {
      if (dp[i - 1][j] != -1) {
        if (j - w[i] >= 0)
          dp[i][j - w[i]] = max(dp[i][j - w[i]], dp[i - 1][j] + v[i]);
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      }
    }
  }
  ll ans = 0;
  FOR(i, 0, c) ans = max(ans, dp[n][i]);
  cout << ans << endl;

  return 0;
}

// Easy things to check:
// - LONG LONGS (WA)
// - const int N is correct (WA, RTE)
// - .size()-k underflow (WA, RTE, TLE)
// - small n edge cases (WA)

// Rare mistakes made in the past:
// - division by 0 (WA)
// - integer division (WA)
// - setprecision (WA)
// - INF not big enough using ll (WA)
// - setting min to 0 instead of -INF (WA)
// - outputting debug (WA)
// - allocating too much memory (locRTE, MLE)
// - stack size (locRTE)