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

#include <bits/stdc++.h>
using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL << 60;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int T;
  cin >> T;
  rep(i, T) {
    ll N, A, B, C, D;
    cin >> N >> A >> B >> C >> D;
    map<ll, ll> m;
    m[N] = 0LL;
    ll ans = INF;
    map<ll, ll> cost;
    cost[2] = A;
    cost[3] = B;
    cost[5] = C;
    while (true && N != 1) {
      map<ll, ll> t;
      for (auto x : m) {
        if (x.first == 1)
          continue;
        ll l = x.first;
        if ((l - 1) < 1000000000000000000 / D) {
          if (t[1] == 0)
            t[1] = (l - 1) * D + x.second;
          else
            chmin(t[1], (l - 1) * D + x.second);
        }
        for (auto y : cost) {
          ll p = y.first;
          ll q = y.second;
          if (l % p == 0) {
            if (t[l / p] == 0)
              t[l / p] = x.second + q;
            else
              chmin(t[l / p], x.second + q);
          } else {
            ll r = l % p;
            if (l / p != 0) {
              if (t[l / p] == 0)
                t[l / p] = x.second + r * D + q;
              else
                chmin(t[l / p], x.second + r * D + q);
            }
            if (t[l / p + 1] == 0)
              t[l / p + 1] = x.second + (p - r) * D + q;
            else
              chmin(t[l / p + 1], x.second + (p - r) * D + q);
          }
        }
      }

      m = t;
      if (m[1] != 0)
        chmin(ans, m[1]);
      if ((int)t.size() <= 1)
        break;
    }
    if (N != 1)
      cout << ans + D << endl;
    else
      cout << D << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using P = pair<int, int>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL << 60;
const ll mod = 1000000007LL;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int T;
  cin >> T;
  rep(i, T) {
    ll N, A, B, C, D;
    cin >> N >> A >> B >> C >> D;
    map<ll, ll> m;
    m[N] = 0LL;
    ll ans = INF;
    map<ll, ll> cost;
    cost[2] = A;
    cost[3] = B;
    cost[5] = C;
    while (true) {
      map<ll, ll> t;
      for (auto x : m) {
        if (x.first == 1)
          continue;
        ll l = x.first;
        if ((l - 1) < 1000000000000000000 / D) {
          if (t[1] == 0)
            t[1] = (l - 1) * D + x.second;
          else
            chmin(t[1], (l - 1) * D + x.second);
        }
        for (auto y : cost) {
          ll p = y.first;
          ll q = y.second;
          if (l % p == 0) {
            if (t[l / p] == 0)
              t[l / p] = x.second + q;
            else
              chmin(t[l / p], x.second + q);
          } else {
            ll r = l % p;
            if (l / p != 0) {
              if (t[l / p] == 0)
                t[l / p] = x.second + r * D + q;
              else
                chmin(t[l / p], x.second + r * D + q);
            }
            if (t[l / p + 1] == 0)
              t[l / p + 1] = x.second + (p - r) * D + q;
            else
              chmin(t[l / p + 1], x.second + (p - r) * D + q);
          }
        }
      }

      m = t;
      if (m[1] != 0)
        chmin(ans, m[1]);
      if ((int)t.size() == 1)
        break;
    }
    cout << ans + D << endl;
  }
}