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

#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast")
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
#ifdef local
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define f(i, l, r) for (int i = l; i <= r; ++i)
#define rf(i, r, l) for (int i = r; i >= l; --i)
#define all(x) (x).begin(), (x).end()
#define l l
#define r r
#define lch (o << 1)
#define rch (o << 1 | 1)
#define mid ((l + r) >> 1)
#define mem(x, y) memset(x, y, sizeof(x))
#define mod1(x) ((x >= mod) && (x -= mod))
#define mod2(x) ((x < 0) && (x += mod))

const ll inf = 1e18;
// const ll INF = INT64_MAX;
double eps = 1e-6;
const int mod = 998244353;
const int N = 1e2 + 10;
const double pi = acos(-1.0);
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
void add(ll &a, ll b) {
  a += b;
  mod1(a);
  mod2(a);
}
int len, C[N], clen;
int idc(int v) { return lower_bound(C + 1, C + len, v) - C; }
void initC() {
  sort(C + 1, C + clen);
  len = unique(C + 1, C + 1 + clen) - C - 1;
}

char s[N][N];
int id[N][N];
int belong[N * N];
vector<pii> Rnex[N * N], Lnex[N * N];
ll dp[2][2010];
ll val[10000];
ll dfs(int u, int L) {
  if (u >= 2000)
    return val[u];
  if (dp[L][u] != -1)
    return dp[L][u];
  dp[L][u] = inf;
  int R = L ^ 1;
  ll res = inf;
  if (L) {
    for (auto v : Lnex[u]) {
      int R2 = 1;
      int add = v.second;
      if (v.second == -1e9) {
        R2 = 0;
        add = 0;
      }
      res = min(res, val[u] + add + dfs(v.first, R ^ R2));
    }
  } else {
    for (auto v : Rnex[u]) {
      if (v.first == 23) {
        int yyyy = 1;
      }
      int R2 = 1;
      int add = v.second;
      if (v.second == -1e9) {
        R2 = 0;
        add = 0;
      }
      res = min(res, val[u] + add + dfs(v.first, R ^ R2));
    }
  }
  return dp[L][u] = res;
}

int main() {
  // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef local
  freopen("in.txt", "r", stdin);
  int start = clock();
#endif
  /*while (1) {
          int n; cin >> n;
          string s[20];
          int c[20];
          f(i, 1, n) {

                  cin >> s[i] >> c[i]; C[++clen] = c[i];
          }
          initC();
          cout << n << '\\n';
          f(i, 1, n) {
                  cout << s[i] << " " << idc(c[i])*idc(c[i]) << '\\n';
          }
          return 0;
  }*/

  int n;
  cin >> n;
  int tot = 0;
  f(i, 1, n) {
    int C;
    cin >> s[i] + 1 >> C;
    int w = strlen(s[i] + 1);
    f(j, 1, w) {
      id[i][j] = ++tot;
      val[tot] = C;
      belong[tot] = i;
    }
    id[i][0] = id[i][w + 1] = 2000 + tot;
    val[2000 + tot] = C;
    belong[2000 + tot] = i;
  }
  f(i, 1, n) {
    int w = strlen(s[i] + 1);
    f(j, 1, w) {
      f(k, 1, n) {
        int w2 = strlen(s[k] + 1);
        int x, x2;
        for (x = j, x2 = w2; x <= w && x2 >= 1; x++, x2--) {
          if (s[i][x] != s[k][x2])
            break;
        }
        vector<pii> &now = Rnex[id[i][j]];
        if (x == w + 1) {
          // debug("R",i, j, k, x2);
          now.push_back({id[k][x2], -1e9});
        }
        if (x2 == 0) {
          // debug("R",i, j, i, x);
          now.push_back({id[i][x], val[id[k][1]] - val[id[i][1]]});
        }
      }
      f(k, 1, n) {
        int w2 = strlen(s[k] + 1);
        int x, x2;
        for (x = j, x2 = 1; x >= 1 && x2 <= w2; x--, x2++) {
          if (s[i][x] != s[k][x2])
            break;
        }
        vector<pii> &now = Lnex[id[i][j]];
        if (x == 0) {
          now.push_back({id[k][x2], -1e9});
        }
        if (x2 == w2 + 1) {
          now.push_back({id[i][x], val[id[k][1]] - val[id[i][1]]});
        }
      }
    }
  }
  mem(dp, -1);
  ll res = inf;
  f(i, 1, n) {
    int w = strlen(s[i] + 1);
    res = min(res, dfs(id[i][1], 0));
    debug(i, 0, res);
    res = min(res, dfs(id[i][w], 1));
    debug(i, 1, res);
    f(j, 1, w) {
      bool Lpol = 1;
      int l = 1, r = j - 1;
      while (l <= r) {
        if (s[i][l] != s[i][r])
          Lpol = 0;
        l++, r--;
      }
      if (Lpol) {
        res = min(res, dfs(id[i][j], 0));
        debug(i, j, 0, res);
      }

      bool Rpol = 1;
      l = j + 1, r = w;
      while (l <= r) {
        if (s[i][l] != s[i][r])
          Rpol = 0;
        l++, r--;
      }
      if (Rpol) {
        res = min(res, dfs(id[i][j], 1));
        debug(i, j, 1, res);
      }
    }
    bool Lpol = 1;
    int l = 1, r = w;
    while (l <= r) {
      if (s[i][l] != s[i][r])
        Lpol = 0;
      l++, r--;
    }
    if (Lpol) {
      res = min(res, val[id[i][1]]);
    }
  }
  debug(res);
  if (res == inf)
    cout << -1;
  else
    cout << res;

#ifdef local
  int end = clock();
  // cout << '\\n' << end - start << "ms";
#endif * /
}

Error: -6
56d36beb-bbec-42fd-bedd-ecb3339f60b1.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02587/C++/s807492888.cpp:85: int main(): Assertion `w>=15' failed.


Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast")
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
#ifdef local
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define f(i, l, r) for (int i = l; i <= r; ++i)
#define rf(i, r, l) for (int i = r; i >= l; --i)
#define all(x) (x).begin(), (x).end()
#define l l
#define r r
#define lch (o << 1)
#define rch (o << 1 | 1)
#define mid ((l + r) >> 1)
#define mem(x, y) memset(x, y, sizeof(x))
#define mod1(x) ((x >= mod) && (x -= mod))
#define mod2(x) ((x < 0) && (x += mod))

const ll inf = 1e18;
// const ll INF = INT64_MAX;
double eps = 1e-6;
const int mod = 998244353;
const int N = 1e2 + 10;
const double pi = acos(-1.0);
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
void add(ll &a, ll b) {
  a += b;
  mod1(a);
  mod2(a);
}
/*int len, C[N], clen;
int id(int v) { return lower_bound(C + 1, C + len, v) - C; }
void initC() { sort(C + 1, C + clen); len = unique(C + 1, C + 1 + clen) - C - 1;
}*/

char s[N][N];
int id[N][N];
int belong[N * N];
vector<pii> Rnex[N * N], Lnex[N * N];
ll dp[2][2010];
ll val[10000];
ll dfs(int u, int L) {
  if (u >= 2000)
    return val[u];
  if (dp[L][u] != -1)
    return dp[L][u];
  dp[L][u] = inf;
  int R = L ^ 1;
  ll res = inf;
  if (L) {
    for (auto v : Lnex[u]) {
      int R2 = 0;
      if (belong[v.first] == belong[u])
        R2 = 1;
      res = min(res, val[u] + v.second + dfs(v.first, R ^ R2));
    }
  } else {
    for (auto v : Rnex[u]) {
      int R2 = 0;
      if (belong[v.first] == belong[u])
        R2 = 1;
      res = min(res, val[u] + v.second + dfs(v.first, R ^ R2));
    }
  }
  return dp[L][u] = res;
}

int main() {
  // ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifdef local
  freopen("in.txt", "r", stdin);
  int start = clock();
#endif
  int n;
  cin >> n;
  int tot = 0;
  f(i, 1, n) {
    int C;
    cin >> s[i] + 1 >> C;
    int w = strlen(s[i] + 1);
    assert(w >= 15);
    f(j, 1, w) {
      id[i][j] = ++tot;
      val[tot] = C;
      belong[tot] = i;
    }
    id[i][0] = id[i][w + 1] = 2000 + tot;
    val[2000 + tot] = C;
    belong[2000 + tot] = i;
  }
  f(i, 1, n) {
    int w = strlen(s[i] + 1);
    f(j, 1, w) {
      f(k, 1, n) {
        int w2 = strlen(s[k] + 1);
        int x, x2;
        for (x = j, x2 = w2; x <= w && x2 >= 1; x++, x2--) {
          if (s[i][x] != s[k][x2])
            break;
        }
        vector<pii> &now = Rnex[id[i][j]];
        if (x == w + 1) {
          // debug("R",i, j, k, x2);
          now.push_back({id[k][x2], 0});
        }
        if (x2 == 0) {
          // debug("R",i, j, i, x);
          now.push_back({id[i][x], val[id[k][1]] - val[id[i][1]]});
        }
      }
      f(k, 1, n) {
        int w2 = strlen(s[k] + 1);
        int x, x2;
        for (x = j, x2 = 1; x >= 1 && x2 <= w2; x--, x2++) {
          if (s[i][x] != s[k][x2])
            break;
        }
        vector<pii> &now = Lnex[id[i][j]];
        if (x == 0) {
          now.push_back({id[k][x2], 0});
        }
        if (x2 == w2 + 1) {
          now.push_back({id[i][x], val[id[k][1]] - val[id[i][1]]});
        }
      }
    }
  }
  mem(dp, -1);
  ll res = inf;
  f(i, 1, n) {
    int w = strlen(s[i] + 1);
    res = min(res, dfs(id[i][1], 0));
    res = min(res, dfs(id[i][w], 1));
    f(j, 1, w) {
      bool Lpol = 1;
      int l = 1, r = j - 1;
      while (l <= r) {
        if (s[i][l] != s[i][r])
          Lpol = 0;
        l++, r--;
      }
      if (Lpol) {
        res = min(res, dfs(id[i][j], 0)); // debug(i, j, 0, res);
      }

      bool Rpol = 1;
      l = j + 1, r = w;
      while (l <= r) {
        if (s[i][l] != s[i][r])
          Rpol = 0;
        l++, r--;
      }
      if (Rpol) {
        res = min(res, dfs(id[i][j], 1)); // debug(i, j, 1, res);
      }
    }
    bool Lpol = 1;
    int l = 1, r = w;
    while (l <= r) {
      if (s[i][l] != s[i][r])
        Lpol = 0;
      l++, r--;
    }
    if (Lpol) {
      res = min(res, val[id[i][1]]);
    }
  }
  assert(res >= 1);
  if (res == inf)
    cout << -1;
  else
    cout << res;

#ifdef local
  int end = clock();
  // cout << '\\n' << end - start << "ms";
#endif * /
}