alexjercan/bugnet huggingface dataset:
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

// WA => CARO!
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fastcin                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
#define endl '\\n'

void mian();

signed main() {
  int _t_;
  cin >> _t_;
  while (_t_--)
    mian();
}

ll a, b, c, d;
map<ll, ll> calculated;
map<ll, ll> values;

ll cost(ll n) {
  if (n <= 1)
    return d * n;
  if (calculated.count(n))
    return calculated[n];
  ll res = d * n;
  if ((double)d * (double)n > 1.1e18)
    res = 1e18;
  for (auto x : values) {
    ll p = x.first, price = x.second;
    ll uppern = ((n - 1) / p + 1) * p;
    ll lowern = (n / p) * p;
    //	cout << "for n=" << n << "upper/lower is " << uppern << " " << lowern <<
    //endl; 	cout << n << "called" << uppern/p << endl;
    res = min(res, d * (uppern - n) + price + cost(uppern / p));
    //	cout << n << "called" << lowern/p << endl;
    res = min(res, d * (n - lowern) + price + cost(lowern / p));
  }
  calculated[n] = res;
  return res;
}

void mian() {
  calculated.clear();
  values.clear();
  ll n;
  cin >> n >> a >> b >> c >> d;
  values[2] = a;
  values[3] = b;
  values[5] = c;
  cout << cost(n) << endl;
}


Error: 0

Fixed submission:

// WA => CARO!
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define fastcin                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);
#define endl '\\n'

int mian();

signed main() {
  int _t_;
  cin >> _t_;
  while (_t_--)
    mian();
}

ll a, b, c, d;
map<ll, ll> calculated;
map<ll, ll> values;

ll cost(ll n) {
  if (n <= 1)
    return d * n;
  if (calculated.count(n))
    return calculated[n];
  ll res = d * n;
  if ((double)d * (double)n > 1.1e18)
    res = 1e18;
  for (auto x : values) {
    ll p = x.first, price = x.second;
    ll uppern = ((n - 1) / p + 1) * p;
    ll lowern = (n / p) * p;
    //	cout << "for n=" << n << "upper/lower is " << uppern << " " << lowern <<
    //endl; 	cout << n << "called" << uppern/p << endl;
    res = min(res, d * (uppern - n) + price + cost(uppern / p));
    //	cout << n << "called" << lowern/p << endl;
    res = min(res, d * (n - lowern) + price + cost(lowern / p));
  }
  calculated[n] = res;
  return res;
}

int mian() {
  calculated.clear();
  values.clear();
  ll n;
  cin >> n >> a >> b >> c >> d;
  values[2] = a;
  values[3] = b;
  values[5] = c;
  cout << cost(n) << endl;
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define MAXN 100100
#define ll unsigned long long
ll n, m, cnt, A, B, C, D, t, ans;
map<ll, ll> dp;
ll solve(ll u) {
  if (u == 1)
    return D;
  if (u == 0)
    return 0;
  if (dp[u] != 0)
    return dp[u];
  ll Min = u * D;
  Min = min(Min, solve((u - u % 2) / 2) + A + u % 2 * D);
  if (u % 3 <= 1)
    Min = min(Min, solve((u - u % 3) / 3) + B + u % 3 * D);
  else
    Min = min(Min, solve((u + 3 - u % 3) / 3) + B + (3 - u % 3) * D);
  if (u % 5 <= 3)
    Min = min(Min, solve((u - u % 5) / 5) + C + u % 5 * D);
  if (u % 5 >= 3)
    Min = min(Min, solve((u + 5 - u % 5) / 5) + C + (5 - u % 5) * D);
  if (u % 2 != 0)
    Min = min(Min, solve((u + 2 - u % 2) / 2) + A + (2 - u % 2) * D);
  dp[u] = Min;
  return Min;
}
int main() {
  scanf("%lld", &t);
  while (t--) {
    dp.clear();
    //		memset(dp, 0, sizeof(dp));
    scanf("%lld", &n);
    scanf("%lld%lld%lld%lld", &A, &B, &C, &D);
    ans = solve(n);
    printf("%lld\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
#define MAXN 100100
#define ll long long
ll n, m, cnt, A, B, C, D, t, dp[1001000], ans;
ll solve(ll u) {
  if (u == 1)
    return D;
  if (u == 0)
    return 0;
  if (u <= 1000000 && dp[u] != 0)
    return dp[u];
  ll Min = u * D;
  Min = min(Min, solve((u - u % 2) / 2) + A + u % 2 * D);
  Min = min(Min, solve((u - u % 3) / 3) + B + u % 3 * D);
  Min = min(Min, solve((u - u % 5) / 5) + C + u % 5 * D);
  if (u % 2 != 0)
    Min = min(Min, solve((u + 2 - u % 2) / 2) + A + (2 - u % 2) * D);
  if (u % 3 != 0)
    Min = min(Min, solve((u + 3 - u % 3) / 3) + B + (3 - u % 3) * D);
  if (u % 5 != 0)
    Min = min(Min, solve((u + 5 - u % 5) / 5) + C + (5 - u % 5) * D);
  if (u <= 1000000)
    dp[u] = Min;
  return Min;
}
int main() {
  scanf("%lld", &t);
  while (t--) {
    memset(dp, 0, sizeof(dp));
    scanf("%lld", &n);
    scanf("%lld%lld%lld%lld", &A, &B, &C, &D);
    ans = solve(n);
    printf("%lld\\n", ans);
  }

  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
const ll inf = 4e18;
int main() {
  int t;
  cin >> t;
  while (t--) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    vector<pair<int, ll>> ops = {{2, a}, {3, b}, {5, c}};
    unordered_map<ll, ll> mp;
    mp[0] = 0;
    mp[1] = d;
    function<ll(ll)> dfs = [&](ll x) {
      if (mp.find(x) != mp.end()) {
        return mp[x];
      }
      ll ret = inf;
      if ((long double)d * (long double)x < ret) {
        ret = d * x;
      }
      for (auto &p : ops) {
        int n = p.first;
        ll c = p.second;
        ret = min(ret, dfs(x / n) + c + d * (x % n));
        if (x % n != 0)
          ret = min(ret, dfs(x / n + 1) + c + d * (n - x % n));
      }
      return mp[x] = ret;
    };
    cout << dfs(n) << '\\n';
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
struct node {
  int l, r, u, d;
  // int cl, cr, cu, cd;
  node() {
    l = r = u = d = -1;
    // cl = cr = cu = cd = 0;
  }
};
int main() {
  int n;
  cin >> n;
  vector<int> a(n * n);
  for (int i = 0; i < n * n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<node> b(n * n);
  for (int i = 0; i < n * n; i++) {
    int x = i / n, y = i % n;
    b[i].l = (y == 0 ? -1 : i - 1);
    b[i].r = (y == n - 1 ? -1 : i + 1);
    b[i].u = (x == 0 ? -1 : i - n);
    b[i].d = (x == n - 1 ? -1 : i + n);
    // b[i].cl = y;
    // b[i].cr = n - y - 1;
    // b[i].cu = x;
    // b[i].cd = n - x - 1;
  }
  ll ans = 0;
  for (int i : a) {
    int cl, cr, cu, cd, l = b[i].l, r = b[i].r, u = b[i].u, d = b[i].d;
    cl = cr = cu = cd = 0;
    // b[i].l = b[i].r = b[i].u = b[i].d = -1;
    while (l != -1) {
      cl++;
      l = b[l].l;
    }
    while (r != -1) {
      cr++;
      r = b[r].r;
    }
    while (u != -1) {
      cu++;
      u = b[u].u;
    }
    while (d != -1) {
      cd++;
      d = b[d].d;
    }
    ans += min({cl, cr, cd, cu});
    if (b[i].l != -1) {
      b[b[i].l].r = b[i].r;
    }
    if (b[i].r != -1) {
      b[b[i].r].l = b[i].l;
    }
    if (b[i].u != -1) {
      b[b[i].u].d = b[i].d;
    }
    if (b[i].d != -1) {
      b[b[i].d].u = b[i].u;
    }
  }
  cout << ans << '\\n';
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
// ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)
const ll LINF = 1001002003004005006ll;

ll a, b, c, d;
map<ll, ll> mem;

ll f(ll x) {
  if (!x)
    return 0;
  if (mem.count(x))
    return mem[x];
  ll res = LINF;
  if (LINF / d > x)
    res = x * d;
  auto f2 = [&](ll y) {
    if (y >= x)
      return LINF;
    return f(y);
  };
  {
    ll l = x / 2LL * 2LL, r = l + 2LL;
    res = min(res, f2(l / 2LL) + a + (x - l) * d);
    res = min(res, f2(r / 2LL) + a + (r - x) * d);
  }
  {
    ll l = x / 3LL * 3LL, r = l + 3LL;
    res = min(res, f2(l / 3LL) + b + (x - l) * d);
    res = min(res, f2(r / 3LL) + b + (r - x) * d);
  }
  {
    ll l = x / 5LL * 5LL, r = l + 5LL;
    res = min(res, f2(l / 5LL) + c + (x - l) * d);
    res = min(res, f2(r / 5LL) + c + (r - x) * d);
  }
  return mem[x] = res;
}

void solve() {
  ll n;
  cin >> n;
  cin >> a >> b >> c >> d;
  mem = map<ll, ll>();
  cout << f(n) << endl;
}

int main() {
  int t;
  cin >> t;
  rep(i, t) solve();
  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
const ll LINF = 1001002003004005006ll;

map<ll, ll> m;
ll a, b, c, d;

ll f(ll x) {
  if (!x)
    return 0;
  if (m.count(x))
    return m[x];
  ll res = LINF;
  if (LINF / d > x)
    res = x * d;
  auto f2 = [&](ll y) {
    if (y >= x)
      return LINF;
    return f(y);
  };
  {
    ll l = x / 2 * 2, r = l + 2;
    res = min(res, f2(l / 2) + (x - 1) * d);
    res = min(res, f2(r / 2) + (r - x) * d);
  }
  {
    ll l = x / 3 * 3, r = l + 3;
    res = min(res, f2(l / 3) + b + (x - l) * d);
    res = min(res, f2(r / 3) * b * (r - x) * d);
  }
  {
    ll l = x / 5 * 5, r = l + 5;
    res = min(res, f2(l / 5) + c + (x - l) * d);
    res = min(res, f2(r / 5) * c * (r - x) * d);
  }
  return m[x] = res;
}

int main() {
  int t;
  cin >> t;
  rep(i, t) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    m = map<ll, ll>();
    cout << f(n) << endl;
  }
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

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

long long A, B, C, D;
map<long long, long long> dp;
long long f(long long N) {
  if (dp[N] != 0)
    return dp[N];
  if (N == 0)
    return dp[N] = 0;
  if (N == 1)
    return dp[N] = D;

  long long res = 1e18;
  if (N < res / D)
    res = min(res, N * D);

  int num[] = {2, 3, 5};
  int cost[] = {A, B, C};
  for (int i = 0; i < 3; ++i) {
    if (N % num[i] == 0) {
      res = min(res, f(N / num[i]) + cost[i]);
      continue;
    }

    int d = N % num[i];
    res = min(res, f((N - d) / num[i]) + cost[i] + D * d);
    d = num[i] - d;
    res = min(res, f((N + d) / num[i]) + cost[i] + D * d);
  }

  return dp[N] = res;
}

int main() {
  int T;
  cin >> T;

  for (int i = 0; i < T; ++i) {
    long long N;
    cin >> N >> A >> B >> C >> D;
    dp.clear();
    cout << f(N) << endl;
  }
}


Error: -11

Fixed submission:

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

int A, B, C, D;
long long f(long long N) {
  if (N == 0)
    return 0;

  long long res = 1e18;

  int num[] = {2, 3, 5};
  for (int i = 0; i < 3; ++i) {
    if (N % num[i] == 0) {
      res = min(res, f(N / num[i]));
      continue;
    }

    int d = N % num[i];
    res = min(res, f((N - d) / num[i]));
    d = num[i] - d;
    res = min(res, f((N + d) / num[i]));
  }

  return res;
}

int main() {
  int T;
  cin >> T;

  for (int i = 0; i < T; ++i) {
    long long N;
    cin >> N >> A >> B >> C >> D;
    cout << f(N) << endl;
  }
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

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

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

using ll = long long;
const long long LINF = 1e18;
ll v[3] = {2, 3, 5}, w[3], d, n, t;
map<ll, ll> m;
ll f(ll x) {
  if (!x)
    return LINF;
  if (x == 1)
    return d;
  if (m[x])
    return m[x];
  ll res = LINF;
  if (res / d > x - 10)
    chmin(res, x * d);
  for (int i = 0; i < 3; i++) {
    chmin(res, f((x + v[i] - 1) / v[i]) + w[i] + (v[i] - x % v[i]) * d);
    chmin(res, f(x / v[i]) + w[i] + (x % v[i]) * d);
  }
  return m[x] = res;
}

int main() {
  cin >> t;
  while (t--) {
    cin >> n >> w[0] >> w[1] >> w[2] >> d;
    m.clear();
    cout << f(n) << endl;
  }
}


Error: -11

Fixed submission:

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

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

using ll = long long;
const long long LINF = 1e18;
ll v[3] = {2, 3, 5}, w[3], d, n, t;
map<ll, ll> m;
ll f(ll x) {
  if (!x)
    return LINF;
  if (x == 1)
    return d;
  if (m[x])
    return m[x];
  ll res = LINF;
  if (res / d > x - 10)
    chmin(res, x * d);
  for (int i = 0; i < 3; i++) {
    chmin(res, f((x + v[i] - 1) / v[i] + w[i] + (v[i] - x % v[i]) * d));
    chmin(res, f(x / v[i]) + w[i] + (x % v[i]) * d);
  }
  return m[x] = res;
}

int main() {
  cin >> t;
  while (t--) {
    cin >> n >> w[0] >> w[1] >> w[2] >> d;
    m.clear();
    cout << f(n) << endl;
  }
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
ll N, A, B, C, D;
map<ll, ll> mp;
ll solve(ll x) {
  if (mp.find(x) != mp.end())
    return mp[x];
  if (x == 1)
    return mp[x] = D;
  ll lb = x / 2, rb = (x + 1) / 2;
  mp[x] =
      min(solve(lb) + (x - lb * 2) * D + A, solve(rb) + (rb * 2 - x) * D + A);
  lb = x / 3, rb = (x + 2) / 3;
  mp[x] = min(mp[x], min(solve(lb) + (x - lb * 3) * D + B,
                         solve(rb) + (rb * 3 - x) * D + B));
  lb = x / 5, rb = (x + 4) / 5;
  mp[x] = min(mp[x], min(solve(lb) + (x - lb * 5) * D + C,
                         solve(rb) + (rb * 5 - x) * D + C));
  if (x >= 2e9)
    return mp[x];
  mp[x] = min(solve(x / 2) + (x - x / 2) * D, mp[x]);
  mp[x] = min(solve(x / 3) + (x - x / 3) * D, mp[x]);
  mp[x] = min(solve(x / 5) + (x - x / 5) * D, mp[x]);
  return mp[x];
}
int main() {
  cin >> T;
  while (T--) {
    mp.clear();
    cin >> N >> A >> B >> C >> D;
    mp[0] = 0;
    cout << solve(N) << "\\n";
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
ll N, A, B, C, D;
map<ll, ll> mp;
ll solve(ll x) {
  if (mp.find(x) != mp.end())
    return mp[x];
  ll lb = x / 2, rb = (x + 1) / 2;
  mp[x] =
      min(solve(lb) + (x - lb * 2) * D + A, solve(rb) + (rb * 2 - x) * D + A);
  lb = x / 3, rb = (x + 2) / 3;
  mp[x] = min(mp[x], min(solve(lb) + (x - lb * 3) * D + B,
                         solve(rb) + (rb * 3 - x) * D + B));
  lb = x / 5, rb = (x + 4) / 5;
  mp[x] = min(mp[x], min(solve(lb) + (x - lb * 5) * D + C,
                         solve(rb) + (rb * 5 - x) * D + C));
  if (x >= 2e9)
    return mp[x];
  mp[x] = min(solve(x / 2) + (x - x / 2) * D, mp[x]);
  mp[x] = min(solve(x / 3) + (x - x / 3) * D, mp[x]);
  mp[x] = min(solve(x / 5) + (x - x / 5) * D, mp[x]);
  return mp[x];
}
int main() {
  cin >> T;
  while (T--) {
    mp.clear();
    cin >> N >> A >> B >> C >> D;
    mp[0] = 0;
    solve(N);
    cout << mp[N] << "\\n";
  }
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 1e7 + 10;
const ll INF = 9e18;

int T;
ll N, A, B, C, D;
map<ll, ll> mp;

ll dp(ll x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return D;
  if (mp.count(x))
    return mp[x];
  ll res = INF;
  if (res / D > x)
    res = x * D;

  ll l2 = (x / 2) * 2;
  ll r2 = ((x + 1) / 2) * 2;
  ll l3 = (x / 3) * 3;
  ll r3 = ((x + 2) / 3) * 3;
  ll l5 = (x / 5) * 5;
  ll r5 = ((x + 4) / 5) * 5;

  res = min(res, llabs(l2 - x) * D + A + dp(l2 / 2));
  res = min(res, llabs(r2 - x) * D + A + dp(r2 / 2));

  res = min(res, llabs(l3 - x) * D + B + dp(l3 / 3));
  res = min(res, llabs(r3 - x) * D + B + dp(r3 / 3));

  res = min(res, llabs(l5 - x) * D + C + dp(l5 / 5));
  res = min(res, llabs(r5 - x) * D + C + dp(r5 / 5));

  return mp[x] = res;
}

int main() {
#ifdef ACM_LOCAL
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif
  cin >> T;
  while (T--) {
    mp.clear();
    cin >> N >> A >> B >> C >> D;
    cout << dp(N) << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 1e7 + 10;
const ll INF = 9e18;

int T;
ll N, A, B, C, D;
ll f[MAX], n = 1e7;
map<ll, ll> mp;

ll dp(ll x) {
  if (x <= n)
    return f[x];
  if (mp.count(x))
    return mp[x];
  ll res = INF;
  if (res / D > N)
    res = N * D;

  ll l2 = (N / 2) * 2;
  ll r2 = ((N + 1) / 2) * 2;
  ll l3 = (N / 3) * 3;
  ll r3 = ((N + 2) / 3) * 3;
  ll l5 = (N / 5) * 5;
  ll r5 = ((N + 4) / 5) * 5;

  res = min(res, llabs(l2 - N) * D + A + dp(l2 / 2));
  res = min(res, llabs(r2 - N) * D + A + dp(r2 / 2));

  res = min(res, llabs(l3 - N) * D + B + dp(l3 / 3));
  res = min(res, llabs(r3 - N) * D + B + dp(r3 / 3));

  res = min(res, llabs(l5 - N) * D + C + dp(l5 / 5));
  res = min(res, llabs(r5 - N) * D + C + dp(r5 / 5));

  return mp[x] = res;
}

int main() {
#ifdef ACM_LOCAL
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif
  cin >> T;
  while (T--) {
    mp.clear();
    cin >> N >> A >> B >> C >> D;
    memset(f, 0x3f, sizeof(f));
    f[0] = 0;
    for (int i = 1; i <= n; i++) {
      f[i] = min(f[i], f[i - 1] + D);
      if (i % 2 == 0)
        f[i] = min(f[i], f[i / 2] + A);
      if (i % 3 == 0)
        f[i] = min(f[i], f[i / 3] + B);
      if (i % 5 == 0)
        f[i] = min(f[i], f[i / 5] + C);
    }
    cout << dp(N) << endl;
  }

  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *    author:  tourist
 *    created: 23.05.2020 15:53:29
 **/
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    long long n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    vector<pair<int, long long>> ops;
    ops.emplace_back(2, a);
    ops.emplace_back(3, b);
    ops.emplace_back(5, c);
    const long long inf = (long long)4e18;
    map<long long, long long> mp;
    mp[0] = 0;
    mp[1] = d;
    function<long long(long long)> Dfs = [&](long long x) {
      if (mp.find(x) != mp.end()) {
        return mp[x];
      }
      long long ret = inf;
      if ((long double)d * (long double)x < ret) {
        ret = d * x;
      }
      for (auto &p : ops) {
        int num = p.first;
        long long cost = p.second;
        ret = min(ret, Dfs(x / num) + cost + d * (x % num));
        if (x % num != 0) {
          ret = min(ret, Dfs(x / num + 1) + cost + d * (num - x % num));
        }
      }
      return mp[x] = ret;
    };
    cout << Dfs(n) << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define endl '\\n'
#define ll long long
#define rint register int

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\\n';

using db = long double;
using pll = pair<ll, ll>;
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
    cin >> a[i];

#define rep(i, x, n, inc) for (i = x; i < n; i += inc)
#define repr(i, x, n, inc) for (i = x; i > n; i += inc)
#define all(a) (a).begin(), (a).end()
#define unique_sort(x)                                                         \\
  sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a, b) make_pair(a, b)
#define ff first
#define ss second
#define hell 1000000007
#define infl LLONG_MAX

#define Foxen(i, s) for (i = s.begin(); i != s.end(); i++)
#define Fill(s, v) memset(s, v, sizeof(s))
#define cout_p(x, p)                                                           \\
  cout << fixed << setprecision((p)) << x << endl // print with precision

#define tc(tt)                                                                 \\
  ll tt;                                                                       \\
  cin >> tt;                                                                   \\
  for (ll _tt = 0; _tt < tt; _tt++) // testcase

ll a, b, c, d, n;

ll dfs(ll i) {
  if (i >= n) {
    return max(0LL, (i - n) * d);
  }
  ll q2 = dfs(i * 2) + a;
  ll q3 = dfs(i * 3) + b;
  ll q5 = dfs(i * 5) + c;

  return min(min(q2, max(0LL, (n - i) * d)), min(q3, q5));
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll t, i, x, j, y, z, k;
  tc(tt) {
    cin >> n >> a >> b >> c >> d;
    z = dfs(1) + d;

    ce(z)
  }
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long

const int N = 1e5 + 5;

int k, a, b, c, d;
unordered_map<int, int> cache;

int work(int n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (cache.count(n))
    return cache[n];
  int &ans = cache[n];
  ans = 4e18;
  if (n <= 4e9)
    ans = min(ans, n * d);
  for (int i = -12; i <= 12; i++) {
    int nxtN = n + i;
    if (nxtN < 0)
      continue;
    if (nxtN % 2 == 0 && nxtN / 2 < n)
      ans = min(ans, d * abs(i) + a + work(nxtN / 2));
    if (nxtN % 3 == 0 && nxtN / 3 < n)
      ans = min(ans, d * abs(i) + b + work(nxtN / 3));
    if (nxtN % 5 == 0 && nxtN / 5 < n)
      ans = min(ans, d * abs(i) + c + work(nxtN / 5));
    if (nxtN % 6 == 0 && nxtN / 6 < n)
      ans = min(ans, d * abs(i) + a + b + work(nxtN / 6));
    if (nxtN % 15 == 0 && nxtN / 15 < n)
      ans = min(ans, d * abs(i) + b + c + work(nxtN / 15));
    if (nxtN % 10 == 0 && nxtN / 10 < n)
      ans = min(ans, d * abs(i) + a + c + work(nxtN / 10));
  }
  return ans;
}

int32_t main() {
  IOS;
  int t;
  cin >> t;
  while (t--) {
    cache.clear();
    cin >> k >> a >> b >> c >> d;
    int ans = work(k);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl "\\n"
#define int long long

const int N = 1e5 + 5;

int k, a, b, c, d;
unordered_map<int, int> cache;

int work(int n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (cache.count(n))
    return cache[n];
  int &ans = cache[n];
  ans = 4e18;
  if (n <= 4e9)
    ans = min(ans, n * d);
  for (int i = -15; i <= 15; i++) {
    int nxtN = n + i;
    if (nxtN < 0)
      continue;
    if (nxtN % 2 == 0 && nxtN / 2 < n)
      ans = min(ans, d * abs(i) + a + work(nxtN / 2));
    if (nxtN % 3 == 0 && nxtN / 3 < n)
      ans = min(ans, d * abs(i) + b + work(nxtN / 3));
    if (nxtN % 5 == 0 && nxtN / 5 < n)
      ans = min(ans, d * abs(i) + c + work(nxtN / 5));
    if (nxtN % 6 == 0 && nxtN / 6 < n)
      ans = min(ans, d * abs(i) + a + b + work(nxtN / 6));
    if (nxtN % 15 == 0 && nxtN / 15 < n)
      ans = min(ans, d * abs(i) + b + c + work(nxtN / 15));
    if (nxtN % 10 == 0 && nxtN / 10 < n)
      ans = min(ans, d * abs(i) + a + c + work(nxtN / 10));
  }
  return ans;
}

int32_t main() {
  IOS;
  int t;
  cin >> t;
  while (t--) {
    cache.clear();
    cin >> k >> a >> b >> c >> d;
    int ans = work(k);
    cout << ans << endl;
  }
  return 0;
}