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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define int long long
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a, b) memset(a, b, sizeof(a))
#define lwb lower_bound
#define upb upper_bound
using namespace std;
V<P<int, int>> v[50005];
char c[55][25];
int l[55], id[55][25][25][2], d[50005], used[50005], en[50005], cost[55];
bool yes(int i, int j, int k, int nxti, bool f) {
  if (!f) {
    FOR(t, j, k) {
      if (l[nxti] - t + j <= 0)
        RE 1;
      if (c[i][t] != c[nxti][l[nxti] - t + j]) {
        RE 0;
      }
    }
    RE 1;
  } else {
    for (int t = k; t >= j; t--) {
      if (k - t + 1 > l[nxti])
        RE 1;
      if (c[i][t] != c[nxti][k - t + 1])
        RE 0;
    }
    RE 1;
  }
}
signed main() {
  int n;
  scanf("%lld", &n);
  FOR(i, 1, n) {
    scanf("%s%lld", c[i] + 1, &cost[i]);
    l[i] = strlen(c[i] + 1);
  }
  int cnt = 0;
  FOR(i, 1, n) {
    FOR(j, 1, l[i]) {
      FOR(k, j - 1, l[i]) id[i][j][k][0] = ++cnt, id[i][j][k][1] = ++cnt;
    }
    id[i][l[i] + 1][l[i]][0] = ++cnt;
    id[i][l[i] + 1][l[i]][1] = ++cnt;
  }
  FILL(d, -1);
  priority_queue<P<int, int>, V<P<int, int>>, greater<P<int, int>>> q;
  FOR(i, 1, n) {
    d[id[i][1][l[i]][0]] = d[id[i][1][l[i]][1]] = cost[i];
    q.emplace(MP(cost[i], id[i][1][l[i]][0]));
    q.emplace(MP(cost[i], id[i][1][l[i]][1]));
  }
  FOR(i, 1, n) {
    en[id[i][l[i] + 1][l[i]][0]] = en[id[i][l[i] + 1][l[i]][1]] = 1;
    FOR(j, 1, l[i]) {
      en[id[i][j][j - 1][0]] = en[id[i][j][j - 1][1]] = 1;
      FOR(k, j, l[i]) {
        bool f = 1;
        FOR(it, j, k) {
          if (c[i][it] != c[i][j + k - it]) {
            f = 0;
            break;
          }
        }
        if (f) {
          en[id[i][j][k][0]] = en[id[i][j][k][1]] = 1;
        }
        FOR(nxti, 1, n) {
          if (yes(i, j, k, nxti, 0)) {
            if (l[nxti] > k - j + 1) {
              v[id[i][j][k][0]].PB(
                  MP(id[nxti][1][l[nxti] - k + j - 1][1], cost[nxti]));
            } else {
              v[id[i][j][k][0]].PB(MP(id[i][j + l[nxti]][k][0], cost[nxti]));
            }
          }
          if (yes(i, j, k, nxti, 1)) {
            if (l[nxti] > k - j + 1) {
              v[id[i][j][k][1]].PB(
                  MP(id[nxti][k - j + 2][l[nxti]][0], cost[nxti]));
            } else {
              v[id[i][j][k][1]].PB(MP(id[i][j][k - l[nxti]][1], cost[nxti]));
            }
          }
        }
      }
    }
  }
  while (!q.empty()) {
    P<int, int> cur = q.top();
    q.pop();
    if (used[cur.S]) {
      continue;
    }
    //		cout<<cur.S<<'\\n';
    used[cur.S] = 1;
    if (en[cur.S]) {
      cout << cur.F;
      RE 0;
    }
    for (auto u : v[cur.S]) {
      if (!used[u.F]) {
        if (d[u.F] == -1) {
          d[u.F] = d[cur.S] + u.S;
        } else
          gmin(d[u.F], d[cur.S] + u.S);
        q.emplace(MP(d[u.F], u.F));
      }
    }
  }
  cout << -1;
  RE 0;
}


Error: -11

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define int long long
#define gmax(x, y) x = max(x, y)
#define gmin(x, y) x = min(x, y)
#define F first
#define S second
#define P pair
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define V vector
#define RE return
#define ALL(a) a.begin(), a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a, b) memset(a, b, sizeof(a))
#define lwb lower_bound
#define upb upper_bound
using namespace std;
V<P<int, int>> v[50005];
char c[55][25];
int l[55], id[55][25][25][2], d[50005], used[50005], en[50005], cost[55];
bool yes(int i, int j, int k, int nxti, bool f) {
  if (!f) {
    FOR(t, j, k) {
      if (l[nxti] - t + j <= 0)
        RE 1;
      if (c[i][t] != c[nxti][l[nxti] - t + j]) {
        RE 0;
      }
    }
    RE 1;
  } else {
    for (int t = k; t >= j; t--) {
      if (t - k + 1 > l[nxti])
        RE 1;
      if (c[i][t] != c[nxti][t - k + 1])
        RE 0;
    }
    RE 1;
  }
}
signed main() {
  int n;
  scanf("%d", &n);
  FOR(i, 1, n) {
    scanf("%s%d", c[i] + 1, &cost[i]);
    l[i] = strlen(c[i] + 1);
  }
  int cnt = 0;
  FOR(i, 1, n) {
    FOR(j, 1, l[i]) {
      FOR(k, j - 1, l[i]) id[i][j][k][0] = ++cnt, id[i][j][k][1] = ++cnt;
    }
  }
  FILL(d, -1);
  priority_queue<P<int, int>, V<P<int, int>>, greater<P<int, int>>> q;
  FOR(i, 1, n) {
    d[id[i][1][l[i]][0]] = d[id[i][1][l[i]][1]] = cost[i];
    q.emplace(MP(cost[i], id[i][1][l[i]][0]));
    q.emplace(MP(cost[i], id[i][1][l[i]][1]));
  }
  FOR(i, 1, n) {
    FOR(j, 1, l[i]) {
      en[id[i][j][j - 1][0]] = en[id[i][j][j - 1][1]] = 1;
      FOR(k, j, l[i]) {
        bool f = 1;
        FOR(it, j, k) {
          if (c[i][it] != c[i][j + k - it]) {
            f = 0;
            break;
          }
        }
        if (f) {
          en[id[i][j][k][0]] = en[id[i][j][k][1]] = 1;
        }
        FOR(nxti, 1, n) {
          if (yes(i, j, k, nxti, 0)) {
            if (l[nxti] > k - j + 1) {
              v[id[i][j][k][0]].PB(
                  MP(id[nxti][1][l[nxti] - k + j - 1][1], cost[nxti]));
            } else {
              v[id[i][j][k][0]].PB(MP(id[i][j + l[nxti]][k][0], cost[nxti]));
            }
          }
          if (yes(i, j, k, nxti, 1)) {
            if (l[nxti] > k - j + 1) {
              v[id[i][j][k][1]].PB(
                  MP(id[nxti][k - j + 2][l[nxti]][0], cost[nxti]));
            } else {
              v[id[i][j][k][1]].PB(MP(id[i][j][k - l[nxti]][1], cost[nxti]));
            }
          }
        }
      }
    }
  }
  while (!q.empty()) {
    P<int, int> cur = q.top();
    q.pop();
    if (used[cur.S]) {
      continue;
    }
    //		cout<<cur.S<<'\\n';
    used[cur.S] = 1;
    if (en[cur.S]) {
      cout << cur.F;
      RE 0;
    }
    for (auto u : v[cur.S]) {
      if (!used[u.F]) {
        if (d[u.F] == -1) {
          d[u.F] = d[cur.S] + u.S;
        } else
          gmin(d[u.F], d[cur.S] + u.S);
        q.emplace(MP(d[u.F], u.F));
      }
    }
  }
  cout << -1;
  RE 0;
}