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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-8;
const double pi = acos(-1);
// int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
vector<int> kaijo, invkaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  invkaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
  rep(i, n + 1) invkaijo[i] = modpow(kaijo[i], mod - 2);
}
int comb(int a, int b) {
  if (a < b)
    return 0;
  if (a < 0 || b < 0)
    return 0;
  return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
         modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
void solve() {
  int n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  int dp[70][40][30][80][2];
  rep(i, 70) {
    rep(j, 40) {
      rep(k, 30) {
        rep(l, 80) { rep(m, 2) dp[i][j][k][l][m] = INF; }
      }
    }
  }
  dp[0][0][0][0][n % 2] = n;
  int ans = INF;
  rep(i, 69) {
    rep(j, 39) {
      rep(k, 29) {
        rep(l, 78) {
          rep(m, 2) {
            if (dp[i][j][k][l][m] != INF) {
              int s = dp[i][j][k][l][m];
              if (s % 2 == 0)
                chmin(dp[i + 1][j][k][l][(s / 2) % 2], s / 2);
              if (s % 2 == 1)
                chmin(dp[i + 1][j][k][l + 1][(s / 2) % 2], s / 2);
              if (s % 2 == 1)
                chmin(dp[i + 1][j][k][l + 1][(s / 2 + 1) % 2], s / 2 + 1);
              if (s % 3 == 0)
                chmin(dp[i][j + 1][k][l][(s / 3) % 2], s / 3);
              if (s % 3 == 1)
                chmin(dp[i][j + 1][k][l + 1][(s / 3) % 2], s / 3);
              if (s % 3 == 2)
                chmin(dp[i][j + 1][k][l + 1][(s / 3 + 1) % 2], s / 3 + 1);
              if (s % 5 == 0)
                chmin(dp[i][j][k + 1][l][(s / 5) % 2], s / 5);
              if (s % 5 == 1)
                chmin(dp[i][j][k + 1][l + 1][(s / 5) % 2], s / 5);
              if (s % 5 == 2)
                chmin(dp[i][j][k + 1][l + 2][(s / 5) % 2], s / 5);
              if (s % 5 == 3)
                chmin(dp[i][j][k + 1][l + 2][(s / 5 + 1) % 2], s / 5 + 1);
              if (s % 5 == 4)
                chmin(dp[i][j][k + 1][l + 1][(s / 5 + 1) % 2], s / 5 + 1);
            }
          }
        }
      }
    }
  }
  rep(i, 70) {
    rep(j, 40) {
      rep(k, 30) {
        rep(l, 80) {
          rep(m, 2) if (dp[i][j][k][l][m] < 70000000000 / d + 70)
              chmin(ans, i * a + j * b + k * c + l * d + dp[i][j][k][l][m] * d);
        }
      }
    }
  }
  cout << ans << endl;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define ll long long
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-8;
const double pi = acos(-1);
// int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi, int ma) {
  random_device rnd;
  mt19937 mt(rnd()); // 32bit
  //[mi,ma]
  uniform_int_distribution<int> engine(mi, ma);
  return engine(mt);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
vector<int> kaijo, invkaijo;
void init_fact(int n) {
  kaijo.resize(n + 1);
  invkaijo.resize(n + 1);
  kaijo[0] = 1;
  for (int i = 1; i <= n; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
  rep(i, n + 1) invkaijo[i] = modpow(kaijo[i], mod - 2);
}
int comb(int a, int b) {
  if (a < b)
    return 0;
  if (a < 0 || b < 0)
    return 0;
  return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
         modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
void solve() {
  int n, a, b, c, d;
  cin >> n >> a >> b >> c >> d;
  int dp[70][40][30][90][5];
  rep(i, 70) {
    rep(j, 40) {
      rep(k, 30) {
        rep(l, 90) {
          rep(m, 4) { dp[i][j][k][l][m] = INF; }
        }
      }
    }
  }
  rep(i, 4) dp[0][0][0][0][i] = n;
  int ans = INF;
  rep(i, 69) {
    rep(j, 39) {
      rep(k, 29) {
        rep(l, 88) {
          rep(m, 4) {
            if (dp[i][j][k][l][m] == INF)
              break;
            int s = dp[i][j][k][l][m];
            if (s % 2 == 0) {
              dp[i + 1][j][k][l][4] = (s >> 1);
              sort(dp[i + 1][j][k][l], dp[i + 1][j][k][l] + 5);
            }
            if (s % 2 == 1) {
              dp[i + 1][j][k][l + 1][4] = (s >> 1);
              sort(dp[i + 1][j][k][l + 1], dp[i + 1][j][k][l + 1] + 5);
            }
            if (s % 2 == 1) {
              dp[i + 1][j][k][l + 1][4] = (s >> 1) + 1;
              sort(dp[i + 1][j][k][l + 1], dp[i + 1][j][k][l + 1] + 5);
            }
            if (s % 3 == 0) {
              dp[i][j + 1][k][l][4] = s / 3;
              sort(dp[i][j + 1][k][l], dp[i][j + 1][k][l] + 5);
            }
            if (s % 3 == 1) {
              dp[i][j + 1][k][l + 1][4] = s / 3;
              sort(dp[i][j + 1][k][l + 1], dp[i][j + 1][k][l + 1] + 5);
            }
            if (s % 3 == 2) {
              dp[i][j + 1][k][l + 1][4] = s / 3 + 1;
              sort(dp[i][j + 1][k][l + 1], dp[i][j + 1][k][l + 1] + 5);
            }
            if (s % 5 == 0) {
              dp[i][j][k + 1][l][4] = s / 5;
              sort(dp[i][j][k + 1][l], dp[i][j][k + 1][l] + 5);
            }
            if (s % 5 == 1) {
              dp[i][j][k + 1][l + 1][4] = s / 5;
              sort(dp[i][j][k + 1][l + 1], dp[i][j][k + 1][l + 1] + 5);
            }
            if (s % 5 == 2) {
              dp[i][j][k + 1][l + 2][4] = s / 5;
              sort(dp[i][j][k + 1][l + 2], dp[i][j][k + 1][l + 2] + 5);
            }
            if (s % 5 == 3) {
              dp[i][j][k + 1][l + 2][4] = s / 5 + 1;
              sort(dp[i][j][k + 1][l + 2], dp[i][j][k + 1][l + 2] + 5);
            }
            if (s % 5 == 4) {
              dp[i][j][k + 1][l + 1][4] = s / 5 + 1;
              sort(dp[i][j][k + 1][l + 1], dp[i][j][k + 1][l + 1] + 5);
            }
          }
        }
      }
    }
  }
  rep(i, 70) {
    rep(j, 40) {
      rep(k, 30) {
        rep(l, 90) {
          if (dp[i][j][k][l][0] < 70000000000 / d + 70)
            chmin(ans, i * a + j * b + k * c + l * d + dp[i][j][k][l][0] * d);
        }
      }
    }
  }
  cout << ans << endl;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
  return 0;
}