alexjercan/bugnet huggingface dataset:
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
map<string, int> price;
map<string, vector<string>> rec;
int R(string o) {
  int m = 0, i;
  if (rec[o].size()) {
    for (i = 0; i < rec[o].size(); i++)
      m += R(rec[o][i]);
  } else
    m = price[o];
  if (price[o] && price[o] < m)
    m = price[o];
  return m;
}
int main() {
  int n, m;
  string s, t;
  for (; cin >> n, n;) {
    price.clear();
    rec.clear();
    for (; n--;) {
      cin >> s;
      cin >> price[s];
    }
    cin >> n;
    for (; n--;) {
      cin >> s >> m;
      for (; m--;) {
        cin >> t;
        rec[s].push_back(t);
      }
    }
    cin >> s;
    cout << R(s) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
map<string, int> price;
map<string, vector<string>> rec;
int R(string o) {
  int m = 0, i;
  if (rec[o].size()) {
    for (i = 0; i < rec[o].size(); i++)
      m += R(rec[o][i]);
  } else
    m = price[o];
  if (price[o] < m)
    m = price[o];
  return m;
}
int main() {
  int n, m;
  string s, t;
  for (; cin >> n, n;) {
    for (; n--;) {
      cin >> s;
      cin >> price[s];
    }
    cin >> n;
    for (; n--;) {
      cin >> s >> m;
      for (; m--;) {
        cin >> t;
        rec[s].push_back(t);
      }
    }
    cin >> s;
    cout << R(s) << endl;
  }
  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int search(string, pair<string, int> *, pair<string, vector<string>> *);
int n;
int m, si;
int main() {

  while (true) {
    pair<string, int> p[101];
    pair<string, vector<string>> chan[101];
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> p[i].first >> p[i].second;
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> chan[i].first >> si;
      for (int i2 = 0; i2 < si; i2++) {
        string s;
        cin >> s;
        chan[i].second.push_back(s);
      }
    }
    string s;
    cin >> s;
    int ans = search(s, p, chan);
    cout << ans << endl;
  }
}
int search(string s, pair<string, int> *p, pair<string, vector<string>> *chan) {
  int num = 0;
  bool flag = true;
  for (int i = 0; i < m; i++) {
    if (chan[i].first == s) {
      flag = false;
      for (int i2 = 0; i2 < chan[i].second.size(); i2++) {
        num += search(chan[i].second[i2], p, chan);
      }
    }
  }
  if (!flag) {
    int k;
    for (int i = 0; i < n; i++) {
      if (p[i].first == s) {
        num = min(p[i].second, num);
        break;
      }
    }
  }
  if (flag) {
    for (int i = 0; i < n; i++) {
      if (p[i].first == s) {
        num += p[i].second;
      }
    }
  }
  return num;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
pair<string, int> p[101];
pair<string, vector<string>> chan[101];
int search(string);
int n;
int m, si;
int main() {

  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> p[i].first >> p[i].second;
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> chan[i].first >> si;
      for (int i2 = 0; i2 < si; i2++) {
        string s;
        cin >> s;
        chan[i].second.push_back(s);
      }
    }
    string s;
    cin >> s;
    int ans = search(s);
    cout << ans << endl;
  }
}
int search(string s) {
  int num = 0;
  bool flag = true;
  for (int i = 0; i < m; i++) {
    if (chan[i].first == s) {
      flag = false;
      for (int i2 = 0; i2 < chan[i].second.size(); i2++) {
        num += search(chan[i].second[i2]);
      }
    }
  }
  if (flag) {
    for (int i = 0; i < n; i++) {
      if (p[i].first == s) {
        num += p[i].second;
      }
    }
  }
  return num;
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

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

map<string, int> normal;
map<string, vector<string>> rec;

int f(string x) {
  int ans = normal[x], sum = 0;
  if (rec[x].size() == 0)
    return ans;
  for (int i = 0; i < rec[x].size(); i++) {
    sum += f(rec[x][i]);
  }
  return min(ans, sum);
}

int main() {
  int n;
  while (cin >> n && n) {
    normal.clear();
    rec.clear();
    for (int i = 0; i < n; i++) {
      string a;
      int b;
      cin >> a >> b;
      normal[a] = b;
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
      string x, y;
      cin >> x;
      int t;
      cin >> t;
      for (int j = 0; j < t; j++) {
        cin >> y;
        rec[x].push_back(y);
      }
    }
    string want;
    cin >> want;
    cout << f(want) << endl;
  }
}

Error: TLE

Fixed submission:

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

map<string, int> normal;
map<string, vector<string>> rec;

int f(string x) {
  int ans = normal[x], sum = 0;
  if (rec[x].size() == 0)
    return ans;
  for (int i = 0; i < rec[x].size(); i++) {
    sum += f(rec[x][i]);
  }
  return min(ans, sum);
}

int main() {
  int n;
  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      string a;
      int b;
      cin >> a >> b;
      normal[a] = b;
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
      string x, y;
      cin >> x;
      int t;
      cin >> t;
      for (int j = 0; j < t; j++) {
        cin >> y;
        rec[x].push_back(y);
      }
    }
    string want;
    cin >> want;
    cout << f(want) << endl;
  }
}
Problem p00201 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];

int main() {
  while (cin >> N, N) {
    REP(i, N) cin >> S[i] >> P[i];
    cin >> M;
    REP(i, M) {
      cin >> O[i] >> K[i];
      REP(j, K[i]) cin >> Q[i][j];
    }
    cin >> T;

    map<string, int> conv;
    REP(i, N) conv[S[i]] = conv.size() - 1;

    REP(i, M) g[i].clear();
    REP(i, M) {
      g[i].push_back(conv[O[i]]);
      REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
    }

    fill(cost, cost + 100, INF);
    REP(i, N) chmin(cost[conv[S[i]]], P[i]);

    REP(t, M - 1) {
      REP(i, M) {
        ll sum = 0;
        FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
        chmin(cost[g[i][0]], sum);
      }
    }

    cout << cost[conv[T]] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second

template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pll> pip;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;

int N, M;
int cost[100];
string S[100], O[100], Q[100][100], T;
int P[100], K[100];
vector<int> g[100];

int main() {
  while (cin >> N, N) {
    REP(i, N) cin >> S[i] >> P[i];
    cin >> M;
    REP(i, M) {
      cin >> O[i] >> K[i];
      REP(j, K[i]) cin >> Q[i][j];
    }
    cin >> T;

    map<string, int> conv;
    REP(i, N) conv[S[i]] = conv.size() - 1;

    REP(i, M) g[i].clear();
    REP(i, M) {
      g[i].push_back(conv[O[i]]);
      REP(j, K[i]) g[i].push_back(conv[Q[i][j]]);
    }

    fill(cost, cost + 200, INF);
    REP(i, N) chmin(cost[conv[S[i]]], P[i]);

    REP(t, M - 1) {
      REP(i, M) {
        ll sum = 0;
        FOR(j, 1, g[i].size()) sum += cost[g[i][j]];
        chmin(cost[g[i][0]], sum);
      }
    }

    cout << cost[conv[T]] << endl;
  }
  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

using namespace std;

map<string, int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x) {

  int len = recipe[x].size();

  if (len == 0) {
    return price[x];
  }

  int ans = 0;
  rep(i, len) { ans += solve(recipe[x][i]); }
  // printf("%d->%d\\n",x,price[x]);
  return min(ans, price[x]);
}

int main() {
  int n;
  while (cin >> n, n) {
    name_id.clear();
    rep(i, 100) recipe[i].clear();
    rep(i, n) {
      string name;
      int p;
      cin >> name >> p;
      name_id[name] = i;
      price[i] = p;
    }

    int m;
    cin >> m;

    rep(i, m) {
      string name;
      int num;
      cin >> name >> num;
      int id = name_id[name];
      rep(k, num) {
        string r;
        cin >> r;
        recipe[id].push_back(name_id[r]);
      }
    }

    string a;
    cin >> a;
    cout << solve(name_id[a]) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define reep(i, n, m) for (int i = n; i < m; i++)
#define rep(i, n) reep(i, 0, n)

using namespace std;

map<string, int> name_id;
vector<int> recipe[100];
int price[100];

int solve(int x) {

  int len = recipe[x].size();

  if (len == 0) {
    return price[x];
  }

  int ans = 0;
  rep(i, len) { ans += solve(recipe[x][i]); }
  // printf("%d->%d\\n",x,price[x]);
  return min(ans, price[x]);
}

int main() {
  int n;
  while (cin >> n, n) {
    rep(i, n) {
      string name;
      int p;
      cin >> name >> p;
      name_id[name] = i;
      price[i] = p;
    }

    int m;
    cin >> m;

    rep(i, m) {
      string name;
      int num;
      cin >> name >> num;
      int id = name_id[name];
      rep(k, num) {
        string r;
        cin >> r;
        recipe[id].push_back(name_id[r]);
      }
    }

    string a;
    cin >> a;
    cout << solve(name_id[a]) << endl;
  }
  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <map>
#include <string>
#include <vector>
using namespace std;

map<string, int> item;
map<string, vector<string>> recipe;
int n, m, k, money;
char name[101], o[101], material[101];

int dp(string str);

int main(void) {

  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%s %d", name, &money);
      item.insert(map<string, int>::value_type(name, money));
    }

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      scanf("%s %d", o, &k);
      for (int j = 0; j < k; j++) {
        scanf("%s", material);
        recipe[o].push_back(material);
      }
    }

    scanf("%s", name);
    printf("%d\\n", dp(name));

    item.clear();
    recipe.clear();
  }

  return (0);
}

int dp(string str) {
  int ans = 0;

  if (recipe[str].size() != 0) {
    for (int i = 0; i < recipe[str].size(); i++) {
      ans += dp(recipe[str][i]);
    }
  } else {
    ans = item[str];
  }

  if (item[str] < ans)
    ans = item[str];

  return ans;
}

Error: TLE

Fixed submission:

/*
 1 sÚ XgÉÜÜêéACeÌ n (®)
 2 sÚ æ 1 ÌACeÌîñ i p (¼p¶ñ
 ®;¼póæØè) 3 sÚ æ 2
 ÌACeÌîñ
 :
 n+1 sÚ æ n ÌACeÌîñ
 n+2 sÚ BàVsÌ m (®)
 n+3 sÚ æ 1 ÌBàVsÌîñ o k q1 q2 EEE qk
 (¼p¶ñ ® ¼p¶ñ EEE
 ¼p¶ñ;¼póæØè) n+4 sÚ æ 2
 ÌBàVsÌîñ
 :
 n+m+2 sÚ æ m ÌBàVsÌîñ
 n+m+3 sÚ wè³ê½ACe¼ (¼p¶ñ)

 ACeXgÉÜÜêéACeÌíÞ n Í 1 Èã 100
 ÈºÌ®
 eACeðwü·éêÌli p Í 0 Èã 1000000
 Èº
 ACeÌ¼O i Í 1 ¶Èã 100 ¶Èº
 ÌAt@xbg©çÈé¼p¶ñ
 BàVsÌíÞ m Í 0 Èã 100 ÈºÌ®
 1 ÂÌBàVsÍACeÌ¼O o
 A»êðìé½ßÉKvÈACeÌÂ k A k
 ÂÌACe ¼ q ÌXgÉæÁÄ^¦çêÜ·Bk Í
 100 ÈºÆµÜ·B
*/

#include <cstdio>
#include <map>
#include <string>
#include <vector>
using namespace std;

map<string, int> item;
map<string, vector<string>> recipe;
int n, m, k, money;
char name[101], o[101], material[101];

int dp(string str);

int main(void) {

  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%s %d", name, &money);
      item.insert(map<string, int>::value_type(name, money));
    }

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      scanf("%s %d", o, &k);
      for (int j = 0; j < k; j++) {
        scanf("%s", material);
        recipe[o].push_back(material);
      }
    }

    scanf("%s", name);
    printf("%d\\n", dp(name));

    item.clear();
  }

  return (0);
}

int dp(string str) {
  int ans = 0;

  if (recipe[str].size() != 0) {
    for (int i = 0; i < recipe[str].size(); i++) {
      ans += dp(recipe[str][i]);
    }
  } else {
    ans = item[str];
  }

  if (item[str] < ans)
    ans = item[str];

  return ans;
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int n, m;
struct Item {
  int id, price;
  string name;
  vector<int> edge;
  Item() {}
};
vector<Item> Graph;
int memo[101];

int Rec(int id) {
  if (memo[id] != -1)
    return memo[id];

  int price = Graph[id].price, cmp = 0;

  for (int i = 0; i < Graph[id].edge.size(); ++i)
    cmp += Rec(Graph[id].edge[i]);

  return memo[id] = (cmp == 0) ? price : min(price, cmp);
}

int main() {
  while (cin >> n, n) {
    string s, d;
    map<string, int> hash;

    Graph.resize(n);
    for (int i = 0; i < n; ++i) {
      memo[i] = -1;
      Graph[i].edge.clear();
    }

    for (int i = 0; i < n; ++i) {
      int p;

      cin >> s >> p;
      hash.insert(map<string, int>::value_type(s, i));
      Graph[i].id = i;
      Graph[i].price = p;
      Graph[i].name = s;
    }

    cin >> m;
    for (int i = 0; i < m; ++i) {
      int num;

      cin >> s >> num;
      for (int j = 0; j < num; ++j) {
        cin >> d;
        Graph[hash[s]].edge.push_back(hash[d]);
      }
    }

    cin >> s;
    cout << Rec(hash[s]) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int n, m;
struct Item {
  int id, price;
  string name;
  vector<int> edge;
  Item() {}
};
vector<Item> Graph;

int Rec(int id) {
  int price = Graph[id].price, cmp = 0;

  for (int i = 0; i < Graph[id].edge.size(); ++i)
    cmp += Rec(Graph[id].edge[i]);

  return (cmp == 0) ? price : min(price, cmp);
}

int main() {
  while (cin >> n, n) {
    string s, d;
    map<string, int> hash;

    Graph.resize(n);
    for (int i = 0; i < n; ++i) {
      int p;

      cin >> s >> p;
      hash.insert(map<string, int>::value_type(s, i));
      Graph[i].id = i;
      Graph[i].price = p;
      Graph[i].name = s;
    }

    cin >> m;
    for (int i = 0; i < m; ++i) {
      int num;

      cin >> s >> num;
      for (int j = 0; j < num; ++j) {
        cin >> d;
        Graph[hash[s]].edge.push_back(hash[d]);
      }
    }

    cin >> s;
    cout << Rec(hash[s]) << endl;
  }

  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

vector<int> list[100];
int cost[100];

int saiki(int n) {
  if (list[n].empty() == 1) {
    return cost[n];
  }
  int cont = 0;
  for (int i = 0; i < list[n].size(); i++) {
    cont += saiki(list[n][i]);
  }
  return min(cont, cost[n]);
}

int main() {

  while (1) {
    map<string, int> name;

    int n;
    scanf("%d", &n);

    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      string a;
      int b;
      cin >> a >> b;
      cost[i] = b;
      name[a] = i;
    }
    for (int i = 0; i < 100; i++) {
      list[i].clear();
    }
    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      string a;
      int b;
      cin >> a >> b;
      int num = name[a];
      for (int j = 0; j < b; j++) {
        string c;
        cin >> c;
        list[num].push_back(name[c]);
      }
    }
    string a;
    cin >> a;

    printf("%d\\n", saiki(name[a]));
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  bool sosuu[1000001];
  bool dp[1000001];

  while (1) {
    for (int i = 0; i <= 1000000; i++) {
      sosuu[i] = 0;
      dp[i] = 0;
    }
    for (int i = 2; i <= 500000; i++) {
      if (sosuu[i] == 0) {
        for (int j = i; j <= 1000000; j += i) {
          sosuu[j] = 1;
        }
        sosuu[i] = 0;
      }
    }
    sosuu[0] = 1;
    sosuu[1] = 1;

    int item[30];

    int n, m;
    scanf("%d%d", &n, &m);

    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &item[i]);
    }
    dp[0] = 1;
    for (int i = 0; i <= m; i++) {
      for (int j = 0; j < n; j++) {
        if (i < item[j]) {
          continue;
        }
        if (dp[i - item[j]] == 1) {
          dp[i] = 1;
        }
      }
    }
    int num = 0;
    for (int i = m; i >= 0; i--) {
      if (dp[i] == 1 && sosuu[i] == 0) {
        num = i;
        break;
      }
    }
    if (num == 0) {
      puts("NA");
    } else {
      printf("%d\\n", num);
    }
  }
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <list>
#include <stdio.h>
#include <string.h>

class item {
public:
  int pr;
  char name[101];
  std::list<int> ren;
};
item its[100];
int solve(int n) {
  int sum;
  std::list<int>::iterator it;
  if (its[n].ren.empty()) {
    return its[n].pr;
  }
  for (sum = 0, it = its[n].ren.begin(); it != its[n].ren.end(); it++) {
    sum += solve(*it);
    if (its[n].pr < sum)
      return its[n].pr;
  }
  return sum;
}
int main() {
  int i, j, n, m, k, l, o;
  char buf[400];
  using namespace std;
  list<int>::iterator it;
  do {
    scanf("%d", &n);
    if (!n)
      break;
    for (i = 0; i < n; i++)
      scanf("%s %d", its[i].name, &(its[i].pr));
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf("%s ", buf);
      for (j = 0; j < n; j++)
        if (!strcmp(buf, its[j].name))
          break;
      scanf("%d ", &k);
      for (l = 0; l < k - 1; l++) {
        scanf("%s ", buf);
        for (o = 0; o < n; o++)
          if (!strcmp(its[o].name, buf))
            break;
        its[j].ren.push_back(o);
      }
      scanf("%s", buf);
      for (o = 0; o < n; o++)
        if (!strcmp(its[o].name, buf))
          break;
      its[j].ren.push_back(o);
    }
    scanf("%s", buf);
    for (i = 0; i < n; i++)
      if (!strcmp(buf, its[i].name))
        break;
    printf("%d\\n", solve(i));
    for (i = 0; i < n; i++)
      its[i].ren.clear();
  } while (1);
  return 0;
}

Error: TLE

Fixed submission:

#include <list>
#include <stdio.h>
#include <string.h>

class item {
public:
  int pr;
  char name[101];
  std::list<int> ren;
};
item its[100];
int solve(int n) {
  int sum;
  std::list<int>::iterator it;
  if (its[n].ren.empty()) {
    return its[n].pr;
  }
  for (sum = 0, it = its[n].ren.begin(); it != its[n].ren.end(); it++) {
    sum += solve(*it);
  }
  return sum < its[n].pr ? sum : its[n].pr;
}
int main() {
  int i, j, n, m, k, l, o;
  char buf[400];
  using namespace std;
  list<int>::iterator it;
  do {
    scanf("%d", &n);
    if (!n)
      break;
    for (i = 0; i < n; i++)
      scanf("%s %d", its[i].name, &(its[i].pr));
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf("%s ", buf);
      for (j = 0; j < n; j++)
        if (!strcmp(buf, its[j].name))
          break;
      scanf("%d ", &k);
      for (l = 0; l < k - 1; l++) {
        scanf("%s ", buf);
        for (o = 0; o < n; o++)
          if (!strcmp(its[o].name, buf))
            break;
        its[j].ren.push_back(o);
      }
      scanf("%s", buf);
      for (o = 0; o < n; o++)
        if (!strcmp(its[o].name, buf))
          break;
      its[j].ren.push_back(o);
    }
    scanf("%s", buf);
    for (i = 0; i < n; i++)
      if (!strcmp(buf, its[i].name))
        break;
    printf("%d\\n", solve(i));
  } while (1);
  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int n;

typedef struct {
  char name[256];
  int cost;
  char used[100][256];
  int size;
} ITEM;

ITEM list[100];

int getCost(int vertex) {
  int i, j;
  int ans;

  if (list[vertex].size == 0) {
    return (list[vertex].cost);
  } else {
    ans = 0;
    for (i = 0; i < list[vertex].size; i++) {
      for (j = 0; j < n; j++) {
        if (strcmp(list[vertex].used[i], list[j].name) == 0) {
          ans += getCost(j);
        }
      }
    }
    list[vertex].cost = list[vertex].cost > ans ? ans : list[vertex].cost;
  }
  return (list[vertex].cost);
}

int main(void) {
  int i, j, k;
  int m, kk;
  char key[256], temp[256];
  int look, sum;

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

    if (n == 0) {
      break;
    }

    for (i = 0; i < n; i++) {
      scanf("%s%d", list[i].name, &list[i].cost);
      list[i].size = 0;
    }

    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf("%s", key);
      for (j = 0; j < n; j++) {
        if (strcmp(key, list[j].name) == 0) {
          look = j;
        }
      }
      scanf("%d", &kk);
      list[look].size = kk;
      for (j = 0; j < kk; j++) {
        scanf("%s", temp);
        for (k = 0; k < n; k++) {
          if (strcmp(temp, list[k].name) == 0) {
            strcpy(list[look].used[j], temp);
          }
        }
      }
    }
    scanf("%s", key);
    for (j = 0; j < n; j++) {
      if (strcmp(key, list[j].name) == 0) {
        look = j;
      }
    }

    printf("%d\\n", getCost(look));
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#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;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define EQ(a, b) (abs((a) - (b)) < EPS)
int n, m;
struct item {
  string name;
  int cost, size;
  vector<string> le;
};
item lis[100];
int cos[100];
int msearch(string s) {
  for (int i = 0; i < n; i++) {
    if (lis[i].name == s)
      return i;
  }
}
int getcost(int v) {
  if (cos[v] != -1)
    return cos[v];
  else {
    if (lis[v].size == 0)
      return lis[v].cost;
    else {
      int ans = 0;
      for (int i = 0; i < lis[v].size; i++) {
        ans += getcost(msearch(lis[v].le[i]));
      }
      cos[v] = ans;
      return ans;
    }
  }
}
int main() {
  while (1) {
    fill(cos, cos + 100, -1);
    cin >> n;
    string s;
    int c;
    for (int i = 0; i < n; i++) {
      cin >> s >> c;
      lis[i].name = s;
      lis[i].cost = c;
      lis[i].size = 0;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      int t, k;
      cin >> s;
      cin >> t;
      for (int j = 0; j < t; j++) {
        string st;
        cin >> st;
        k = msearch(s);
        lis[k].le.pb(st);
      }
      lis[k].size = t;
    }
    string str;
    cin >> str;
    cout << getcost(msearch(str)) << endl;
  }
  return 0;
}