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

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
ll const mod = 1e9 + 7;
const ll MAX = 300000;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;

map<ll, ll> memo;
ll N;
int ct[4];
int dv[3] = {2, 3, 5};
ll rec(ll n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return ct[3];
  if (memo.count(n))
    return memo[n];
  ll res = 1e18;
  if (n < res / ct[3])
    res = n * ct[3]; // if res=n*ct[3],overflow will happen.
  rep(i, 3) {
    if (n % dv[i] == 0) {
      res = min(res, rec(n / dv[i]) + ct[i]);
    } else {
      ll a = (n / dv[i]) * dv[i];
      ll b = a + dv[i];
      res = min(res, rec(a / dv[i]) + ct[i] + (n - a) * ct[3]);
      res = min(res, rec(b / dv[i]) + ct[i] + (b - n) * ct[3]);
    }
  }
  memo[n] = res;
  return res;
}

struct edge {
  int to, cost;
};
int main() {
  int t;
  cin >> t;
  vector<ll> ans;
  rep(i, t) {

    cin >> N >> ct[0] >> ct[1] >> ct[2] >> ct[3];

    ans.push_back(rec(N));
    memo.clear();
  }
  rep(i, t) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
ll const mod = 1e9 + 7;
const ll MAX = 300000;
using vi = vector<int>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;

struct edge {
  int to, cost;
};
int main() {
  int t;
  cin >> t;

  rep(i, t) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    priority_queue<lP, vector<lP>, greater<lP>> q;

    map<ll, int> mp;
    q.push(P(0, n));
    int cnt = 0;
    while (q.size()) {
      lP p = q.top();
      q.pop();
      ll v = p.second;
      if (p.second == 1)
        continue;
      if (mp[v] > 0 && mp[v] < p.first)
        continue;

      if (cnt < 6 && mp[v + 1] == 0 || mp[v + 1] > p.first + d) {
        cnt++;
        mp[v + 1] = p.first + d;
        q.push(lP(p.first + d, v + 1));
      }
      if (mp[v - 1] == 0 || mp[v - 1] > p.first + d) {
        mp[v - 1] = p.first + d;
        q.push(lP(p.first + d, v - 1));
      }
      if (v % 2 == 0) {
        if (mp[v / 2] == 0 || mp[v / 2] > p.first + a) {
          mp[v / 2] = p.first + a;
          q.push(lP(p.first + a, v / 2));
        }
      }
      if (v % 3 == 0) {
        if (mp[v / 3] == 0 || mp[v / 3] > p.first + b) {
          mp[v / 3] = p.first + b;
          q.push(lP(p.first + b, v / 3));
        }
      }
      if (v % 5 == 0) {
        if (mp[v / 5] == 0 || mp[v / 5] > p.first + c) {
          mp[v / 5] = p.first + c;
          q.push(lP(p.first + c, v / 5));
        }
      }
    }
    cout << mp[1] + d << endl;
  }

  return 0;
}