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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* template */

using ll = long long;

void debug_out() { std::cout << std::endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  cout << "debug: ";                                                           \\
  debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (long long i = (long long)(a); i < (long long)(n); i++)
#define RRep(i, a, n)                                                          \\
  for (long long i = ((long long)(n - 1ll)); i >= (long long)(a); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

struct Edge {
  int to;
  ll weight;
  Edge(int t, ll w) : to(t), weight(w) {}
};

struct edge {
  int from;
  int to;
  ll weight;
  edge(int f, int t, ll w) : from(f), to(t), weight(w) {}
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll LNF = 1ll << 60;

constexpr int INF = 1e9 + 7;

vector<int> dx = {-1, 0, 1, 0, -1, 1, -1, 1};
vector<int> dy = {0, -1, 0, 1, 1, -1, -1, 1};

/* template */
ll n, a, b, c, d;
map<ll, ll> m;

ll dfs(ll here) {
  if (m[here] != 0)
    return m[here];
  if (here <= 0)
    return 0;
  if (here == 1)
    return d;
  ll t = LNF;
  if (here < LNF / d)
    t = here * d;
  chmin(t, a + d * (here % 2ll) + dfs(here / 2ll));
  if (here % 2 == 1) {
    chmin(t, a + d + dfs((here + 1ll) / 2ll));
  }
  chmin(t, b + d * (here % 3ll) + dfs(here / 3ll));
  if (here % 3 != 0) {
    chmin(t, b + d * (3ll - here % 3ll) + dfs((here + 2ll) / 3ll));
  }
  chmin(t, c + d * (here % 5ll) + dfs(here / 5ll));
  if (here % 5 != 0) {
    chmin(t, c + d * (5ll - here % 5ll) + dfs((here + 4ll) / 5ll));
  }
  m[here] = t;
  return t;
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  rep(_, 0, t) {
    cin >> n >> a >> b >> c >> d;
    m.clear();
    ll ans = dfs(n);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* template */

using ll = long long;

void debug_out() { std::cout << std::endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  cout << "debug: ";                                                           \\
  debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (long long i = (long long)(a); i < (long long)(n); i++)
#define RRep(i, a, n)                                                          \\
  for (long long i = ((long long)(n - 1ll)); i >= (long long)(a); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

struct Edge {
  int to;
  ll weight;
  Edge(int t, ll w) : to(t), weight(w) {}
};

struct edge {
  int from;
  int to;
  ll weight;
  edge(int f, int t, ll w) : from(f), to(t), weight(w) {}
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll LNF = 1ll << 60;

constexpr int INF = 1e9 + 7;

vector<int> dx = {-1, 0, 1, 0, -1, 1, -1, 1};
vector<int> dy = {0, -1, 0, 1, 1, -1, -1, 1};

/* template */
ll n, a, b, c, d;
map<ll, ll> m;

ll dfs(ll here) {
  if (m[here] != 0)
    return m[here];
  if (here <= 0)
    return 0;
  if (here == 1)
    return d;
  ll t = LNF;
  if (here < LNF / d)
    t = here * d;
  chmin(t, a + d * (here % 2ll) + dfs(here / 2ll));
  if (here % 2 == 1) {
    chmin(t, a + d + dfs((here + 1ll) / 2ll));
  }
  chmin(t, b + d * (here % 3ll) + dfs(here / 3ll));
  if (here % 3 != 0) {
    chmin(t, b + d * (3ll - here % 3ll) + dfs((here + 2ll) / 3ll));
  }
  chmin(t, c + d * (here % 5ll) + dfs(here / 5ll));
  if (here % 5 != 0) {
    chmin(t, c + d * (5ll - here % 5ll) + dfs((here + 4ll) / 5ll));
  }
  m[here] = t;
  return t;
};

int solve() {
  cin >> n >> a >> b >> c >> d;
  m.clear();
  /*
  auto dfs = [&](ll here, auto &dfs)->ll{
      if(m[here]!=0) return m[here];
      if(here<=0) return 0;
      if(here==1) return d;
      ll t = LNF;
      if(here<LNF/d) t = here*d;
      chmin(t,a+d*(here%2ll)+dfs(here/2ll,dfs));
      if(here%2==1){
          chmin(t,a+d+dfs((here+1ll)/2ll,dfs));
      }
      chmin(t,b+d*(here%3ll)+dfs(here/3ll,dfs));
      if(here%3!=0){
          chmin(t,b+d*(3ll-here%3ll)+dfs((here+2ll)/3ll,dfs));
      }
      chmin(t,c+d*(here%5ll)+dfs(here/5ll,dfs));
      if(here%5!=0){
          chmin(t,c+d*(5ll-here%5ll)+dfs((here+4ll)/5ll,dfs));
      }
      m[here] = t;
      return t;
  };
  */
  dfs(n);
  cout << m[n] << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  rep(_, 0, t) { solve(); }
}