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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
int main() {
  while (1) {
    vector<string> menu[105];
    vector<string> vec;
    int n, m;
    cin >> n;
    if (n == 0)
      break;
    map<string, int> ma;
    for (int i = 0; i < n; i++) {
      string name;
      int cost;
      cin >> name >> cost;
      ma[name] = cost;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      string s;
      int k;
      cin >> s;
      vec.pb(s);
      cin >> k;
      for (int j = 0; j < k; j++) {
        string ss;
        cin >> ss;
        menu[i].pb(ss);
      }
    }
    bool update = true;
    while (update) {
      update = false;
      for (int i = 0; i < m; i++) {
        int sum = 0;
        for (int j = 0; j < menu[i].size(); j++) {
          sum += ma[menu[i][j]];
        }
        if (ma[vec[i]] > sum) {
          ma[vec[i]] = sum;
          update = true;
        }
      }
    }
    string out;
    cin >> out;
    cout << ma[out] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace ::std;
int n;

struct item {
  string name;
  int cost;
  vector<string> used;
  int size;
};

item list[100];
int memo[100];

int getCost(int v) {
  if (memo[v] != -1)
    return memo[v];
  int i, j;
  int ans;

  if (list[v].size == 0) {
    return (list[v].cost);
  } else {
    ans = 0;
    for (i = 0; i < list[v].size; i++) {
      for (j = 0; j < n; j++) {
        if (list[v].used[i] == list[j].name) {
          ans += getCost(j);
        }
      }
    }
    list[v].cost = min(list[v].cost, ans);
  }
  return (list[v].cost);
}

int main(void) {
  int i, j, k;
  int m, kk;
  string key, temp;
  int look, sum;

  while (1) {
    scanf("%d", &n);

    if (n == 0) {
      break;
    }
    for (i = 0; i < n; i++)
      memo[i] = -1;
    for (i = 0; i < n; i++) {
      cin >> list[i].name >> list[i].cost;
      list[i].size = 0;
    }

    cin >> m;
    for (i = 0; i < m; i++) {
      cin >> key;
      for (j = 0; j < n; j++) {
        if (key == list[j].name) {
          look = j;
        }
      }
      cin >> kk;
      list[look].size = kk;
      for (j = 0; j < kk; j++) {
        cin >> temp;
        for (k = 0; k < n; k++) {
          if (temp == list[k].name) {
            list[look].used[j] = temp;
          }
        }
      }
    }
    cin >> key;
    for (j = 0; j < n; j++) {
      if (key == list[j].name) {
        look = j;
      }
    }

    cout << getCost(look);
  }

  return (0);
}