Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
map<string, int> data;
map<string, vector<string>> ren;

int res(string s) {
  if (!ren.count(s))
    return data[s];
  int sum = 0;
  for (int i = 0; i < ren[s].size(); i++) {
    sum += res(ren[s][i]);
  }
  if (data[s] > sum) {
    data[s] = sum;
  }
  return data[s];
}

int main() {
  int n, m;
  while (cin >> n, n) {
    data.clear();
    ren.clear();
    for (int i = 0; i < n; i++) {
      string item;
      int cst;
      cin >> item >> cst;
      data[item] = cst;
    }
    cin >> m;
    int k;
    for (int i = 0; i < m; i++) {
      string renkin;
      cin >> renkin >> k;
      // int sum=0;
      for (int j = 0; j < k; j++) {
        string tmp;
        cin >> tmp;
        ren[renkin].push_back(tmp);
      }
      // data[renkin]=min(sum,data[renkin]);
    }
    string ans;
    cin >> ans;
    cout << res(ans) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
map<string, int> data;
map<string, vector<string>> ren;

int res(string s) {
  if (!ren.count(s))
    return data[s];
  int sum = 0;
  for (int i = 0; i < ren[s].size(); i++) {
    sum += res(ren[s][i]);
  }
  if (data[s] > sum) {
    data[s] = sum;
  }
  return data[s];
}

int main() {
  int n, m;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      string item;
      int cst;
      cin >> item >> cst;
      data[item] = cst;
    }
    cin >> m;
    int k;
    for (int i = 0; i < m; i++) {
      string renkin;
      cin >> renkin >> k;
      // int sum=0;
      for (int j = 0; j < k; j++) {
        string tmp;
        cin >> tmp;
        ren[renkin].push_back(tmp);
      }
      // data[renkin]=min(sum,data[renkin]);
    }
    string ans;
    cin >> ans;
    cout << res(ans) << endl;
  }
  return 0;
}