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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<LL, LL>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL t, n, d, nn[3], c[3];
map<LL, LL> mp;
multiset<pii> q;
/*
LL dfs(LL x)
{
        if(x==0) return 0;
        if(x==1) return d;
        if(mp[x]>0) return mp[x];
        //if(vis[x]) return 1e18;
        vis[x]=1;
        //cout<<x<<' '<<mp.size()<<endl;
        LL ret=5e18;
        rep(ii,3)
        {
                LL i=nn[ii],tmp=x-x%i;
                if(!vis[tmp/i])
ret=min(ret,dfs(tmp/i)+min(c[ii]+x%i*d,abs(tmp/i-x)*d)); if(x%i>0)
                {
                        tmp=x-x%i+i;
                        if(!vis[tmp/i])
ret=min(ret,dfs(tmp/i)+min(c[ii]+(i-x%i)*d,abs(tmp/i-x)*d));
                }
        }
        //cout<<x<<' '<<ret<<endl;
        //if(ret>=5e18) cout<<x<<endl;
        vis[x]=0;
        return mp[x]=ret;
}
*/
int main() {
  nn[0] = 2;
  nn[1] = 3;
  nn[2] = 5;
  cin >> t;
  while (t--) {
    cin >> n >> c[0] >> c[1] >> c[2] >> d;
    mp.clear();
    q.clear();
    mp[n] = 0;
    q.insert(mpr(0, n));
    while (!q.empty()) {
      LL x = q.begin()->se, y = q.begin()->fi;
      // cout<<x<<' '<<y<<endl;
      q.erase(q.begin());
      rep(ii, 3) {
        LL i = nn[ii], tmp = x - x % i,
           tmp2 = y + min(c[ii] + x % i * d, abs(tmp / i - x) < (LL)2e18 / d
                                                 ? abs(tmp / i - x) * d
                                                 : (LL)2e18);
        if (tmp / i != n && (mp[tmp / i] == 0 || mp[tmp / i] > tmp2)) {
          mp[tmp / i] = tmp2;
          q.insert(mpr(tmp2, tmp / i));
        }
        if (x % i > 0) {
          tmp = x - x % i + i;
          tmp2 = y + min(c[ii] + (i - x % i) * d,
                         abs(tmp / i - x) < (LL)2e18 / d ? abs(tmp / i - x) * d
                                                         : (LL)2e18);
          if (tmp / i != n && (mp[tmp / i] == 0 || mp[tmp / i] > tmp2)) {
            mp[tmp / i] = tmp2;
            q.insert(mpr(tmp2, tmp / i));
          }
        }
      }
      if (x == 0)
        break;
    }
    // cout<<(int)(mp.find(0)==mp.end())<<endl;
    cout << mp[0] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD = 1e9 + 7;

LL t, n, d, nn[3], c[3];
map<LL, LL> mp;

LL dfs(LL x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return d;
  if (mp[x] > 0)
    return mp[x];
  // cout<<x<<' '<<mp.size()<<endl;
  LL ret = 5e18;
  rep(ii, 3) {
    LL i = nn[ii], tmp = x - x % i;
    if (tmp / i != x)
      ret =
          min(ret, dfs(tmp / i) + min(c[ii] + x % i * d, abs(tmp / i - x) * d));
    if (x % i > 0) {
      tmp = x - x % i + i;
      if (tmp / i != x)
        ret = min(ret, dfs(tmp / i) +
                           min(c[ii] + (i - x % i) * d, abs(tmp / i - x) * d));
    }
  }
  // cout<<x<<' '<<ret<<endl;
  return mp[x] = ret;
}

int main() {
  nn[0] = 2;
  nn[1] = 3;
  nn[2] = 5;
  cin >> t;
  while (t--) {
    cin >> n >> c[0] >> c[1] >> c[2] >> d;
    mp.clear();
    cout << dfs(n) << endl;
  }
  return 0;
}