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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class t> using table = vector<vector<t>>;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  int S, D, M;
  while (cin >> S >> D >> M) {
    map<pair<int, int>, int> nums;
    vector<vector<pair<int, int>>> sws(S);
    for (int i = 0; i < S; ++i) {
      int k;
      cin >> k;
      vector<pair<int, int>> sw(k);
      for (int j = 0; j < k; ++j) {
        int w, p;
        cin >> w >> p;
        sw[j] = make_pair(w, p);
      }
      sws[i] = sw;
    }
    map<int, int> mp;
    for (int i = 0; i < D; ++i) {
      int f;
      cin >> f;
      mp[f]++;
    }
    for (int i = 0; i < S; ++i) {
      for (int j = 0; j < sws[i].size(); ++j) {
        nums[sws[i][j]] += mp[i];
      }
    }

    vector<vector<int>> memo(nums.size() + 1, vector<int>(M + 1, -1e8));
    memo[0][0] = 0;
    int aa = 0;
    for (auto num : nums) {
      for (int nweight = 0; nweight <= M; ++nweight) {
        for (int i = 0; i <= num.second; ++i) {
          const int nextweight = nweight + num.first.second * i;
          const int nextvalue = memo[aa][nweight] + num.first.first * i;
          if (nextweight <= M) {
            memo[aa + 1][nextweight] = max(memo[aa + 1][nextweight], nextvalue);
          } else {
            break;
          }
        }
      }

      aa++;
    }

    int ans = 0;
    int ans2 = 0;
    for (int i = 0; i <= M; ++i) {
      if (ans < memo[nums.size()][i]) {
        ans = memo[nums.size()][i];
        ans2 = i;
      }
    }
    cout << ans << " " << ans2 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class t> using table = vector<vector<t>>;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  int S, D, M;
  while (cin >> S >> D >> M) {
    map<pair<int, int>, int> nums;
    vector<vector<pair<int, int>>> sws(M);
    for (int i = 0; i < S; ++i) {
      int k;
      cin >> k;
      vector<pair<int, int>> sw(k);
      for (int j = 0; j < k; ++j) {
        int w, p;
        cin >> w >> p;
        sw[j] = make_pair(w, p);
      }
      sws[i] = sw;
    }
    map<int, int> mp;
    for (int i = 0; i < D; ++i) {
      int f;
      cin >> f;
      mp[f]++;
    }
    for (int i = 0; i < S; ++i) {
      for (int j = 0; j < sws[i].size(); ++j) {
        nums[sws[i][j]] += mp[i];
      }
    }

    vector<vector<int>> memo(nums.size() + 1, vector<int>(M + 1, -1e8));
    memo[0][0] = 0;
    int aa = 0;
    for (auto num : nums) {
      for (int nweight = 0; nweight <= M; ++nweight) {
        for (int i = 0; i <= num.second; ++i) {
          const int nextweight = nweight + num.first.second * i;
          const int nextvalue = memo[aa][nweight] + num.first.first * i;
          if (nextweight <= M) {
            memo[aa + 1][nextweight] = max(memo[aa + 1][nextweight], nextvalue);
          }
        }
      }

      aa++;
    }

    int ans = 0;
    int ans2 = 0;
    for (int i = 0; i <= M; ++i) {
      if (ans < memo[nums.size()][i]) {
        ans = memo[nums.size()][i];
        ans2 = i;
      }
    }
    cout << ans << " " << ans2 << endl;
  }
  return 0;
}