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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int NUM_ITEM_MAX = 200;

long long int updateValue(int n, vector<long long int> &valueList,
                          vector<vector<int>> &recipeList,
                          vector<bool> &updatedList) {
  if (updatedList[n]) {
    return valueList[n];
  }
  updatedList[n] = true;
  vector<int> recipe = recipeList[n];
  if (recipe.empty()) {
    return valueList[n];
  }
  long long int value = 0;
  for (int i = 0; i < (int)recipe.size(); ++i) {
    int id = recipe[i];
    value += updateValue(id, valueList, recipeList, updatedList);
  }
  if (value < valueList[n]) {
    valueList[n] = value;
  }
  return valueList[n];
}

int main() {
  int n;
  while (cin >> n && n) {
    int id = 0;
    map<string, int> nameList;
    vector<long long int> valueList(NUM_ITEM_MAX + 1, 1LL << 62);
    for (int i = 0; i < n; ++i) {
      string s;
      int v;
      cin >> s >> v;
      int d = id++;
      nameList.insert(make_pair(s, d));
      valueList[d] = v;
    }
    int m;
    vector<vector<int>> recipeList(NUM_ITEM_MAX + 1, vector<int>());
    cin >> m;
    for (int i = 0; i < m; ++i) {
      string o;
      int k;
      cin >> o >> k;
      int count = nameList.count(o);
      if (count == 0) {
        int d = id++;
        nameList.insert(make_pair(o, d));
      }
      int productId = nameList[o];
      for (int j = 0; j < k; ++j) {
        string q;
        cin >> q;
        count = nameList.count(q);
        if (count == 0) {
          int d = id++;
          nameList.insert(make_pair(q, d));
        }
        int materialId = nameList[q];
        recipeList[productId].push_back(materialId);
      }
    }
    vector<bool> updatedList(nameList.size(), false);
    for (int i = 0; i < (int)nameList.size(); ++i) {
      updateValue(i, valueList, recipeList, updatedList);
    }

    string ansName;
    cin >> ansName;
    cout << valueList[nameList[ansName]] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  while (cin >> n && n) {
    int id = 0;
    map<string, int> nameList;
    vector<long long int> valueList(n, 0);
    for (int i = 0; i < n; ++i) {
      string s;
      int v;
      cin >> s >> v;
      int d = id++;
      nameList.insert(make_pair(s, d));
      valueList[d] = v;
    }
    int m;
    vector<vector<int>> recipeList(n, vector<int>());
    cin >> m;
    for (int i = 0; i < m; ++i) {
      string o;
      int k;
      cin >> o >> k;
      int productId = nameList.find(o)->second;
      for (int j = 0; j < k; ++j) {
        string q;
        cin >> q;
        int materialId = nameList.find(q)->second;
        recipeList[productId].push_back(materialId);
      }
    }
    vector<int> depthList(n, -1);
    queue<int> updateList;
    for (int i = 0; i < n; ++i) {
      updateList.push(i);
    }
    while (!updateList.empty()) {
      int id = updateList.front();
      updateList.pop();
      if (recipeList[id].empty()) {
        depthList[id] = 0;
        continue;
      }
      int size = recipeList[id].size();
      int d = -1;
      for (int i = 0; i < size; ++i) {
        d = max(d, depthList[recipeList[id][i]]);
      }
      if (d > -1) {
        depthList[id] = d + 1;
      } else {
        updateList.push(id);
      }
    }
    vector<pair<int, int>> sortedList;
    for (int i = 0; i < n; ++i) {
      sortedList.push_back(make_pair(depthList[i], i));
    }
    sort(sortedList.begin(), sortedList.end());

    for (int i = 0; i < (int)sortedList.size(); ++i) {
      int id = sortedList[i].second;
      vector<int> recipe = recipeList[id];
      if (recipe.empty()) {
        continue;
      }
      long long int value = 0;
      for (int j = 0; j < (int)recipe.size(); ++j) {
        value += valueList[recipe[j]];
      }
      if (value < valueList[id]) {
        valueList[id] = value;
      }
    }

    string ansName;
    cin >> ansName;
    cout << valueList[nameList.find(ansName)->second] << endl;
  }
  return 0;
}