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

#include <bits/stdc++.h>
using namespace std;
// using Graph = vector<vector<edge>>;
using dou = long double;
string yes = "yes";
string Yes = "Yes";
string YES = "YES";
string no = "no";
string No = "No";
string NO = "NO";

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define brep(n) for (int bit = 0; bit < (1 << n); bit++)
#define erep(i, container) for (auto i : container)
#define irep(i, n) for (int i = n - 1; i >= (int)0; i--)
#define rrep(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define reprep(i, j, h, w) rep(i, h) rep(j, w)
#define all(x) (x).begin(), (x).end()
#define VEC(type, name, n)                                                     \\
  std::vector<type> name(n);                                                   \\
  rep(i, n) std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query                                                                  \\
  int qq;                                                                      \\
  std::cin >> qq;                                                              \\
  rep(qqq, qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a), 0ll)
#define keta fixed << setprecision
#define vvector(name, typ, m, n, a)                                            \\
  vector<vector<typ>> name(m, vector<typ>(n, a))
#define vvvector(name, t, l, m, n, a)                                          \\
  vector<vector<vector<t>>> name(l, vector<vector<t>>(m, vector<t>(n, a)));
#define vvvvector(name, t, k, l, m, n, a)                                      \\
  vector<vector<vector<vector<t>>>> name(                                      \\
      k, vector<vector<vector<t>>>(l, vector<vector<t>>(m, vector<t>(n, a))));
#define case std::cout << "Case #" << qqq + 1 << ": "
#define res resize
#define as assign
#define ffor for (;;)
#define ppri(a, b) std::cout << a << " " << b << std::endl
#define pppri(a, b, c) std::cout << a << " " << b << " " << c << std::endl
#define aall(x, n) (x).begin(), (x).begin() + (n)
#define ssum(a) accumulate(a, 0ll)

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
// typedef long long T;
const int INF = 1000000000;
const ll INF64 = 9223372036854775807ll;
const ll MOD = 1000000007ll;
const dou pi = 3.141592653589793;
// mapは、if関数などで覗いただけで構成されるので注意しよう！
ll n, a, b, c, d;
ll ans = INF64;
std::map<ll, ll> m;
int cou = 0;
void dfs(ll num, ll val) {
  if (m.find(num) != m.end()) {
    if (m[num] <= val)
      return;
    else
      chmin(m[num], val);
  } else {
    m[num] = val;
  }
  /*
  cou++;
  if(cou%20000==0){
      ppri(m.size(),ans);
  }
  */
  if (double(val) + double(num) * double(d) < double(INF64))
    chmin(ans, val + num * d);
  if (num == 1ll)
    return;
  dfs(num / 2, val + d * (num % 2) + a);
  if (num % 2)
    dfs((num + 1) / 2, val + d * (2 - (num % 2)) + a);
  // if(num<=2)return;
  dfs(num / 3, val + d * (num % 3) + b);
  if (num % 3)
    dfs((num + 2) / 3, val + d * (3 - (num % 3)) + b);
  // if(num<=4)return;
  dfs(num / 5, val + d * (num % 5) + c);
  if (num % 5)
    dfs((num + 4) / 5, val + d * (5 - (num % 5)) + c);
  return;
}

int main() {
  query {
    std::cin >> n >> a >> b >> c >> d;
    ans = INF64;
    m.clear();
    dfs(n, 0ll);
    std::cout << ans << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// using Graph = vector<vector<edge>>;
using dou = long double;
string yes = "yes";
string Yes = "Yes";
string YES = "YES";
string no = "no";
string No = "No";
string NO = "NO";

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define brep(n) for (int bit = 0; bit < (1 << n); bit++)
#define erep(i, container) for (auto i : container)
#define irep(i, n) for (int i = n - 1; i >= (int)0; i--)
#define rrep(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define reprep(i, j, h, w) rep(i, h) rep(j, w)
#define all(x) (x).begin(), (x).end()
#define VEC(type, name, n)                                                     \\
  std::vector<type> name(n);                                                   \\
  rep(i, n) std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query                                                                  \\
  int qq;                                                                      \\
  std::cin >> qq;                                                              \\
  rep(qqq, qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a), 0ll)
#define keta fixed << setprecision
#define vvector(name, typ, m, n, a)                                            \\
  vector<vector<typ>> name(m, vector<typ>(n, a))
#define vvvector(name, t, l, m, n, a)                                          \\
  vector<vector<vector<t>>> name(l, vector<vector<t>>(m, vector<t>(n, a)));
#define vvvvector(name, t, k, l, m, n, a)                                      \\
  vector<vector<vector<vector<t>>>> name(                                      \\
      k, vector<vector<vector<t>>>(l, vector<vector<t>>(m, vector<t>(n, a))));
#define case std::cout << "Case #" << qqq + 1 << ": "
#define res resize
#define as assign
#define ffor for (;;)
#define ppri(a, b) std::cout << a << " " << b << std::endl
#define pppri(a, b, c) std::cout << a << " " << b << " " << c << std::endl
#define aall(x, n) (x).begin(), (x).begin() + (n)
#define ssum(a) accumulate(a, 0ll)

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
// typedef long long T;
const int INF = 1000000000;
const ll INF64 = 9223372036854775807ll;
const ll MOD = 1000000007ll;
const dou pi = 3.141592653589793;
// mapは、if関数などで覗いただけで構成されるので注意しよう！
ll n, a, b, c, d;
ll ans = INF64;
std::map<ll, ll> m;
int cou = 0;
void dfs(ll num, ll val) {
  if (m.find(num) != m.end()) {
    if (m[num] < val)
      return;
    else
      chmin(m[num], val);
  } else {
    m[num] = val;
  }
  /*cou++;
  if(cou%20000==0){
      ppri(cou,ans);
  }*/
  if (num < INF)
    chmin(ans, val + num * d);
  if (num == 1)
    return;
  dfs(num / 2, val + d * (num % 2) + a);
  dfs((num + 1) / 2, val + d * (2 - (num % 2)) + a);
  if (num <= 2)
    return;
  dfs(num / 3, val + d * (num % 3) + b);
  dfs((num + 2) / 3, val + d * (3 - (num % 3)) + b);
  if (num <= 4)
    return;
  dfs(num / 5, val + d * (num % 5) + c);
  dfs((num + 4) / 5, val + d * (5 - (num % 5)) + c);
  return;
}

int main() {
  query {
    std::cin >> n >> a >> b >> c >> d;
    ans = INF64;
    m.clear();
    dfs(n, 0);
    std::cout << ans << std::endl;
  }
}