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

#include <algorithm>
#include <stdio.h>
#include <utility>
using namespace std;
int t, ii, jj, kk, x, y;
long long p;
long long n, a, b, c, d;
long long l;
pair<long long, long long> r[20000];
int table[64][50][30];
long long ansl[20000];
long long ansr[20000];
long long two;
long long lc, rc;
double ac, bc;
int main() {
  l = 1000000000;
  l *= 1500000000;
  l += 6;
  p = 1;
  while (p < l) {
    while (p < l) {
      while (p < l) {
        r[x].first = p;
        r[x].second = ii * 10000 + jj * 100 + kk;
        x++;
        p *= 5;
        kk++;
      }
      for (int e = 0; e < kk; e++) {
        p /= 5;
      }
      kk = 0;
      p *= 3;
      jj++;
    }
    for (int e = 0; e < jj; e++) {
      p /= 3;
    }
    jj = 0;
    p *= 2;
    ii++;
  }
  std::sort(r, r + x);
  for (int i = 0; i < x; i++) {
    ii = r[i].second / 10000;
    jj = r[i].second / 100 % 100;
    kk = r[i].second % 100;
    table[ii][jj][kk] = i;
  }
  scanf("%d", &t);
  for (int q = 0; q < t; q++) {
    scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &c, &d);
    for (int i = 0; i < x; i++) {
      ansl[i] = l;
      ansr[i] = l;
      ac = n / r[i].first;
      ac *= d;
      bc = l;
      bc *= 2;
      if (ac < bc) {
        if (n / r[i].first * d < l)
          ansl[i] = n / r[i].first * d;
      }
    }
    two = d + d;
    if (a + d < two)
      two = a + d;
    for (int i = x - 1; i >= 0; i--) {
      if (n / r[i].first < 1) {
        ansl[i] = 0;
        ansr[i] = d;
      } else if (n / r[i].first < 2) {
        ansl[i] = d;
        ansr[i] = two;
      }
      if (ansl[i] + d < ansr[i])
        ansr[i] = ansl[i] + d;
      if (ansr[i] + d < ansl[i])
        ansl[i] = ansr[i] + d;
      ii = r[i].second / 10000;
      jj = r[i].second / 100 % 100;
      kk = r[i].second % 100;
      if (ii) {
        y = table[ii - 1][jj][kk];
        lc = ansl[i] + a;
        lc += d * (n / r[y].first - n / r[i].first * 2);
        if (ansl[y] > lc)
          ansl[y] = lc;
        rc = ansr[i] + a;
        rc += d * ((n / r[i].first + 1) * 2 - (n / r[y].first + 1));
        if (ansr[y] > rc)
          ansr[y] = rc;
      }
      if (jj) {
        y = table[ii][jj - 1][kk];
        lc = ansl[i] + b;
        lc += d * (n / r[y].first - n / r[i].first * 3);
        if (ansl[y] > lc)
          ansl[y] = lc;
        rc = ansr[i] + b;
        rc += d * ((n / r[i].first + 1) * 3 - (n / r[y].first + 1));
        if (ansr[y] > rc)
          ansr[y] = rc;
      }
      if (kk) {
        y = table[ii][jj][kk - 1];
        lc = ansl[i] + c;
        lc += d * (n / r[y].first - n / r[i].first * 5);
        if (ansl[y] > lc)
          ansl[y] = lc;
        rc = ansr[i] + c;
        rc += d * ((n / r[i].first + 1) * 5 - (n / r[y].first + 1));
        if (ansr[y] > rc)
          ansr[y] = rc;
      }
    }
    printf("%lld\\n", ansl[0]);
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <utility>
using namespace std;
int t, ii, jj, kk, x, y;
long long p;
long long n, a, b, c, d;
long long l;
pair<long long, long long> r[11000];
int table[64][50][30];
long long ansl[11000];
long long ansr[11000];
long long two;
long long lc, rc;
double ac, bc;
int main() {
  l = 1000000000;
  l *= 2000000000;
  l += 6;
  p = 1;
  while (p < l) {
    while (p < l) {
      while (p < l) {
        r[x].first = p;
        r[x].second = ii * 10000 + jj * 100 + kk;
        x++;
        p *= 5;
        kk++;
      }
      for (int e = 0; e < kk; e++) {
        p /= 5;
      }
      kk = 0;
      p *= 3;
      jj++;
    }
    for (int e = 0; e < jj; e++) {
      p /= 3;
    }
    jj = 0;
    p *= 2;
    ii++;
  }
  std::sort(r, r + x);
  for (int i = 0; i < x; i++) {
    ii = r[i].second / 10000;
    jj = r[i].second / 100 % 100;
    kk = r[i].second % 100;
    table[ii][jj][kk] = i;
  }
  scanf("%d", &t);
  for (int q = 0; q < t; q++) {
    scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &c, &d);
    for (int i = 0; i < x; i++) {
      ansl[i] = l;
      ansr[i] = l;
      ac = n / r[i].first;
      ac *= d;
      bc = l;
      bc *= 2;
      if (ac < bc) {
        if (n / r[i].first * d < l)
          ansl[i] = n / r[i].first * d;
      }
    }
    two = d + d;
    if (a + d < two)
      two = a + d;
    for (int i = x - 1; i >= 0; i--) {
      if (n / r[i].first < 1) {
        ansl[i] = 0;
        ansr[i] = d;
      } else if (n / r[i].first < 2) {
        ansl[i] = d;
        ansr[i] = two;
      }
      if (ansl[i] + d < ansr[i])
        ansr[i] = ansl[i] + d;
      if (ansr[i] + d < ansl[i])
        ansl[i] = ansr[i] + d;
      ii = r[i].second / 10000;
      jj = r[i].second / 100 % 100;
      kk = r[i].second % 100;
      if (ii) {
        y = table[ii - 1][jj][kk];
        lc = ansl[i] + a;
        lc += d * (n / r[y].first - n / r[i].first * 2);
        if (ansl[y] > lc)
          ansl[y] = lc;
        rc = ansr[i] + a;
        rc += d * ((n / r[i].first + 1) * 2 - (n / r[y].first + 1));
        if (ansr[y] > rc)
          ansr[y] = rc;
      }
      if (jj) {
        y = table[ii][jj - 1][kk];
        lc = ansl[i] + b;
        lc += d * (n / r[y].first - n / r[i].first * 3);
        if (ansl[y] > lc)
          ansl[y] = lc;
        rc = ansr[i] + b;
        rc += d * ((n / r[i].first + 1) * 3 - (n / r[y].first + 1));
        if (ansr[y] > rc)
          ansr[y] = rc;
      }
      if (kk) {
        y = table[ii][jj][kk - 1];
        lc = ansl[i] + c;
        lc += d * (n / r[y].first - n / r[i].first * 5);
        if (ansl[y] > lc)
          ansl[y] = lc;
        rc = ansr[i] + c;
        rc += d * ((n / r[i].first + 1) * 5 - (n / r[y].first + 1));
        if (ansr[y] > rc)
          ansr[y] = rc;
      }
    }
    for (int i = 0; i < 20; i++) {
      // printf("%lld (%lld %lld) %lld %lld\\n", r[i].first, n / r[i].first, n /
      // r[i].first + 1, ansl[i], ansr[i]);
    }
    printf("%lld\\n", ansl[0]);
  }
}