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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
map<pair<string, ll>, ll> mp, visit1;
ll cost[52], flag, n;
string ar[52];
pair<string, ll> compSuff(string s1, string s2) {
  ll i, j, f = 1, k;
  i = 0;
  j = s2.size() - 1;
  while (i < s1.size() && j >= 0) {
    if (s1[i] != s2[j]) {
      f = 0;
      break;
    }
    i++;
    j--;
  }
  string str = "";
  if (i == s1.size()) {
    for (k = j; k >= 0; k--) {
      str += s2[k];
    }
  } else {
    for (k = i; k < s1.size(); k++) {
      str += s1[k];
    }
  }
  pair<string, ll> p = {str, f};
  return p;
}
pair<string, ll> compPref(string s1, string s2) {
  ll i, j, k, f = 1;
  i = 0;
  j = 0;
  while (i < s1.size() && j < s2.size()) {
    if (s1[i] != s2[j]) {
      f = 0;
      break;
    }
    i++;
    j++;
  }
  string str = "";
  if (i == s1.size()) {
    for (k = j; k < s2.size(); k++) {
      str += s2[k];
    }
  } else {
    for (k = i; k < s1.size(); k++) {
      str += s1[k];
    }
  }
  pair<string, ll> p = {str, f};
  return p;
}
ll checkPal(string s) {
  string str = "";
  ll j;
  for (j = s.size() - 1; j >= 0; j--) {
    str += s[j];
  }
  if (str == s)
    return 1;
  else
    return 0;
}
ll rec(string str, ll side) {
  pair<string, ll> p = {str, side};
  ll z = checkPal(str);
  if (z) {
    flag = 1;
    return 0;
  }
  if (mp[p] != 0)
    return mp[p];
  if (visit1[p] == 1)
    return 1e17;
  visit1[p] = 1;
  ll min1 = 1e17;
  for (ll i = 1; i <= n; i++) {
    pair<string, ll> z;
    if (side == 0)
      z = compSuff(str, ar[i]);
    else
      z = compPref(str, ar[i]);
    if (z.second == 1) {
      if (str.size() >= ar[i].size())
        min1 = min(min1, cost[i] + rec(z.first, side));
      else
        min1 = min(min1, cost[i] + rec(z.first, !side));
    }
  }
  return mp[p] = min1;
}
int main() {
  ll i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ar[i] >> cost[i];
  }
  ll min1 = 1e17;
  for (i = 1; i <= n; i++) {
    min1 = min(min1, cost[i] + rec(ar[i], 0));
  }
  if (flag == 0)
    cout << -1;
  else
    cout << min1;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define LL long long

const int MOD = 1e9 + 7;
const LL INF = 1e18 + 9;
const int MX = 1e5 + 5;

int n;
vector<string> inp;
vector<LL> cost;

bool isPalindrome(string s) {
  int l = s.size();
  for (int i = 0; i < l; i++) {
    if (s[i] != s[l - 1 - i])
      return false;
  }
  return true;
}

vector<string> getNextState(string s1, string s2) {
  vector<string> ret;

  int l1 = s1.size();
  int l2 = s2.size();
  int l = min(l1, l2);
  for (int i = 0; i < l; i++) {
    if (s1[i] != s2[l2 - 1 - i]) {
      return ret;
    }
  }
  if (l1 == l2) {
    ret.push_back("");
    ret.push_back("");
  } else if (l1 > l2) {
    string tmp = s1.substr(l2);
    ret.push_back(tmp);
    ret.push_back("");
  } else {
    string tmp = s2.substr(0, l2 - l1);
    ret.push_back("");
    ret.push_back(tmp);
  }
  return ret;
}

map<pair<string, string>, LL> cache;

LL solve(string pre, string suff) {
  if (isPalindrome(pre + suff)) {
    return 0;
  }

  if (cache.find({pre, suff}) != cache.end()) {
    return cache[{pre, suff}];
  }

  LL ret = INF;

  for (int i = 0; i < n; i++) {
    vector<string> cand;
    if (pre.size() > 0) {
      cand = getNextState(pre, inp[i]);
    } else {
      cand = getNextState(inp[i], suff);
    }

    if (cand.size() == 0) {
      continue;
    }

    ret = min(ret, cost[i] + solve(cand[0], cand[1]));
  }

  cache[{pre, suff}] = ret;
  return ret;
}

int main() {
  cin >> n;
  inp.resize(n);
  cost.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> inp[i] >> cost[i];
  }

  LL ans = INF;
  for (int i = 0; i < n; i++) {
    ans = min(ans, cost[i] + solve(inp[i], ""));
  }
  if (ans == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;

  return 0;
}