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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int T;
  cin >> T;
  ll N, A, B, C, D;
  pair<ll, ll> state;
  ll ans[T];
  for (int i = 0; i < T; ++i) {
    cin >> N >> A >> B >> C >> D;
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        que;
    map<ll, ll> amap;
    que.push(mp(0, N));
    while (!que.empty()) {
      state = que.top();
      que.pop();
      if (amap[state.se] > 0)
        continue;
      amap[state.se] = state.fi;
      if (state.se == 0)
        break;
      if (state.se % 2 == 0 && amap[state.se / 2] == 0 && state.se / 2 != N) {
        if (A / D >= state.se / 2) {
          que.push(mp(state.fi + (state.se / 2) * D, state.se / 2));
        } else
          que.push(mp(state.fi + A, state.se / 2));
      }
      if (state.se % 3 == 0 && amap[state.se / 3] == 0 && state.se / 3 != N) {
        if (B / D >= state.se / 3 * 2) {
          que.push(mp(state.fi + (state.se / 3 * 2) * D, state.se / 3));
        } else
          que.push(mp(state.fi + B, state.se / 3));
      }
      if (state.se % 5 == 0 && amap[state.se / 5] == 0 && state.se / 5 != N) {
        if (C / D >= state.se / 5 * 4) {
          que.push(mp(state.fi + (state.se / 5 * 4) * D, state.se / 5));
        } else
          que.push(mp(state.fi + C, state.se / 5));
      }
      if (amap[state.se - (state.se % 2)] == 0 &&
          state.se - (state.se % 2) != N) {
        que.push(mp(state.fi + (state.se % 2) * D, state.se - (state.se % 2)));
      }
      if (amap[state.se - (state.se % 3)] == 0 &&
          state.se - (state.se % 3) != N) {
        que.push(mp(state.fi + (state.se % 3) * D, state.se - (state.se % 3)));
      }
      if (amap[state.se - (state.se % 5)] == 0 &&
          state.se - (state.se % 5) != N) {
        que.push(mp(state.fi + (state.se % 5) * D, state.se - (state.se % 5)));
      }
      if (amap[state.se + (2 - state.se % 2) % 2] == 0 &&
          state.se + (2 - state.se % 2) % 2 != N) {
        que.push(mp(state.fi + ((2 - state.se % 2) % 2) * D,
                    state.se + (2 - state.se % 2) % 2));
      }
      if (amap[state.se + (3 - state.se % 3) % 3] == 0 &&
          state.se + (3 - state.se % 3) % 3 != N) {
        que.push(mp(state.fi + ((3 - state.se % 3) % 3) * D,
                    state.se + (3 - state.se % 3) % 3));
      }
      if (amap[state.se + (5 - state.se % 5) % 5] == 0 &&
          state.se + (5 - state.se % 5) % 5 != N) {
        que.push(mp(state.fi + ((5 - state.se % 5) % 5) * D,
                    state.se + (5 - state.se % 5) % 5));
      }
    }
    ans[i] = amap[0];
  }
  for (int i = 0; i < T; ++i)
    cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int T;
  cin >> T;
  ll N, A, B, C, D;
  pair<ll, ll> state;
  ll ans[T];
  for (int i = 0; i < T; ++i) {
    cin >> N >> A >> B >> C >> D;
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        que;
    map<ll, ll> amap;
    que.push(mp(0, N));
    while (!que.empty()) {
      state = que.top();
      que.pop();
      if (amap[state.se] > 0)
        continue;
      amap[state.se] = state.fi;
      if (state.se == 0)
        break;
      if (state.se % 2 == 0 && D * (state.se / 2) > 0 &&
          amap[state.se / 2] == 0 && state.se / 2 != N)
        que.push(
            mp(min(state.fi + A, state.fi + D * (state.se / 2)), state.se / 2));
      if (state.se % 2 == 0 && D * (state.se / 2) <= 0 &&
          amap[state.se / 2] == 0 && state.se / 2 != N)
        que.push(mp(state.fi + A, state.se / 2));
      if (state.se % 3 == 0 && D * (state.se / 3 * 2) > 0 &&
          amap[state.se / 3] == 0 && state.se / 3 != N)
        que.push(mp(min(state.fi + B, state.fi + D * (state.se / 3 * 2)),
                    state.se / 3));
      if (state.se % 3 == 0 && D * (state.se / 3 * 2) <= 0 &&
          amap[state.se / 3] == 0 && state.se / 3 != N)
        que.push(mp(state.fi + B, state.se / 3));
      if (state.se % 5 == 0 && D * (state.se / 5 * 4) > 0 &&
          amap[state.se / 5] == 0 && state.se / 5 != N)
        que.push(mp(min(state.fi + C, state.fi + D * (state.se / 5 * 4)),
                    state.se / 5));
      if (state.se % 5 == 0 && D * (state.se / 5 * 4) <= 0 &&
          amap[state.se / 5] == 0 && state.se / 5 != N)
        que.push(mp(state.fi + C, state.se / 5));
      if (amap[state.se - (state.se % 2)] == 0 &&
          state.se - (state.se % 2) != N) {
        que.push(mp(state.fi + D * (state.se % 2), state.se - (state.se % 2)));
      }
      if (amap[state.se - (state.se % 3)] == 0 &&
          state.se - (state.se % 3) != N) {
        que.push(mp(state.fi + D * (state.se % 3), state.se - (state.se % 3)));
      }
      if (amap[state.se - (state.se % 5)] == 0 &&
          state.se - (state.se % 5) != N) {
        que.push(mp(state.fi + D * (state.se % 5), state.se - (state.se % 5)));
      }
      if (amap[state.se + (2 - state.se % 2) % 2] == 0 &&
          state.se + (2 - state.se % 2) % 2 != N) {
        que.push(mp(state.fi + D * ((2 - state.se % 2) % 2),
                    state.se + (2 - state.se % 2) % 2));
      }
      if (amap[state.se + (3 - state.se % 3) % 3] == 0 &&
          state.se + (3 - state.se % 3) % 3 != N) {
        que.push(mp(state.fi + D * ((3 - state.se % 3) % 3),
                    state.se + (3 - state.se % 3) % 3));
      }
      if (amap[state.se + (5 - state.se % 5) % 5] == 0 &&
          state.se + (5 - state.se % 5) % 5 != N) {
        que.push(mp(state.fi + D * ((5 - state.se % 5) % 5),
                    state.se + (5 - state.se % 5) % 5));
      }
    }
    ans[i] = amap[0];
  }
  for (int i = 0; i < T; ++i)
    cout << ans[i] << endl;
}