Problem p00677 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

class Goods {
public:
  int w;
  int p;
  Goods(int _w, int _p) : w(_w), p(_p) {}
};

int dp[101][301];

int main() {
  int s, d, m;

  while (~scanf("%d %d %d", &s, &d, &m)) {

    map<int, vector<Goods>> data;

    for (int i = 0; i < s; i++) {
      int k;
      scanf("%d", &k);

      for (int j = 0; j < k; j++) {
        int w, p;
        scanf("%d %d", &w, &p);
        data[i].push_back(Goods(w, p));
      }
    }

    vector<int> days;

    for (int i = 0; i < d; i++) {
      int f;
      scanf("%d", &f);
      days.push_back(f);
    }

    memset(dp, 0, sizeof(dp));

    for (int idx = 0; idx < days.size(); idx++) {
      vector<Goods> &it = data[days[idx]];

      int cost[301];
      memset(cost, 0, sizeof(cost));

      for (int i = 0; i < it.size(); i++) {
        int w = it[i].w;
        int p = it[i].p;
        for (int used = m - p; used >= 0; used--) {
          cost[used + p] = max(cost[used] + w, cost[used + p]);
        }
      }

      for (int j = 0; j <= 300; j++) {
        for (int used = 0; used <= j; used++) {
          dp[idx + 1][j] = max(dp[idx + 1][j], dp[idx][j - used] + cost[used]);
        }
      }
    }

    int resw = 0;
    int resm = 0;
    for (int i = 0; i <= m; i++) {
      if (resw < dp[d][i]) {
        resm = i;
        resw = dp[d][i];
      }
    }

    printf("%d %d\\n", resw, resm);
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

class Goods {
public:
  int w;
  int p;
  Goods(int _w, int _p) : w(_w), p(_p) {}
};

int dp[101][5001];

int main() {
  int s, d, m;

  while (~scanf("%d %d %d", &s, &d, &m)) {

    map<int, vector<Goods>> data;
    int MAX_W = 0;
    for (int i = 0; i < s; i++) {
      int k;
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        int w, p;
        scanf("%d %d", &w, &p);
        data[i].push_back(Goods(w, p));
        MAX_W += w;
      }
    }

    vector<int> days;

    for (int i = 0; i < d; i++) {
      int f;
      scanf("%d", &f);
      days.push_back(f);
    }

    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;

    for (int idx = 0; idx < days.size(); idx++) {
      vector<Goods> &it = data[days[idx]];

      for (int i = 0; i < it.size(); i++) {
        int w = it[i].w;
        int p = it[i].p;
        for (int j = 0; j <= MAX_W; j++) {
          if (j - w >= 0) {
            dp[idx + 1][j] =
                min(min(dp[idx][j], dp[idx + 1][j]), dp[idx][j - w] + p);
          } else {
            dp[idx + 1][j] = min(dp[idx + 1][j], dp[idx][j]);
          }
        }
      }
    }

    int resw = 0;
    int resm = 0;
    for (int i = 0; i <= MAX_W; i++) {
      if (dp[d][i] <= m) {
        resm = dp[d][i];
        resw = i;
      }
    }

    printf("%d %d\\n", resw, resm);
  }
}