alexjercan/bugnet huggingface dataset:
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

typedef long long LL;

LL a, b, c, d;
std::unordered_map<LL, LL> map;

template <class T> void update(T &x, const T &y) {
  if (x > y)
    x = y;
}

LL dfs(LL n) {
  // std::cout<<n<<std::endl;
  if (n == 0)
    return 0;
  if (map.count(n))
    return map[n];
  if (n == 1)
    return d;
  LL ans = 6e18;
  if (ans / n >= d)
    update(ans, n * d);
  LL k = n / 2 * 2;
  update(ans, dfs(n / 2) + a + d * (n - k));
  if (n > 2)
    update(ans, dfs(n / 2 + 1) + a + d * (k + 2 - n));
  k = n / 3 * 3;
  update(ans, dfs(n / 3) + b + d * (n - k));
  update(ans, dfs(n / 3 + 1) + b + d * (k + 3 - n));
  k = n / 5 * 5;
  update(ans, dfs(n / 5) + c + d * (n - k));
  update(ans, dfs(n / 5 + 1) + c + d * (k + 5 - n));
  return map[n] = ans;
}

int main() {
  LL n;
  int T;
  for (scanf("%d", &T); T; --T) {
    std::cin >> n >> a >> b >> c >> d;
    map.clear();
    std::cout << dfs(n) << std::endl;
    // for (auto p : map) std::cout << p.first << " " << p.second << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

typedef long long ll;

ll T, n, a, b, c, d;
ll ans;

void bfs() {
  std::priority_queue<std::pair<ll, ll>> q;
  q.push(std::make_pair(0, 0));

  std::pair<ll, ll> t;
  ll now, cost;

  while (!q.empty()) {
    t = q.top();
    q.pop();

    now = t.first, cost = t.second;

    if (cost >= ans)
      continue;

    if (now == n) {
      ans = cost;
    } else if (now == 0) {
      q.push(std::make_pair(now + 1, cost + d));
    } else if (now > n) {
      q.push(std::make_pair(now - 1, cost + d));
    } else {
      q.push(std::make_pair(now * 2, cost + a));
      q.push(std::make_pair(now * 3, cost + b));
      q.push(std::make_pair(now * 5, cost + c));
      q.push(std::make_pair(now + 1, cost + d));
      q.push(std::make_pair(now - 1, cost + d));
    }
  }
}

int main() {

  std::ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> T;

  while (T--) {
    ans = 0x7f7f7f7f7f7f7f7f;
    cin >> n >> a >> b >> c >> d;
    bfs();
    cout << ans << endl;
  }

  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <iostream>
#include <map>
int64_t n, a, b, c, d;
std::map<int64_t, int64_t> memo;

int64_t dfs(int64_t n) {
  if (memo.count(n)) {
    return memo[n];
  }
  int64_t tmp = (1LL << 58);
  if (n < tmp / d)
    tmp = n * d;
  if (n % 2 == 0)
    tmp = std::min(tmp, dfs(n / 2) + a);
  else {
    tmp = std::min(tmp, dfs((n + 1) / 2) + a + d);
    tmp = std::min(tmp, dfs((n - 1) / 2) + a + d);
  }
  if (n % 3 == 0)
    tmp = std::min(tmp, dfs(n / 3) + b);
  else {
    int64_t low3 = n / 3 * 3;
    int64_t high3 = (n / 3 + 1) * 3;
    assert(low3 < n && n < high3);
    tmp = std::min(tmp, dfs(low3 / 3) + b + d * (n - low3));
    tmp = std::min(tmp, dfs(high3 / 3) + b + d * (high3 - n));
  }
  if (n % 5 == 0)
    tmp = std::min(tmp, dfs(n / 5) + c);
  else {
    int64_t low5 = n / 5 * 5;
    int64_t high5 = (n / 5 + 1) * 5;
    assert(low5 < n && n < high5);
    tmp = std::min(tmp, dfs(low5 / 5) + c + d * (n - low5));
    tmp = std::min(tmp, dfs(high5 / 5) + c + d * (high5 - n));
  }
  return memo[n] = tmp;
}

void solve() {
  std::cin >> n >> a >> b >> c >> d;
  memo.clear();
  memo[0] = 0;
  memo[1] = d;
  dfs(n);
  std::cout << memo[n] << std::endl;
}

int main() {
  int t;
  std::cin >> t;
  while (t--)
    solve();
}


Error: -11

Fixed submission:

#include <iostream>
#include <map>

int64_t n, a, b, c, d;
std::map<int64_t, int64_t> memo;

int64_t dfs(int64_t n) {
  if (memo.count(n))
    return memo[n];
  int64_t tmp = (1LL << 58);
  if (n % 2 == 0)
    tmp = std::min(tmp, dfs(n / 2) + a);
  else {
    tmp = std::min(tmp, dfs((n + 1) / 2) + a + d);
    tmp = std::min(tmp, dfs((n - 1) / 2) + a + d);
  }
  if (n % 3 == 0)
    tmp = std::min(tmp, dfs(n / 3) + b);
  else {
    int64_t low3 = n / 3 * 3;
    int64_t high3 = (n / 3 + 1) * 3;
    tmp = std::min(tmp, dfs(low3) + b + d * (n - low3));
    tmp = std::min(tmp, dfs(high3) + b + d * (high3 - n));
  }
  if (n % 5 == 0)
    tmp = std::min(tmp, dfs(n / 5) + c);
  else {
    int64_t low5 = n / 5 * 5;
    int64_t high5 = (n / 5 + 1) * 5;
    tmp = std::min(tmp, dfs(low5) + c + d * (n - low5));
    tmp = std::min(tmp, dfs(high5) + c + d * (high5 - n));
  }
  return memo[n] = tmp;
}

void solve() {
  std::cin >> n >> a >> b >> c >> d;
  memo.clear();
  memo[0] = 0;
  memo[1] = d;
  dfs(n);
  std::cout << memo[n] << std::endl;
}

int main() {
  int t;
  std::cin >> t;
  while (t--)
    solve();
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long int lld;

lld n, a[3], d;
map<lld, lld> mm;
int k[3] = {2, 3, 5};
lld recur(lld n) {
  if (mm.find(n) != mm.end())
    return mm[n];
  lld ans = 1e18;
  if ((long double)d * (long double)n < ans)
    ans = min(ans, d * n);
  for (int p = 0; p < 3; p++) {
    lld cc = recur(n / k[p]) + a[p] + (n % k[p]) * d;
    ans = min(ans, cc);
    if (n % k[p] != 0) {
      lld cc2 = recur((n / k[p]) + 1) + a[p] + (k[p] - (n % k[p])) * d;
      ans = min(ans, cc2);
    }
  }
  return mm[n] = ans;
}
int main(void) {
  int t;
  scanf("%d", &t);
  while (t--) {
    mm.clear();
    scanf("%lld%lld%lld%lld%lld", &n, &a[0], &a[1], &a[2], &d);
    mm[1] = d;
    mm[0] = 0;
    printf("%lld\\n", recur(n));
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int lld;

lld n, a, b, c, d;
map<lld, lld> mm;

void recur(lld n, lld dist) {
  mm[n] = dist;
  if (n == 1)
    return;
  lld dist1 = (n % 2) * d + dist + a;
  lld dist2 = (n % 3) * d + dist + b;
  lld dist3 = (n % 5) * d + dist + c;
  if ((n / 2) >= 1 && (double)(n - (n / 2)) <= (double)dist1 / (double)d)
    dist1 = min(dist1, (n - (n / 2)) * d + dist);
  if ((n / 3) >= 1 && (double)(n - (n / 3)) <= (double)dist2 / (double)d)
    dist2 = min(dist2, (n - (n / 3)) * d + dist);
  if ((n / 5) >= 1 && (double)(n - (n / 5)) <= (double)dist3 / (double)d)
    dist3 = min(dist3, (n - (n / 5)) * d + dist);
  if ((n / 2) >= 1 && mm[n / 2] == 0 || mm[n / 2] > dist1)
    recur(n / 2, dist1);
  if ((n / 3) >= 1 && mm[n / 3] == 0 || mm[n / 3] > dist2)
    recur(n / 3, dist2);
  if ((n / 5) >= 1 && mm[n / 5] == 0 || mm[n / 5] > dist3)
    recur(n / 5, dist3);
  lld dist4 = (2 - (n % 2)) * d + dist + a;
  lld dist5 = (3 - (n % 3)) * d + dist + b;
  lld dist6 = (5 - (n % 5)) * d + dist + c;
  if (n > 2) {
    if ((double)(n - (n / 2) - 1) <= (double)dist4 / (double)d)
      dist4 = min(dist4, (n - (n / 2) - 1) * d + dist);
    if ((double)(n - (n / 3) - 1) <= (double)dist5 / (double)d)
      dist5 = min(dist5, (n - (n / 3) - 1) * d + dist);
    if ((double)(n - (n / 5) - 1) <= (double)dist6 / (double)d)
      dist6 = min(dist6, (n - (n / 5) - 1) * d + dist);
  }
  if (mm[(n / 2) + 1] == 0 || mm[(n / 2) + 1] > dist4)
    recur((n / 2) + 1, dist4);
  if (mm[(n / 3) + 1] == 0 || mm[(n / 3) + 1] > dist5)
    recur((n / 3) + 1, dist5);
  if (mm[(n / 5) + 1] == 0 || mm[(n / 5) + 1] > dist6)
    recur((n / 5) + 1, dist6);
}
int main(void) {
  int t;
  scanf("%d", &t);
  while (t--) {
    mm.clear();
    scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &c, &d);
    recur(n, 0);
    printf("%lld\\n", mm[1] + d);
  }
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define inf 8000000000000000000LL
using namespace std;
int T, a, b, c, d;
long long n;
map<long long, long long> mp;
long long solve(long long x) {
  if (mp.find(x) != mp.end())
    return mp[x];
  if (x <= 1LL)
    return 1LL * x * d;
  long long res = inf;
  if (x % 2 == 0) {
    if (solve(x / 2) <= inf)
      res = min(res, solve(x / 2) + a);
  } else if (x % 2 == 1) {
    if (solve(x / 2) <= inf)
      res = min(res, solve(x / 2) + a + d);
    if (solve((x + 1) / 2) <= inf)
      res = min(res, solve((x + 1) / 2) + a + d);
  }
  if (x % 3 == 0) {
    if (solve(x / 3) <= inf)
      res = min(res, solve(x / 3) + b);
  } else if (x % 3 == 1) {
    if (solve(x / 3) <= inf)
      res = min(res, solve(x / 3) + b + d);
  } else if (x % 3 == 2) {
    if (solve((x + 1) / 3) <= inf)
      res = min(res, solve((x + 1) / 3) + b + d);
  }
  if (x % 5 == 0) {
    if (solve(x / 5) <= inf)
      res = min(res, solve(x / 5) + c);
  } else if (x % 5 == 1) {
    if (solve(x / 5) <= inf)
      res = min(res, solve(x / 5) + c + d);
  } else if (x % 5 == 2) {
    if (solve(x / 5) <= inf)
      res = min(res, solve(x / 5) + c + d + d);
  } else if (x % 5 == 3) {
    if (solve((x + 2) / 5) <= inf)
      res = min(res, solve((x + 2) / 5) + c + d + d);
  } else if (x % 5 == 4) {
    if (solve((x + 1) / 5) <= inf)
      res = min(res, solve((x + 1) / 5) + c + d);
  }
  if (x <= res / d)
    res = 1LL * x * d;
  mp[x] = res;
  return res;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    mp.clear();
    scanf("%lld%d%d%d%d", &n, &a, &b, &c, &d);
    printf("%lld\\n", solve(n));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Maxn 207
using namespace std;
int T, n;
long long a[Maxn];
int op[Maxn];
long long r[62];
bool add(long long x) {
  for (int i = 60; i >= 0; i--)
    if (x & (1LL << i)) {
      if (r[i])
        x ^= r[i];
      else {
        r[i] = x;
        return true;
      }
    }
  return false;
}
void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    char ch = getchar();
    while (ch != '0' && ch != '1')
      ch = getchar();
    if (ch == '0')
      op[i] = 0;
    else
      op[i] = 1;
  }
  memset(r, 0, sizeof(r));
  for (int i = n; i; i--) {
    if (op[i] == 0)
      add(a[i]);
    else if (op[i] == 1 && add(a[i])) {
      printf("%d\\n", 1);
      return;
    }
  }
  printf("%d\\n", 0);
}
int main() {
  scanf("%d", &T);
  while (T--)
    solve();
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1);
const ll INF = 1e18;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};

ll N, A, B, C, D;
map<ll, ll> dp;
ll dfs(ll x) {
  if (dp[x] != 0) {
    return dp[x];
  }
  if (x <= 0) {
    return 0;
  }
  if (x == 1) {
    return D;
  }
  ll k = INF;
  if (x < INF / D) {
    k = x * D;
  }
  k = min(min(k, A + D * (x % 2) + dfs(x / 2)),
          min(B + D * (x % 3) + dfs(x / 3), C + D * (x % 5) + dfs(x / 5)));
  if (x % 2 != 0) {
    k = min(k, A + D * (2 - (x % 2)) + dfs(x / 2 + 1));
  }
  if (x % 3 != 0) {
    k = min(k, B + D * (3 - (x % 3)) + dfs(x / 3 + 1));
  }
  if (x % 5 != 0) {
    k = min(k, C + D * (5 - (x % 5)) + dfs(x / 5 + 1));
  }
  dp[x] = k;
  return k;
}
int main() {
  int t;
  cin >> t;
  rep(i, t) {
    cin >> N >> A >> B >> C >> D;
    dp.clear();
    ll ans = dfs(N);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = acos(-1);
const ll INF = 1e18;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, n) FOR(i, 0, n)
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge {
  ll to, cost;
};

ll n, a, b, c, d, ans;
void bfs(ll x, ll C) {
  if (C < ans) {
    if (x == 1) {
      ans = min(ans, C + d);
    } else if (x == 2) {
      ll z;
      z = min(a + d, 2 * d) + C;
      ans = min(ans, z);
    } else if (x == 0) {
      ans = min(ans, C);
    } else {
      if ((double)log(x) + log(d) < 42) {
        ans = min(ans, x * d + C);
      }
      rep(i, 6) {
        if (i == 0) {
          bfs(x / 2, C + a + d * (x % 2));
        } else if (i == 1 && x % 2 != 0) {
          bfs(x / 2 + 1, C + a + d * (2 - (x % 2)));
        } else if (i == 2) {
          bfs(x / 3, C + b + d * (x % 3));
        } else if (i == 3 && x % 3 != 0) {
          bfs(x / 3 + 1, C + b + d * (3 - (x % 3)));
        } else if (i == 4) {
          bfs(x / 5, C + c + d * (x % 5));
        } else if (i == 5 && x % 5 != 0) {
          bfs(x / 5 + 1, C + c + d * (5 - (x % 5)));
        }
      }
    }
  }
}

int main() {
  ll t;
  cin >> t;
  rep(q, t) {
    cin >> n >> a >> b >> c >> d;
    if (log(d) + log(n) < 42) {
      ans = d * n;
    } else {
      ans = INF;
    }
    bfs(n, 0);
    cout << ans << endl;
  }
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
map<long long, long long> f;
int T;
long long n, a, b, c, d;
long long mo(long long x, long long y) { return y - ((x - 1) % y + 1); }
long long om(long long x, long long y) { return -((x - 1) % y + 1); }
long long getf(long long x) {
  if (f.count(x))
    return f[x];
  if (x == 1) {
    return f[x] = d;
  }
  f[x] = getf((x + mo(x, 2)) / 2) + a + mo(x, 2) * d;
  if (x <= 1e16 / d)
    f[x] = min(f[x], x * d);
  f[x] = min(f[x], getf((x + mo(x, 3)) / 3) + b + mo(x, 3) * d);
  f[x] = min(f[x], getf((x + mo(x, 5)) / 5) + c + mo(x, 5) * d);
  f[x] = min(f[x], getf((x + om(x, 2)) / 2) + a - om(x, 2) * d);
  f[x] = min(f[x], getf((x + om(x, 3)) / 3) + b - om(x, 3) * d);
  f[x] = min(f[x], getf((x + om(x, 5)) / 5) + c - om(x, 5) * d);
  return f[x];
}
void solve() {
  cin >> n >> a >> b >> c >> d;
  f.clear();
  f[0] = 0;
  cout << getf(n) << endl;
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> T;
  while (T--)
    solve();
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
map<long long, long long> f;
int T;
long long n, a, b, c, d;
long long mo(long long x, long long y) { return y - ((x - 1) % y + 1); }
long long om(long long x, long long y) { return -((x - 1) % y + 1); }
long long getf(long long x) {
  if (f.count(x))
    return f[x];
  f[x] = getf((x + mo(x, 2)) / 2) + a + mo(x, 2) * d;
  f[x] = min(f[x], getf((x + mo(x, 3)) / 3) + b + mo(x, 3) * d);
  f[x] = min(f[x], getf((x + mo(x, 5)) / 5) + c + mo(x, 5) * d);
  if ((x + om(x, 2)) != 0)
    f[x] = min(f[x], getf((x + om(x, 2)) / 2) + a - om(x, 2) * d);
  else
    f[x] = min(f[x], getf((x + om(x, 2)) / 2) - om(x, 2) * d);
  if ((x + om(x, 3)) != 0)
    f[x] = min(f[x], getf((x + om(x, 3)) / 3) + b - om(x, 3) * d);
  else
    f[x] = min(f[x], getf((x + om(x, 3)) / 2) - om(x, 3) * d);
  if ((x + om(x, 5)) != 0)
    f[x] = min(f[x], getf((x + om(x, 5)) / 5) + c - om(x, 5) * d);
  else
    f[x] = min(f[x], getf((x + om(x, 5)) / 2) - om(x, 5) * d);
  return f[x];
}
void solve() {
  cin >> n >> a >> b >> c >> d;
  f.clear();
  f[0] = 0;
  cout << getf(n) << endl;
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> T;
  while (T--)
    solve();
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib> //rand()
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
typedef long long ll;
typedef unsigned long long ull;
using std::cin;
using std::cout;
using std::map;
using std::string;

map<ll, ll> mp;
ll a, b, c, d, T, N;

inline ll dfs(ll n) {
  if (mp.count(n))
    return mp[n];
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  ll tmp = 0x7f7f7f7f7f7f7f7f;
  if (n < tmp / d)
    tmp = n * d;
  ll a1 = n / 2 * 2, a2 = (n + 1) / 2 * 2;
  ll b1 = n / 3 * 3, b2 = (n + 2) / 3 * 3;
  ll c1 = n / 5 * 5, c2 = (n + 4) / 5 * 5;
  tmp = std::min(tmp, a + dfs(a1 / 2) + d * std::abs(a1 - n)),
  tmp = std::min(tmp, a + dfs(a2 / 2) + d * std::abs(a2 - n)),
  tmp = std::min(tmp, b + dfs(b1 / 3) + d * std::abs(b1 - n)),
  tmp = std::min(tmp, b + dfs(b2 / 3) + d * std::abs(b2 - n)),
  tmp = std::min(tmp, c + dfs(c1 / 5) + d * std::abs(c1 - n)),
  tmp = std::min(tmp, c + dfs(c2 / 5) + d * std::abs(c2 - n));
  mp[n] = tmp;
  return tmp;
}

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("in.in", "r", stdin);
  cin >> T;
  while (T--) {
    mp.clear();
    cin >> N >> a >> b >> c >> d;
    cout << dfs(N) << "\\n";
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib> //rand()
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
using std::cin;
using std::cout;
using std::string;

const int mod = 998244353;
int hh, ll, h_, l_;
long long dp[3005][3005];

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("color.in", "r", stdin);
  cin >> hh >> ll >> h_ >> l_;
  dp[hh][ll] = 1;
  rep(i, 1, h_) {
    rep(j, 1, l_) {
      if (i < hh || j < ll || (i == hh && j == ll))
        continue;
      dp[i][j] = dp[i - 1][j] * j % mod + dp[i][j - 1] * i % mod + mod -
                 (i - 1) * (j - 1) * dp[i - 1][j - 1] % mod;
      dp[i][j] %= mod;
      // cout << dp[i-1][j] << "*" << (j+ll) << " " << dp[i][j-1]  << "*" <<
      // (i+hh) << " " << (i+hh-1) * (j+ll-1) << "\\n"; cout << ":::" << i << " "
      // << j << " " << dp[i][j] << "\\n";
    }
  }
  cout << dp[h_][l_] << "\\n";
  return 0;
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
using mp = set<pair<ll, ll>>;

ll answer1(ll, ll, ll, ll, ll);
ll nearn(ll, int);
int main() {
  int T;
  ll N;
  ll A, B, C, D;
  cin >> T;

  for (int i = 0; i < T; i++) {
    cin >> N >> A >> B >> C >> D;
    cout << answer1(N, A, B, C, D) << endl;
  }
}

ll nearn(ll a, ll b, bool c) {
  if (c) {
    return (ll)(a - a % b);
  } else {
    return (ll)(a + (b - a % b));
  }
}

ll answer1(ll N, ll A, ll B, ll C, ll D) {
  mp myroute;
  myroute.insert(make_pair(0, N));
  pair<ll, ll> empty = *begin(myroute);
  do {
    ll t = empty.first;
    ll n = empty.second;

    myroute.insert(make_pair(D * (n - 0) + t, 0));

    ll ia = nearn(n, 5, true);

    myroute.insert(make_pair(D * (n - ia) + C + t, ia / 5));
    ia = nearn(n, 5, false);
    myroute.insert(make_pair(D * (ia - n) + C + t, ia / 5));

    ia = nearn(n, 3, true);
    myroute.insert(make_pair(D * (n - ia) + B + t, ia / 3));
    ia = nearn(n, 3, false);
    myroute.insert(make_pair(D * (ia - n) + B + t, ia / 3));

    ia = nearn(n, 2, true);
    myroute.insert(make_pair(D * (n - ia) + A + t, ia / 2));
    ia = nearn(n, 2, false);
    myroute.insert(make_pair(D * (ia - n) + A + t, ia / 2));

    myroute.erase(*begin(myroute));
    empty = *begin(myroute);

  } while (empty.second != 0);

  return empty.first;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
using mp = pair<ll, ll>;

ll answer1(ll, ll, ll, ll, ll);

int main() {
  int T;
  ll N;
  ll A, B, C, D;
  cin >> T;
  // myroute.push(make_pair(0,0));
  for (int i = 0; i < T; i++) {
    cin >> N >> A >> B >> C >> D;
    cout << answer1(N, A, B, C, D) << endl;
  }
}

ll answer1(ll N, ll A, ll B, ll C, ll D) {
  priority_queue<mp, vector<mp>, greater<mp>> myroute;
  myroute.push(make_pair(0, 0));
  vector<ll> mybranch(5);
  mybranch.at(0) = A;
  mybranch.at(1) = B;
  mybranch.at(2) = C;
  mybranch.at(3) = D;
  mybranch.at(4) = D;
  do {
    for (int i = 0; i < 5; i++) {
      mp currentc = myroute.top();
      currentc.first += mybranch.at(i);
      if (i == 0) {
        currentc.second *= 2;
      } else if (i == 1) {
        currentc.second *= 3;
      } else if (i == 2) {
        currentc.second *= 5;
      } else if (i == 3) {
        currentc.second += 1;
      } else {
        currentc.second -= 1;
      }
      if (currentc.second > N)
        continue;
      myroute.push(currentc);
    }
    myroute.pop();
  } while (myroute.top().second != N);

  return myroute.top().first;
}
Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long ans;
long long N, A, B, C, D;
unordered_map<long long, long long> mp;

long long cost(long long N) {
  if (mp.count(N))
    return mp[N];
  if (N == 0)
    return 0;
  if (N == 1)
    return D;
  // cout<<N<<"#"<<endl;
  ans = min({abs(D * N), abs((N - N / 5 * 5) * D + C + cost(N / 5)),
             abs(abs(N - (N + 4) / 5 * 5) * D + C + cost((N + 4) / 5)),
             abs((N - N / 3 * 3) * D + B + cost(N / 3)),
             abs(abs(N - (N + 2) / 3 * 3) * D + B + cost((N + 2) / 3)),
             abs((N - N / 2 * 2) * D + A + cost(N / 2)),
             abs(abs(N - (N + 1) / 2 * 2) * D + A + cost((N + 1) / 2))});
  // cout<<N<<" "<<A<<B<<C<<D<<" "<<ans<<endl;
  return mp[N] = ans;
}

int main() {
  int T;
  cin >> T;

  for (int i = 0; i < T; i++) {
    cin >> N >> A >> B >> C >> D;
    mp.clear();
    cout << setprecision(20) << cost(N) << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long ans = 10000000000000;
long long N, A, B, C, D;
long long memo[100000] = {};
long long cost(long long N) {
  if (memo[N] != 0)
    return memo[N];
  if (N == 0)
    return 0;
  if (N == 1)
    return D;
  if (N == 2)
    return min(D * 2, D + A);
  if (N == 3)
    return min({D * 3, D + B, D * 2 + A});
  // cout<<N<<"#"<<endl;
  ans = min({D * N, (N - N / 5 * 5) * D + C + cost(N / 5),
             ((N + 1) / 5 * 5) * D + C + cost((N + 1) / 5),
             (N - N / 3 * 3) * D + B + cost(N / 3),
             ((N + 1) / 3 * 3) * D + B + cost((N + 1) / 3),
             (N - N / 2 * 2) * D + A + cost(N / 2),
             ((N + 1) / 2 * 2) * D + A + cost((N + 1) / 2)});
  // cout<<N<<" "<<A<<B<<C<<D<<" "<<ans<<endl;
  memo[N] = ans;
  return ans;
}

int main() {
  int T;
  cin >> T;

  for (int i = 0; i < T; i++) {
    cin >> N >> A >> B >> C >> D;
    cout << cost(N) << endl;
  }
  return 0;
}