Problem p02669 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second
#define mp make_pair

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

ll N, A, B, C, D;
map<ll, ll> MP;

ll Solve(ll num) {
  if (num <= 0)
    return 0;
  if (num == 1)
    return MP[num] = D;
  if (MP[num] > 0)
    return MP[num];

  ll ans = INF;
  for (ll i = -3; i <= 5; i++) {
    ll now = num - i;
    if (now < 0)
      continue;
    if (now % 2 == 0 && now / 2 < num) {
      ans = min(ans, Solve(now / 2) + A + abs(i) * D);
      ll Lim = (INF - Solve(now / 2)) / D;
      ll deg = abs(now / 2 - num);
      if (deg <= Lim) {
        ans = min(ans, Solve(now / 2) + deg * D);
      }
    }
    if (now % 3 == 0 && now / 3 < num) {
      ans = min(ans, Solve(now / 3) + B + abs(i) * D);
      ll Lim = (INF - Solve(now / 3)) / D;
      ll deg = abs(now / 3 - num);
      if (deg <= Lim) {
        ans = min(ans, Solve(now / 3) + deg * D);
      }
    }
    if (now % 5 == 0 && now / 5 < num) {
      ans = min(ans, Solve(now / 5) + C + abs(i) * D);
      ll Lim = (INF - Solve(now / 5)) / D;
      ll deg = abs(now / 5 - num);
      if (deg <= Lim) {
        ans = min(ans, Solve(now / 5) + deg * D);
      }
    }
    if (MP[now] > 0)
      ans = min(ans, MP[now] + abs(i) * D);
  }
  return MP[num] = ans;
}

int Ans() {
  MP = {};
  cin >> N >> A >> B >> C >> D;
  MP[1] = D;
  cout << Solve(N) << endl;
  return 0;
}

int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++)
    Ans();
  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second
#define mp make_pair

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }

ll N, A, B, C, D;
map<ll, ll> MP;

ll Solve(ll num) {
  if (num <= 0)
    return 0;
  if (num == 1)
    return MP[num] = D;
  if (MP[num] > 0)
    return MP[num];

  ll ans = INF;
  for (ll i = -3; i < 5; i++) {
    ll now = num - i;
    if (now < 0)
      continue;
    if (now % 2 == 0 && now / 2 < num) {
      ans = min(ans, Solve(now / 2) + A + abs(i) * D);
      ll Lim = (INF - Solve(now / 2)) / D;
      ll deg = abs(now / 2 - num);
      if (deg <= Lim) {
        ans = min(ans, Solve(now / 2) + deg * D);
      }
    }
    if (now % 3 == 0 && now / 3 < num) {
      ans = min(ans, Solve(now / 3) + B + abs(i) * D);
      ll Lim = (INF - Solve(now / 3)) / D;
      ll deg = abs(now / 3 - num);
      if (deg <= Lim) {
        ans = min(ans, Solve(now / 3) + deg * D);
      }
    }
    if (now % 5 == 0 && now / 5 < num) {
      ans = min(ans, Solve(now / 5) + C + abs(i) * D);
      ll Lim = (INF - Solve(now / 5)) / D;
      ll deg = abs(now / 5 - num);
      if (deg <= Lim) {
        ans = min(ans, Solve(now / 5) + deg * D);
      }
    }
    if (MP[now] > 0)
      ans = min(ans, MP[now] + abs(i) * D);
  }
  return MP[num] = ans;
}

int Ans() {
  MP = {};
  cin >> N >> A >> B >> C >> D;
  MP[1] = D;
  cout << Solve(N) << endl;
  return 0;
}

int main() {
  int T;
  cin >> T;
  for (int i = 0; i < T; i++)
    Ans();
  return 0;
}