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

#include <bits/stdc++.h>
#define Int uint64_t

using namespace std;

void solve() {
  Int N, A, B, C, D;
  cin >> N >> A >> B >> C >> D;

  Int ans = 1e18;
  using P = pair<Int, Int>;
  priority_queue<P, vector<P>, greater<P>> que;
  que.emplace(0, N);
  map<Int, Int> mp;
  auto insert = [&](Int n, Int cost) {
    if (!mp.count(n) || mp[n] > cost) {
      que.emplace(cost, n);
    }
  };

  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    Int cost = p.first, n = p.second;
    if (mp.count(n) && mp[n] <= cost) {
      continue;
    }
    mp[n] = cost;
    if (n == 0) {
      ans = min(ans, cost);
      continue;
    }

    {
      if (n % 2 == 0) {
        Int add = min(A, n / 2 * D);
        insert(n / 2, cost + add);
      } else {
        Int add = min(A, n / 2 * D) + D;
        insert(n / 2, cost + add);
        add = min(A, (n + 1) / 2 * D) + D;
        insert((n + 1) / 2, cost + add);
      }
    }
    {
      if (n % 3 == 0) {
        Int add = min(B, n / 3 * 2 * D);
        insert(n / 3, cost + add);
      } else {
        Int m = n % 3;
        Int add = min(B, n / 3 * 2 * D) + m * D;
        insert(n / 3, cost + add);
        add = min(B, (n + 2) / 3 * 2 * D) + (3 - m) * D;
        insert((n + 2) / 3, cost + add);
      }
    }
    {
      if (n % 5 == 0) {
        Int add = min(C, n / 5 * 4 * D);
        insert(n / 5, cost + add);
      } else {
        Int m = n % 5;
        Int add = min(C, n / 5 * 4 * D) + m * D;
        insert(n / 5, cost + add);
        add = min(C, (n + 4) / 5 * 4 * D) + (5 - m) * D;
        insert((n + 4) / 5, cost + add);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int T;
  cin >> T;
  while (T--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Int uint64_t

using namespace std;

void solve() {
  Int N, A, B, C, D;
  cin >> N >> A >> B >> C >> D;

  Int ans = 1e18;
  deque<pair<Int, Int>> que;
  que.emplace_back(N, 0);
  unordered_map<Int, Int> mp;
  auto insert = [&](Int n, Int cost) {
    if (!mp.count(n) || mp[n] > cost) {
      que.emplace_back(n, cost);
    }
  };

  while (!que.empty()) {
    auto p = que.front();
    que.pop_front();
    Int n = p.first, cost = p.second;
    if (mp.count(n) && mp[n] <= cost) {
      continue;
    }
    mp[n] = cost;
    if (n == 0) {
      ans = min(ans, cost);
      continue;
    }

    {
      if (n % 2 == 0) {
        Int add = min(A, n / 2 * D);
        insert(n / 2, cost + add);
      } else {
        Int add = min(A, n / 2 * D) + D;
        insert(n / 2, cost + add);
        add = min(A, (n + 1) / 2 * D) + D;
        insert((n + 1) / 2, cost + add);
      }
    }
    {
      if (n % 3 == 0) {
        Int add = min(B, n / 3 * 2 * D);
        insert(n / 3, cost + add);
      } else {
        Int m = n % 3;
        Int add = min(B, n / 3 * 2 * D) + m * D;
        insert(n / 3, cost + add);
        add = min(B, (n + 2) / 3 * 2 * D) + (3 - m) * D;
        insert((n + 2) / 3, cost + add);
      }
    }
    {
      if (n % 5 == 0) {
        Int add = min(C, n / 5 * 4 * D);
        insert(n / 5, cost + add);
      } else {
        Int m = n % 5;
        Int add = min(C, n / 5 * 4 * D) + m * D;
        insert(n / 5, cost + add);
        add = min(C, (n + 4) / 5 * 4 * D) + (5 - m) * D;
        insert((n + 4) / 5, cost + add);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int T;
  cin >> T;
  while (T--) {
    solve();
  }

  return 0;
}