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

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  Init();
  ll t;
  cin >> t;
  while (t--) {
    ll n, a, b, c, d, ans = 1e18;
    ;
    cin >> n >> a >> b >> c >> d;

    // m倍する時のコスト
    vi cost(6);
    cost[2] = a, cost[3] = b, cost[5] = c;

    // que<v, num>; numの時のコスト
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        que;
    // 最小値（メモ）
    map<ll, ll> mp;

    // ansより大きかったら狩る
    // 既に通っている場合は、その時のコストより小さくないと狩る
    // 上記を除いてqueに加える
    auto insert = [&](ll v, ll n) {
      if (v < ans && (!mp.count(n) || v < mp[n])) {
        que.emplace(v, n);
        mp[n] = v;
      }
    };

    // 初期状態
    que.emplace(0, n);
    while (que.size()) {
      auto p = que.top();
      que.pop();
      ll v = p.first, num = p.second;
      // 優先度付き（昇順）なので、ansを超えた時点でbreak
      if (v >= ans)
        break;
      // ある地点から全て-1（コストd）で行けるなら試す
      if (num <= 2e18 / d)
        chmin(ans, v + num * d);
      // 2, 3, 5倍を試す
      for (auto &mul : {2, 3, 5}) {
        ll r = num % mul;
        ll vt = v + r * d + cost[mul];
        insert(vt, num / mul);
        vt = v + (mul - r) * d + cost[mul];
        insert(vt, num / mul + 1);
      }
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MODV 1000000007
#define INF INT_MAX     // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);                                                             \\
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

ull n, a, b, c, d;
ull minCoin = ULLONG_MAX;

void solve(ull num, ull cost) {
  if (num >= n) {
    cost += (num - n) * d;
    chmin(minCoin, cost);
    return;
  }
  if (cost + a < minCoin)
    solve(num * 2, cost + min(a, num * d));
  if (num * 2 > n) {
    cost += (n - num) * d;
    chmin(minCoin, cost);
  }

  if (cost + b < minCoin)
    solve(num * 3, cost + min(b, 2 * num * d));
  if (num * 3 > n) {
    cost += (n - num) * d;
    chmin(minCoin, cost);
  }

  if (cost + c < minCoin)
    solve(num * 5, cost + min(c, 4 * num * d));
  if (num * 5 > n) {
    cost += (n - num) * d;
    chmin(minCoin, cost);
  }

  if (cost + d < minCoin)
    solve(num + 1, cost + d);
}

int main() {
  Init();
  ll t;
  cin >> t;
  rep(i, t) {
    cin >> n >> a >> b >> c >> d;
    minCoin = ULLONG_MAX;
    solve(1, d);
    cout << minCoin << endl;
  }
}