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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\\n"

ll D;
ll cost[6];
map<ll, ll> dp;
ll go(ll n);

ll call(ll n, ll i) {
  ll l = n - (n % i);
  ll r = l + i;
  if (l == n)
    r = n;
  return min(go(l / i) + cost[i] + (n - l) * D,
             go(r / i) + cost[i] + (r - n) * D);
}

const ll INF = 1e18;

ll go(ll n) {
  if (dp.find(n) != dp.end())
    return dp[n];
  ll ans = INF;
  if (ans / D > n)
    ans = D * n;
  ans = min(ans, call(n, 2));
  ans = min(ans, call(n, 3));
  ans = min(ans, call(n, 5));
  dp[n] = ans;
  return dp[n];
}

void solveTest() {
  ll n;
  cin >> n >> cost[2] >> cost[3] >> cost[5] >> D;
  dp.clear();
  dp[0] = 0;
  dp[1] = D;
  cout << go(n) << "\\n";
}

int main() {
  int t;
  cin >> t;
  while (t--)
    solveTest();
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\\n"

ll D;
ll cost[6];
map<ll, ll> dp;
ll go(ll n);

ll call(ll n, ll i) {
  ll l = n - (n % i);
  ll r = l + i;
  if (l == n)
    r = n;
  return min(go(l / i) + cost[i] + (n - l) * D,
             go(r / i) + cost[i] + (r - n) * D);
}

const ll INF = 1e18;

ll go(ll n) {
  if (dp.find(n) != dp.end())
    return dp[n];
  ll ans = INF;
  if (ans / D > n)
    ans = D * n;
  return dp[n] = min(ans, min(call(n, 2), min(call(n, 3), call(n, 5))));
}

void solveTest() {
  ll n;
  cin >> n >> cost[2] >> cost[3] >> cost[5] >> D;
  dp.clear();
  dp[0] = 0;
  cout << go(n) << "\\n";
}

int main() {
  int t;
  cin >> t;
  while (t--)
    solveTest();
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

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

ll solve(ll num);

ll t, n, a, b, c, d;
map<ll, ll> memo;
ll ans;

int main() {

  cin >> t;

  for (int i = 0; i < t; ++i) {
    //  条件ごとにmapをクリア
    memo.clear();
    cin >> n >> a >> b >> c >> d;

    //  再帰関数を呼び出し
    ans = solve(n);
    cout << ans << endl;
  }

  return 0;
}

ll solve(ll num) {

  ll cost, l2, r2, l3, r3, l5, r5;

  // 再帰を抜ける処理
  if (num == 0)
    return 0;
  if (num == 1)
    return d;
  if (memo.count(num))
    return memo.at(num);

  // 2, 3, 5で割り切れる数値に移動して商を計算
  // numが2, 3, 5で割り切れる値の場合は、numをそのままl(num), r(num)とする
  l2 = (num / 2) * 2;
  r2 = ((num + 1) / 2) * 2;
  l3 = (num / 3) * 3;
  r3 = ((num + 2) / 3) * 3;
  l5 = (num / 5) * 5;
  r5 = ((num + 4) / 5) * 5;

  //  numまで操作dを実施した場合のコストで初期化
  //  costをn+dとするとオーバーフローするが、下だとオーバーフローしない
  //  なんでif処理が必要なのか理解できない・・・
  cost = 1e18;
  if (num < cost / d)
    cost = num * d;

  // コストを計算
  // 割った回数だけa, b, cをコストに加算、割り切れない分はdとして加算
  cost = min(cost, (llabs(num - l2)) * d + a + solve(l2 / 2));
  cost = min(cost, (llabs(num - r2)) * d + a + solve(r2 / 2));
  cost = min(cost, (llabs(num - l3)) * d + b + solve(l3 / 3));
  cost = min(cost, (llabs(num - r3)) * d + b + solve(r3 / 3));
  cost = min(cost, (llabs(num - l5)) * d + c + solve(l5 / 5));
  cost = min(cost, (llabs(num - r5)) * d + c + solve(r5 / 5));

  // 計算したコストをmapに記憶
  memo[num] = cost;

  return cost;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  map::at


Fixed submission:

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

ll solve(ll num);

ll t, n, a, b, c, d;
map<ll, ll> memo;
ll ans;

int main() {

  cin >> t;

  for (int i = 0; i < t; ++i) {
    //  条件ごとにmapをクリア
    memo.clear();
    cin >> n >> a >> b >> c >> d;

    //  再帰関数を呼び出し
    ans = solve(n);
    cout << ans << endl;
  }

  return 0;
}

ll solve(ll num) {

  ll cost, l2, r2, l3, r3, l5, r5;

  // 再帰を抜ける処理
  if (num == 0)
    return 0;
  if (num == 1)
    return d;
  if (memo.count(num))
    return memo.at(num);

  // 2, 3, 5で割り切れる数値に移動して商を計算
  // numが2, 3, 5で割り切れる値の場合は、numをそのままl(num), r(num)とする
  l2 = (num / 2) * 2;
  r2 = ((num + 1) / 2) * 2;
  l2 = (num / 3) * 3;
  r2 = ((num + 2) / 3) * 3;
  l2 = (num / 5) * 5;
  r2 = ((num + 4) / 5) * 5;

  // numまで操作dを実施した場合のコストで初期化
  cost = num * d;

  // コストを計算
  // 割った回数だけa, b, cをコストに加算、割り切れない分はdとして加算
  cost = min(cost, llabs(num - l2) * d + a + solve(num / 2));
  cost = min(cost, llabs(num - r2) * d + a + solve(num / 2));
  cost = min(cost, llabs(num - l3) * d + b + solve(num / 3));
  cost = min(cost, llabs(num - r3) * d + b + solve(num / 3));
  cost = min(cost, llabs(num - l5) * d + c + solve(num / 5));
  cost = min(cost, llabs(num - r5) * d + c + solve(num / 5));

  // 計算したコストをmapに記憶
  memo.at(num) = cost;

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

#include <bits/stdc++.h>
#define ll long long
#define INF 1e11
using namespace std;
unordered_map<ll, ll> s;
set<ll> p;
ll n, A, B, C, D;
int t;
ll tr(ll x, ll y) {
  if (INF / (x - y) >= D)
    return D * (x - y);
  return INF;
}
void get(ll x, ll y) {
  if (s[x] == 0) {
    s[x] = y;
    p.insert(x);
  } else
    s[x] = min(s[x], y);
}
int main() {
  cin >> t;
  while (t--) {
    s.clear();
    p.clear();
    cin >> n >> A >> B >> C >> D;
    p.insert(n);
    if (n == 1) {
      cout << D << endl;
      continue;
    }
    s[0] = min((A + D) * 70, min((B + D) * 48, (C + D + D) * 30));
    while (!p.empty()) {
      auto g = p.end();
      g--;
      ll x = (*g);
      p.erase(g);
      ll y = s[x];
      if (y > s[0])
        continue;
      get(x / 2, y + min(A + D * (x - (x / 2) * 2), tr(x, x / 2)));
      if (x % 2 != 0)
        get(x / 2 + 1, y + min(A + D, tr(x, x / 2 + 1)));
      if (x % 3 <= 1)
        get(x / 3, y + min(B + D * (x - (x / 3) * 3), tr(x, x / 3)));
      else
        get(x / 3 + 1,
            y + min(B + D * ((x / 3 + 1) * 3 - x), tr(x, x / 3 + 1)));
      if (x % 5 < 3)
        get(x / 5, y + min(C + D * (x - (x / 5) * 5), tr(x, x / 5)));
      else
        get(x / 5 + 1,
            y + min(C + D * ((x / 5 + 1) * 5 - x), tr(x, x / 5 + 1)));
    }
    cout << s[0] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1e11
using namespace std;
unordered_map<ll, ll> s;
set<ll> p;
ll n, A, B, C, D;
int t;
ll tr(ll x, ll y) {
  if (INF / (x - y) >= D)
    return D * (x - y);
  return INF;
}
void get(ll x, ll y) {
  if (s[x] == 0) {
    s[x] = y;
    p.insert(x);
  } else
    s[x] = min(s[x], y);
}
int main() {
  cin >> t;
  while (t--) {
    s.clear();
    p.clear();
    cin >> n >> A >> B >> C >> D;
    p.insert(n);
    s[0] = min((A + D) * 70, min((B + D) * 48, (C + D + D) * 30));
    while (!p.empty()) {
      auto g = p.end();
      g--;
      ll x = (*g);
      p.erase(g);
      ll y = s[x];
      if (y > s[0])
        continue;
      get(x / 2, y + min(A + D * (x - (x / 2) * 2), tr(x, x / 2)));
      if (x % 2 != 0)
        get(x / 2 + 1, y + min(A + D, tr(x, x / 2 + 1)));
      if (x % 3 <= 1)
        get(x / 3, y + min(B + D * (x - (x / 3) * 3), tr(x, x / 3)));
      else
        get(x / 3 + 1,
            y + min(B + D * ((x / 3 + 1) * 3 - x), tr(x, x / 3 + 1)));
      if (x % 5 < 3)
        get(x / 5, y + min(C + D * (x - (x / 5) * 5), tr(x, x / 5)));
      else
        get(x / 5 + 1,
            y + min(C + D * ((x / 5 + 1) * 5 - x), tr(x, x / 5 + 1)));
    }
    cout << s[0] << endl;
  }
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
unordered_map<long long, long long> M;

int A, B, C, D;
long long dfs(long long N) {
  if (N == 0)
    return 0;
  if (M.count(N))
    return M[N];
  long long ret = 1e18;
  ret = min((__int128)ret, (__int128)N * D);
  ret = min(ret, dfs(N / 5) + (N % 5) * D + C);
  if (N % 5 >= 3 && N / 5 + 1 < N)
    ret = min(ret, dfs(N / 5 + 1) + (5 - N % 5) * D + C);
  ret = min(ret, dfs(N / 3) + (N % 3) * D + B);
  if (N % 3 >= 2 && N / 3 + 1 < N)
    ret = min(ret, dfs(N / 3 + 1) + (3 - N % 3) * D + B);
  ret = min(ret, dfs(N / 2) + (N % 2) * D + A);
  if (N / 2 + 1 < N)
    ret = min(ret, dfs(N / 2 + 1) + (2 - N % 2) * D + A);
  M[N] = ret;
  return ret;
}

int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    long long N;
    scanf("%lld%d%d%d%d", &N, &A, &B, &C, &D);
    M.clear();
    printf("%lld\\n", dfs(N));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

typedef long long ll;

ll T, n, a, b, c, d;
ll ans;

void bfs() {
  std::queue<std::pair<ll, ll>> q;
  q.push(std::make_pair(0, 0));

  std::pair<ll, ll> t;
  ll now, cost;

  while (!q.empty()) {
    t = q.front();
    q.pop();
    now = t.first, cost = t.second;
    if (cost >= ans)
      continue;

    if (now == n) {
      ans = cost;
    } else if (now == 0) {
      q.push(std::make_pair(now + 1, cost + d));
    } else if (now > n) {
      q.push(std::make_pair(now - 1, cost + d));
    } else {
      q.push(std::make_pair(now * 2, cost + a));
      q.push(std::make_pair(now * 3, cost + b));
      q.push(std::make_pair(now * 5, cost + c));
      q.push(std::make_pair(now + 1, cost + d));
      q.push(std::make_pair(now - 1, cost + d));
    }
  }
}

int main() {

  std::ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> T;

  while (T--) {
    ans = 0x7f7f7f7f7f7f7f7f;
    cin >> n >> a >> b >> c >> d;
    bfs();
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#define int long long
using namespace std;
map<int, int> memo;
int a, b, c, d;
int dfs(int n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (memo.count(n))
    return memo[n];
  int l2 = (n / 2) * 2;
  int r2 = ((n + 1) / 2) * 2;
  int l3 = (n / 3) * 3;
  int r3 = ((n + 2) / 3) * 3;
  int l5 = (n / 5) * 5;
  ;
  int r5 = ((n + 4) / 5) * 5;
  int res = 1e18;
  if (n < res / d)
    res = n * d;
  res = min(res, llabs(l2 - n) * d + a + dfs(l2 / 2));
  res = min(res, llabs(r2 - n) * d + a + dfs(r2 / 2));
  res = min(res, llabs(l3 - n) * d + b + dfs(l3 / 3));
  res = min(res, llabs(r3 - n) * d + b + dfs(r3 / 3));
  res = min(res, llabs(l5 - n) * d + c + dfs(l5 / 5));
  res = min(res, llabs(r5 - n) * d + c + dfs(r5 / 5));
  memo[n] = res;
  return res;
}
signed main() {
  int t;
  cin >> t;
  while (t--) {
    memo.clear();
    int n;
    cin >> n >> a >> b >> c >> d;
    cout << dfs(n) << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#define int long long
using namespace std;
map<int, int> m;
int t, n, a, b, c, d;
int dfs(int x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return d;
  if (m.count(x))
    return m[x];
  int l2 = (n / 2) * 2, r2 = ((n + 1) / 2) * 2, l3 = (n / 3) * 3,
      r3 = ((n + 2) / 3) * 3, l5 = (n / 5) * 5, r5 = ((n + 4) / 5) * 5,
      ans = 1e18;
  if (x < ans / d)
    ans = x * d;
  ans = min(ans, 1ll * abs(l2 - x) * d + a + dfs(l2 / 2));
  ans = min(ans, 1ll * abs(r2 - x) * d + a + dfs(r2 / 2));
  ans = min(ans, 1ll * abs(l3 - x) * d + b + dfs(l3 / 3));
  ans = min(ans, 1ll * abs(r3 - x) * d + b + dfs(r3 / 3));
  ans = min(ans, 1ll * abs(l5 - x) * d + c + dfs(l5 / 5));
  ans = min(ans, 1ll * abs(r5 - x) * d + c + dfs(r5 / 5));
  m[x] = ans;
  return ans;
}
signed main(void) {
  cin >> t;
  for (int k = 0; k < t; k++) {
    m.clear();
    cin >> n >> a >> b >> c >> d;
    cout << dfs(n) << endl;
  }
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

/**
 *   author:  cryforthemoon46
 *   created: 23.05.2020 21:01:17
 **/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)

const ll INF = (ll)1e18;

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

ll solve(ll x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return D;
  if (memo.count(x))
    return memo[x];
  memo[x] = min({
      (x < INF / D) ? x * D : INF,
      llabs((x / 2) * 2 - x) * D + A + solve(x / 2),
      llabs(((x + 1) / 2) * 2 - x) * D + A + solve((x + 1) / 2),
      llabs((x / 3) * 3 - x) * D + B + solve(x / 3),
      llabs(((x + 2) / 3) * 3 - x) * D + B + solve((x + 2) / 3),
      llabs((x / 5) * 5 - x) * D + C + solve(x / 5),
      llabs(((x + 4) / 5) * 5 - x) * D + C + solve((x + 4) / 5),
  });
  return memo[x];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> T;
  REP(i, T) {
    memo.clear();
    cin >> N >> A >> B >> C >> D;
    cout << solve(N) << endl;
  }
}

Error: -11

Fixed submission:

/**
 *   author:  cryforthemoon46
 *   created: 23.05.2020 21:01:17
 **/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FOREACH(x, a) for (auto &&x : (a))

const double EPS = (double)(1e-7);
const ll INF = (ll)1e18;
const int MOD = (int)1e9 + 7;
const ll dx[4] = {0, 1, 0, -1};
const ll dy[4] = {-1, 0, 1, 0};
const ll MAX_N = (ll)1e18;

ll T, N, A, B, C, D;

ll solve() {
  ll dp[N + 2];
  dp[0] = 0;
  FOR(i, 1, N + 2) {
    dp[i] = INF;
    if (i % 2 == 0) {
      dp[i] = min(dp[i], dp[i / 2] + A);
    }
    if (i % 3 == 0) {
      dp[i] = min(dp[i], dp[i / 3] + B);
    }
    if (i % 5 == 0) {
      dp[i] = min(dp[i], dp[i / 5] + C);
    }
    dp[i] = min(dp[i], dp[i - 1] + D);
    dp[i - 1] = min(dp[i - 1], dp[i] + D);
  }
  return dp[N];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> T;
  REP(i, T) {
    cin >> N >> A >> B >> C >> D;
    cout << solve() << endl;
  }
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

map<long long, long long> dp;
long long a, b, c, d;

long long rec(long long n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  auto I = dp.find(n);
  if (I != dp.end())
    return I->second;
  long long best = LLONG_MAX;
  if (n % 2 == 0) {
    best = min(best, a + rec(n / 2));
  }
  if (n % 3 == 0) {
    best = min(best, b + rec(n / 3));
  }
  if (n % 5 == 0) {
    best = min(best, c + rec(n / 5));
  }
  if (LLONG_MAX / d > n) {
    best = min(best, d * n);
  }
  for (int i = -5; i <= 5; i++) {
    long long m = n + i;
    if (m < 0)
      continue;
    if (m % 2 == 0 && m / 2 < n) {
      best = min(best, abs(i) * d + a + rec(m / 2));
    }
    if (m % 3 == 0 && m / 3 < n) {
      best = min(best, abs(i) * d + b + rec(m / 3));
    }
    if (m % 5 == 0 && m / 5 < n) {
      best = min(best, abs(i) * d + c + rec(m / 5));
    }
  }
  dp[n] = best;
  return best;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T-- > 0) {
    long long n;
    cin >> n >> a >> b >> c >> d;
    dp.clear();
    long long ans = rec(n);
    cout << ans << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

map<long long, long long> dp;
long long a, b, c, d;

long long rec(long long n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  auto I = dp.find(n);
  if (I != dp.end())
    return I->second;
  long long best = LLONG_MAX;
  if (n % 2 == 0) {
    best = min(best, a + rec(n / 2));
  }
  if (n % 3 == 0) {
    best = min(best, b + rec(n / 3));
  }
  if (n % 5 == 0) {
    best = min(best, c + rec(n / 5));
  }
  if (LLONG_MAX / d > n) {
    best = min(best, d * n);
  }
  for (int i = -10; i <= 10; i++) {
    long long m = n + i;
    if (m < 0)
      continue;
    if (m % 2 == 0 && m / 2 < n) {
      best = min(best, abs(i) * d + a + rec(m / 2));
    }
    if (m % 3 == 0 && m / 3 < n) {
      best = min(best, abs(i) * d + b + rec(m / 3));
    }
    if (m % 5 == 0 && m / 5 < n) {
      best = min(best, abs(i) * d + c + rec(m / 5));
    }
  }
  dp[n] = best;
  return best;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T-- > 0) {
    long long n;
    cin >> n >> a >> b >> c >> d;
    dp.clear();
    long long ans = rec(n);
    cout << ans << "\\n";
  }

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

#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

std::map<ll, ll> dp;
std::set<ll> pq;

ll const inf = 1000000000000000000;

void update(ll pos, ll val) {
  pq.insert(pos);
  if (dp.find(pos) == dp.end())
    dp[pos] = val;
  else
    dp[pos] = std::min(dp[pos], val);
}

void solve() {
  dp.clear();
  pq.clear();

  ll n, a, b, c, d;
  std::cin >> n >> a >> b >> c >> d;
  ll result = inf;
  dp[n] = 0;
  pq.insert(n);

  while (0 < pq.size()) {
    std::set<ll>::iterator it = pq.end();
    it--;
    ll node = *it;
    pq.erase(it);
    if (d <= inf / node)
      result = std::min(result, dp[node] + d * node);

    if (node < 2)
      break;

    int rest = node % 2;

    update((node - rest) / 2, dp[node] + rest * d + a);
    update((node + (2 - rest) % 2) / 2, dp[node] + (2 - rest) % 2 * d + a);

    rest = node % 3;
    update((node - rest) / 3, dp[node] + rest * d + b);
    update((node + (3 - rest) % 3) / 3, dp[node] + (3 - rest) % 3 * d + b);

    rest = node % 5;
    update((node - rest) / 5, dp[node] + rest * d + c);
    update((node + (5 - rest) % 5) / 5, dp[node] + (5 - rest) % 5 * d + c);
  }
  std::cout << result << '\\n';
}

int main() {
  int t;
  std::cin >> t;
  for (int testcase = 1; testcase <= t; testcase++)
    solve();
}


Error: 0

Fixed submission:

#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

std::map<ll, ll> dp;
std::set<ll> pq;

ll const inf = 1000000000000000000;

void update(ll pos, ll val) {
  pq.insert(pos);
  if (dp.find(pos) == dp.end())
    dp[pos] = val;
  else
    dp[pos] = std::min(dp[pos], val);
}

ll solve() {
  dp.clear();
  pq.clear();

  ll n, a, b, c, d;
  std::cin >> n >> a >> b >> c >> d;
  ll result = inf;
  dp[n] = 0;
  pq.insert(n);

  while (0 < pq.size()) {
    std::set<ll>::iterator it = pq.end();
    it--;
    pq.erase(it);
    ll node = *it;
    if (d <= inf / node)
      result = std::min(result, dp[node] + d * node);

    if (node < 2)
      break;

    int rest = node % 2;

    update((node - rest) / 2, dp[node] + rest * d + a);
    update((node + (2 - rest) % 2) / 2, dp[node] + (2 - rest) % 2 * d + a);

    rest = node % 3;
    update((node - rest) / 3, dp[node] + rest * d + b);
    update((node + (3 - rest) % 3) / 3, dp[node] + (3 - rest) % 3 * d + b);

    rest = node % 5;
    update((node - rest) / 5, dp[node] + rest * d + c);
    update((node + (5 - rest) % 5) / 5, dp[node] + (5 - rest) % 5 * d + c);
  }
  std::cout << result << '\\n';
}

int main() {
  int t;
  std::cin >> t;
  for (int testcase = 1; testcase <= t; testcase++)
    solve();
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <map>
#define Z int
#define V void
#define L long long
#define USE using namespace std
#define S(n) scanf("%lld", &n)
#define P(n) printf("%lld\\n", n)
#define W while
#define sm(a, b) ((a) < (b) ? (a) : (b))
#define I if
#define R return
USE;
map<L, L> rem;
L t, n, a, b, c, d, lim = (L)(1e18), ans;
L dfs(L x) {
  I(rem[x] || !x) R rem[x];
  rem[x] = lim;
  L y;
  y = x / 2 * 2, rem[x] = sm(rem[x], dfs(y / 2) + (x - y) * d + a);
  y = (x / 2 + 1) * 2, rem[x] = sm(rem[x], dfs(y / 2) + (y - x) * d + a);
  y = x / 3 * 3, rem[x] = sm(rem[x], dfs(y / 3) + (x - y) * d + b);
  y = (x / 3 + 1) * 3, rem[x] = sm(rem[x], dfs(y / 3) + (y - x) * d + b);
  y = x / 5 * 5, rem[x] = sm(rem[x], dfs(y / 5) + (x - y) * d + c);
  y = (x / 5 + 1) * 5, rem[x] = sm(rem[x], dfs(y / 5) + (y - x) * d + c);
  I(x <= lim / d) rem[x] = sm(rem[x], x * d);
  R rem[x];
}
V doit() { S(n), S(a), S(b), S(c), S(d), rem.clear(), P(dfs(n)); }
Z main() {
  S(t);
  W(t--) doit();
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <map>
#define Z int
#define V void
#define L long long
#define USE using namespace std
#define S(n) scanf("%lld", &n)
#define P(n) printf("%lld\\n", n)
#define W while
#define I if
#define R return
USE;
map<L, L> rem;
L t, n, a, b, c, d, lim = (L)(1e18), ans;
L cc(L x, L y) { R(x == 0 || x > y); }
V dfs(L x) {
  I(!n) R;
  L y;
  y = x / 2 * 2;
  I(y / 2 != x) I(cc(rem[y / 2], rem[x] + (x - y) * d + a)) {
    rem[y / 2] = rem[x] + (x - y) * d + a;
    I(cc(rem[y], rem[x] + (x - y) * d)) rem[y] = rem[x] + (x - y) * d;
    dfs(y / 2);
  }
  y = (x / 2 + 1) * 2;
  I(y / 2 != x) I(cc(rem[y / 2], rem[x] + (y - x) * d + a)) {
    rem[y / 2] = rem[x] + (y - x) * d + a;
    I(cc(rem[y], rem[x] + (y - x) * d)) rem[y] = rem[x] + (y - x) * d;
    dfs(y / 2);
  }
  y = x / 3 * 3;
  I(y / 3 != x) I(cc(rem[y / 3], rem[x] + (x - y) * d + b)) {
    rem[y / 3] = rem[x] + (x - y) * d + b;
    I(cc(rem[y], rem[x] + (x - y) * d)) rem[y] = rem[x] + (x - y) * d;
    dfs(y / 3);
  }
  y = (x / 3 + 1) * 3;
  I(y / 3 != x) I(cc(rem[y / 3], rem[x] + (y - x) * d + b)) {
    rem[y / 3] = rem[x] + (y - x) * d + b;
    I(cc(rem[y], rem[x] + (y - x) * d)) rem[y] = rem[x] + (y - x) * d;
    dfs(y / 3);
  }
  y = x / 5 * 5;
  I(y / 5 != x) I(cc(rem[y / 5], rem[x] + (x - y) * d + c)) {
    rem[y / 5] = rem[x] + (x - y) * d + c;
    I(cc(rem[y], rem[x] + (x - y) * d)) rem[y] = rem[x] + (x - y) * d;
    dfs(y / 5);
  }
  y = (x / 5 + 1) * 5;
  I(y / 5 != x) I(cc(rem[y / 5], rem[x] + (y - x) * d + c)) {
    rem[y / 5] = rem[x] + (y - x) * d + c;
    I(cc(rem[y], rem[x] + (y - x) * d)) rem[y] = rem[x] + (y - x) * d;
    dfs(y / 5);
  }
  I(x <= lim / d) I(cc(rem[0], rem[x] + x * d)) rem[0] = rem[x] + x * d;
}
V doit() {
  S(n), S(a), S(b), S(c), S(d);
  rem.clear();
  rem[n] = 1;
  dfs(n);
  P(rem[0] - 1);
}
Z main() {
  S(t);
  W(t--) doit();
}