Problem p02669 (C++) — buggy submission, verdict: Runtime Error

// #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ULONG;
typedef long long LONG;

// #define PI 3.1415926535897932384626433
// #define _DBG_

ULONG A, B, C, D;
map<ULONG, ULONG> ma;

ULONG solveOne(ULONG cur) {

  // メモ化済み
  if (ma.find(cur) != ma.end()) {
    return ma[cur];
  }

  if (cur <= 0) {
    return (ULONG)1e18;
  } else if (cur == 1) {
    return D;
  }

  ULONG res = cur * D;

  int mod = cur % 2;
  if (mod == 0) {
    res = min(solveOne(cur / 2) + A, res);
  } else {
    res = min(solveOne((cur - mod) / 2) + A + D * (mod), res);
    res = min(solveOne((cur + (2 - mod)) / 2) + A + D * (2 - mod), res);
  }

  mod = cur % 3;
  if (mod == 0) {
    res = min(solveOne(cur / 3) + B, res);
  } else {
    res = min(solveOne((cur - mod) / 3) + B + D * (mod), res);
    res = min(solveOne((cur + (3 - mod)) / 3) + B + D * (3 - mod), res);
  }

  mod = cur % 5;
  if (mod == 0) {
    res = min(solveOne(cur / 5) + C, res);
  } else {
    res = min(solveOne((cur - mod) / 5) + C + D * (mod), res);
    res = min(solveOne((cur + (5 - mod)) / 5) + C + D * (5 - mod), res);
  }

  ma[cur] = res;
  return res;
}

int main(void) {

  // LONG N;
  // N = 32;
  // A = 10;
  // B = 8;
  // C = 5;
  // D = 4;
  // ma.clear();
  // LONG ans = solveOne(N);
  // cout << ans << endl;
  // cin >> N;

  int T;
  cin >> T;

  vector<vector<ULONG>> param(T, vector<ULONG>(5));
  vector<ULONG> ans(T);
  for (int t = 0; t < T; t++) {
    ULONG N, A, B, C, D;
    cin >> N >> A >> B >> C >> D;
    param[t][0] = N;
    param[t][1] = A;
    param[t][2] = B;
    param[t][3] = C;
    param[t][4] = D;
  }
  for (int t = 0; t < T; t++) {
    ULONG N;
    N = param[t][0];
    A = param[t][1];
    B = param[t][2];
    C = param[t][3];
    D = param[t][4];
    ma.clear();
    ans[t] = solveOne(N);
  }
  for (int t = 0; t < T; t++) {
    cout << ans[t] << endl;
  }

#ifdef _DBG_
  int tmpi;
  cin >> tmpi;
#endif
}


Error: TLE

Fixed submission:

// #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define PI 3.1415926535897932384626433
// #define _DBG_

long long solveOne(vector<long long> param) {
  long long N = 32, A = 10, B = 8, C = 5, D = 4;

  N = param[0];
  A = param[1];
  B = param[2];
  C = param[3];
  D = param[4];

  // Aのmax回数（maxdepth）
  long long maxAnum = (long long)(sqrt(N)) + 1;

  vector<long long> way(maxAnum);
  //	long long mincost = (long long)0 - 1;
  long long mincost = (long long)pow(2, 61) - 1;

  // maxAnum ^ 5(0 *2, *3, *5 +1) の組み合わせを全部やる
  // ただし掛け算だから、昇順のみとかでよい。

  vector<long long> keta(maxAnum, 1);
  vector<long long> baisuCost(7, 0);

  baisuCost[2] = A;
  baisuCost[3] = B;
  baisuCost[5] = C;
  baisuCost[6] = D;
  while (true) {
    long long thiscost = D; // 最初は+1から
    long long totalNo = 1;
    for (long long i = 0; i < maxAnum; i++) {
      if (keta[i] < 6) {
        totalNo *= keta[i];
      } else {
        totalNo++;
      }
      thiscost += baisuCost[keta[i]];
    }

    thiscost += abs((long long)N - (long long)totalNo) * D;

#ifdef _DBG_
    // for (long long i = 0; i < maxAnum; i++) {
    //	cout << keta[i] << " ";
    // }
    // cout << "cost=" << thiscost << endl;
#endif

    if (thiscost < mincost) {
      mincost = thiscost;
    }

    switch (keta[0]) {
    case 1:
      keta[0] = 2;
      break;
    case 2:
      keta[0] = 3;
      break;
    case 3:
      keta[0] = 5;
      break;
    case 5:
      keta[0] = 6;
      break;
    case 6:
      keta[0] = 7;
      break;
    }
    // くり上がり。
    // 降順のみでよい
    for (long long k = 0; k < maxAnum; k++) {

      if (keta[k] == 7) {
        if (k < maxAnum - 1) {
          keta[k] = 1;
          switch (keta[k + 1]) {
          case 1:
            keta[k + 1] = 2;
            break;
          case 2:
            keta[k + 1] = 3;
            break;
          case 3:
            keta[k + 1] = 5;
            break;
          case 5:
            keta[k + 1] = 6;
            break;
          case 6:
            keta[k + 1] = 7;
            break;
          }

          // if (keta[k + 1]!=7){
          //	for (int kk = 0; kk <= k; kk++) {
          //		keta[kk] = keta[k + 1];
          //	}
          // }
        }
      }
    }
    if (keta[maxAnum - 1] == 7)
      break;
  }

  return mincost;
}

int main(void) {
  int T = 1;
  cin >> T;

  vector<vector<long long>> param(T, vector<long long>(5));
  vector<long long> ans(T);
  for (int t = 0; t < T; t++) {
    long long N, A, B, C, D;
    cin >> N >> A >> B >> C >> D;
    param[t][0] = N;
    param[t][1] = A;
    param[t][2] = B;
    param[t][3] = C;
    param[t][4] = D;
  }
  for (int t = 0; t < T; t++) {
    ans[t] = solveOne(param[t]);
  }
  for (int t = 0; t < T; t++) {
    cout << ans[t] << endl;
  }

#ifdef _DBG_
  int tmpi;
  cin >> tmpi;
#endif
}